db_test.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * @license
  3. * Blockly Tests
  4. *
  5. * Copyright 2016 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. 'use strict';
  21. function test_DB_getNeighbours() {
  22. var db = new Blockly.ConnectionDB();
  23. // Search an empty list.
  24. assertEquals(helper_getNeighbours(db, 10 /* x */, 10 /* y */, 100 /* radius */).length, 0);
  25. // Set up some connections.
  26. for (var i = 0; i < 10; i++) {
  27. db.addConnection_(helper_createConnection(0, i, Blockly.PREVIOUS_STATEMENT));
  28. }
  29. // Test block belongs at beginning
  30. var result = helper_getNeighbours(db, 0, 0, 4);
  31. assertEquals(5, result.length);
  32. for (i = 0; i < result.length; i++) {
  33. assertNotEquals(result.indexOf(db[i]), -1); // contains
  34. }
  35. // Test block belongs at middle
  36. result = helper_getNeighbours(db, 0, 4, 2);
  37. assertEquals(5, result.length);
  38. for (i = 0; i < result.length; i++) {
  39. assertNotEquals(result.indexOf(db[i + 2]), -1); // contains
  40. }
  41. // Test block belongs at end
  42. result = helper_getNeighbours(db, 0, 9, 4);
  43. assertEquals(5, result.length);
  44. for (i = 0; i < result.length; i++) {
  45. assertNotEquals(result.indexOf(db[i + 5]), -1); // contains
  46. }
  47. // Test block has no neighbours due to being out of range in the x direction
  48. result = helper_getNeighbours(db, 10, 9, 4);
  49. assertEquals(result.length, 0);
  50. // Test block has no neighbours due to being out of range in the y direction
  51. result = helper_getNeighbours(db, 0, 19, 4);
  52. assertEquals(result.length, 0);
  53. // Test block has no neighbours due to being out of range diagonally
  54. result = helper_getNeighbours(db, -2, -2, 2);
  55. assertEquals(result.length, 0);
  56. }
  57. function helper_getNeighbours(db, x, y, radius) {
  58. return db.getNeighbours(helper_createConnection(x, y, Blockly.NEXT_STATEMENT), radius);
  59. }
  60. function helper_createConnection(x, y, type) {
  61. var conn = new Blockly.Connection({workspace: {}}, type);
  62. conn.x_ = x;
  63. conn.y_ = y;
  64. return conn;
  65. }