789b3748b14861b58e065f5d88bc9c978aabe96693682302a233a3e163a0a5f5f73f87590967d6b4988e0b33043d84dda743328ae06006c55368928736d125 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. 'use strict';
  2. describe('Walkontable.CellRange', function () {
  3. describe('getAll', function () {
  4. it('should get all cells in range', function () {
  5. var from = new Walkontable.CellCoords(1, 1);
  6. var to = new Walkontable.CellCoords(3, 3);
  7. var range = new Walkontable.CellRange(from, from, to);
  8. var all = range.getAll();
  9. expect(all.length).toBe(9);
  10. expect(all[0].row).toBe(from.row);
  11. expect(all[0].col).toBe(from.col);
  12. expect(all[1].row).toBe(1);
  13. expect(all[1].col).toBe(2);
  14. expect(all[8].row).toBe(to.row);
  15. expect(all[8].col).toBe(to.col);
  16. });
  17. it('should get all cells in range (reverse order)', function () {
  18. var from = new Walkontable.CellCoords(3, 3);
  19. var to = new Walkontable.CellCoords(1, 1);
  20. var range = new Walkontable.CellRange(from, from, to);
  21. var all = range.getAll();
  22. expect(all.length).toBe(9);
  23. expect(all[0].row).toBe(to.row);
  24. expect(all[0].col).toBe(to.col);
  25. expect(all[1].row).toBe(1);
  26. expect(all[1].col).toBe(2);
  27. expect(all[8].row).toBe(from.row);
  28. expect(all[8].col).toBe(from.col);
  29. });
  30. });
  31. describe('getInner', function () {
  32. it('should get cells in range excluding from and to', function () {
  33. var from = new Walkontable.CellCoords(1, 1);
  34. var to = new Walkontable.CellCoords(3, 3);
  35. var range = new Walkontable.CellRange(from, from, to);
  36. var inner = range.getInner();
  37. expect(inner.length).toBe(7);
  38. expect(inner[1].row).toBe(1);
  39. expect(inner[1].col).toBe(3);
  40. });
  41. it('should get cells in range excluding from and to (reverse order)', function () {
  42. var from = new Walkontable.CellCoords(3, 3);
  43. var to = new Walkontable.CellCoords(1, 1);
  44. var range = new Walkontable.CellRange(from, from, to);
  45. var inner = range.getInner();
  46. expect(inner.length).toBe(7);
  47. expect(inner[1].row).toBe(1);
  48. expect(inner[1].col).toBe(3);
  49. });
  50. });
  51. describe('includes', function () {
  52. it('should return true if range is a single cell and the same cell is given', function () {
  53. var from = new Walkontable.CellCoords(0, 0);
  54. var to = new Walkontable.CellCoords(0, 0);
  55. var range = new Walkontable.CellRange(from, from, to);
  56. expect(range.includes(new Walkontable.CellCoords(0, 0))).toBe(true);
  57. });
  58. it('should return true if given cell is within the range', function () {
  59. var from = new Walkontable.CellCoords(1, 1);
  60. var to = new Walkontable.CellCoords(3, 3);
  61. var range = new Walkontable.CellRange(from, from, to);
  62. expect(range.includes(new Walkontable.CellCoords(1, 1))).toBe(true);
  63. expect(range.includes(new Walkontable.CellCoords(3, 1))).toBe(true);
  64. expect(range.includes(new Walkontable.CellCoords(3, 3))).toBe(true);
  65. expect(range.includes(new Walkontable.CellCoords(1, 3))).toBe(true);
  66. expect(range.includes(new Walkontable.CellCoords(2, 2))).toBe(true);
  67. expect(range.includes(new Walkontable.CellCoords(1, 2))).toBe(true);
  68. expect(range.includes(new Walkontable.CellCoords(2, 1))).toBe(true);
  69. });
  70. it('should return false if given cell outside the range', function () {
  71. var from = new Walkontable.CellCoords(1, 1);
  72. var to = new Walkontable.CellCoords(3, 3);
  73. var range = new Walkontable.CellRange(from, from, to);
  74. expect(range.includes(new Walkontable.CellCoords(0, 0))).toBe(false);
  75. expect(range.includes(new Walkontable.CellCoords(4, 4))).toBe(false);
  76. expect(range.includes(new Walkontable.CellCoords(1, 4))).toBe(false);
  77. expect(range.includes(new Walkontable.CellCoords(4, 1))).toBe(false);
  78. expect(range.includes(new Walkontable.CellCoords(-1, -1))).toBe(false);
  79. });
  80. });
  81. describe('includesRange', function () {
  82. describe('B has more than one cell', function () {
  83. /*
  84. +----------+
  85. | a |
  86. | +------+ |
  87. | | b | |
  88. | | | |
  89. | +------+ |
  90. +----------+
  91. */
  92. it('B is included in A, none of borders touch each other', function () {
  93. var aTopLeft = new Walkontable.CellCoords(0, 0);
  94. var aBottomRight = new Walkontable.CellCoords(5, 5);
  95. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  96. var bTopLeft = new Walkontable.CellCoords(1, 1);
  97. var bBottomRight = new Walkontable.CellCoords(4, 4);
  98. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  99. expect(a.includesRange(b)).toBe(true);
  100. });
  101. /*
  102. +----------+
  103. | b |
  104. | +------+ |
  105. | | a | |
  106. | | | |
  107. | +------+ |
  108. +----------+
  109. */
  110. it('A is included in B, none of borders touch each other', function () {
  111. var aTopLeft = new Walkontable.CellCoords(1, 1);
  112. var aBottomRight = new Walkontable.CellCoords(4, 4);
  113. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  114. var bTopLeft = new Walkontable.CellCoords(0, 0);
  115. var bBottomRight = new Walkontable.CellCoords(5, 4);
  116. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  117. expect(a.includesRange(b)).toBe(false);
  118. });
  119. /*
  120. +-----------+
  121. | a | b | |
  122. | | | |
  123. | +-----+ |
  124. +-----------+
  125. */
  126. it('B is included in A, top borders touch', function () {
  127. var aTopLeft = new Walkontable.CellCoords(0, 0);
  128. var aBottomRight = new Walkontable.CellCoords(5, 5);
  129. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  130. var bTopLeft = new Walkontable.CellCoords(0, 1);
  131. var bBottomRight = new Walkontable.CellCoords(4, 4);
  132. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  133. expect(a.includesRange(b)).toBe(true);
  134. });
  135. /*
  136. +---------+
  137. | a | b |
  138. | | |
  139. | +-----|
  140. | |
  141. +---------+
  142. */
  143. it('B is included in A, top and right borders touch', function () {
  144. var aTopLeft = new Walkontable.CellCoords(0, 0);
  145. var aBottomRight = new Walkontable.CellCoords(5, 5);
  146. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  147. var bTopLeft = new Walkontable.CellCoords(0, 1);
  148. var bBottomRight = new Walkontable.CellCoords(4, 5);
  149. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  150. expect(a.includesRange(b)).toBe(true);
  151. });
  152. /*
  153. +---------+
  154. | +-----|
  155. | a | b |
  156. | | |
  157. | +-----|
  158. +---------+
  159. */
  160. it('B is included in A, right borders touch', function () {
  161. var aTopLeft = new Walkontable.CellCoords(0, 0);
  162. var aBottomRight = new Walkontable.CellCoords(5, 5);
  163. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  164. var bTopLeft = new Walkontable.CellCoords(1, 1);
  165. var bBottomRight = new Walkontable.CellCoords(4, 5);
  166. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  167. expect(a.includesRange(b)).toBe(true);
  168. });
  169. /*
  170. +---------+
  171. | +-----|
  172. | a | b |
  173. | | |
  174. +---------+
  175. */
  176. it('B is included in A, bottom and right borders touch', function () {
  177. var aTopLeft = new Walkontable.CellCoords(0, 0);
  178. var aBottomRight = new Walkontable.CellCoords(5, 5);
  179. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  180. var bTopLeft = new Walkontable.CellCoords(1, 1);
  181. var bBottomRight = new Walkontable.CellCoords(5, 5);
  182. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  183. expect(a.includesRange(b)).toBe(true);
  184. });
  185. /*
  186. +-----------+
  187. | +-----+ |
  188. | a | b | |
  189. | | | |
  190. +-----------+
  191. */
  192. it('B is included in A, bottom borders touch', function () {
  193. var aTopLeft = new Walkontable.CellCoords(0, 0);
  194. var aBottomRight = new Walkontable.CellCoords(5, 5);
  195. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  196. var bTopLeft = new Walkontable.CellCoords(1, 1);
  197. var bBottomRight = new Walkontable.CellCoords(5, 4);
  198. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  199. expect(a.includesRange(b)).toBe(true);
  200. });
  201. /*
  202. +-----------+
  203. |-----+ a |
  204. | b | |
  205. | | |
  206. +-----------+
  207. */
  208. it('B is included in A, bottom and left borders touch', function () {
  209. var aTopLeft = new Walkontable.CellCoords(0, 0);
  210. var aBottomRight = new Walkontable.CellCoords(5, 5);
  211. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  212. var bTopLeft = new Walkontable.CellCoords(1, 0);
  213. var bBottomRight = new Walkontable.CellCoords(5, 4);
  214. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  215. expect(a.includesRange(b)).toBe(true);
  216. });
  217. /*
  218. +-----------+
  219. |-----+ a |
  220. | b | |
  221. | | |
  222. |-----+ |
  223. +-----------+
  224. */
  225. it('B is included in A, left borders touch', function () {
  226. var aTopLeft = new Walkontable.CellCoords(0, 0);
  227. var aBottomRight = new Walkontable.CellCoords(5, 5);
  228. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  229. var bTopLeft = new Walkontable.CellCoords(1, 0);
  230. var bBottomRight = new Walkontable.CellCoords(4, 4);
  231. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  232. expect(a.includesRange(b)).toBe(true);
  233. });
  234. /*
  235. +-----------+
  236. | b | a |
  237. | | |
  238. |-----+ |
  239. +-----------+
  240. */
  241. it('B is included in A, top and left borders touch', function () {
  242. var aTopLeft = new Walkontable.CellCoords(0, 0);
  243. var aBottomRight = new Walkontable.CellCoords(5, 5);
  244. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  245. var bTopLeft = new Walkontable.CellCoords(0, 0);
  246. var bBottomRight = new Walkontable.CellCoords(4, 4);
  247. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  248. expect(a.includesRange(b)).toBe(true);
  249. });
  250. /*
  251. +------------+
  252. | a | b | |
  253. | | | |
  254. +------------+
  255. */
  256. it('B is included in A, top and bottom borders touch', function () {
  257. var aTopLeft = new Walkontable.CellCoords(0, 0);
  258. var aBottomRight = new Walkontable.CellCoords(5, 5);
  259. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  260. var bTopLeft = new Walkontable.CellCoords(0, 1);
  261. var bBottomRight = new Walkontable.CellCoords(5, 4);
  262. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  263. expect(a.includesRange(b)).toBe(true);
  264. });
  265. /*
  266. +----------+
  267. | a | b |
  268. | | |
  269. +----------+
  270. */
  271. it('B is included in A, top, right and bottom borders touch', function () {
  272. var aTopLeft = new Walkontable.CellCoords(0, 0);
  273. var aBottomRight = new Walkontable.CellCoords(5, 5);
  274. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  275. var bTopLeft = new Walkontable.CellCoords(0, 1);
  276. var bBottomRight = new Walkontable.CellCoords(5, 5);
  277. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  278. expect(a.includesRange(b)).toBe(true);
  279. });
  280. /*
  281. +----------+
  282. | b | a |
  283. | | |
  284. +----------+
  285. */
  286. it('B is included in A, top, left and bottom borders touch', function () {
  287. var aTopLeft = new Walkontable.CellCoords(0, 0);
  288. var aBottomRight = new Walkontable.CellCoords(5, 5);
  289. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  290. var bTopLeft = new Walkontable.CellCoords(0, 0);
  291. var bBottomRight = new Walkontable.CellCoords(5, 4);
  292. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  293. expect(a.includesRange(b)).toBe(true);
  294. });
  295. /*
  296. +----------+
  297. | a |
  298. |----------|
  299. | b |
  300. |----------|
  301. +----------+
  302. */
  303. it('B is included in A, left and right borders touch', function () {
  304. var aTopLeft = new Walkontable.CellCoords(0, 0);
  305. var aBottomRight = new Walkontable.CellCoords(5, 5);
  306. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  307. var bTopLeft = new Walkontable.CellCoords(1, 0);
  308. var bBottomRight = new Walkontable.CellCoords(4, 5);
  309. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  310. expect(a.includesRange(b)).toBe(true);
  311. });
  312. /*
  313. +----------+
  314. | a |
  315. |----------|
  316. | b |
  317. +----------+
  318. */
  319. it('B is included in A, left, bottom and right borders touch', function () {
  320. var aTopLeft = new Walkontable.CellCoords(0, 0);
  321. var aBottomRight = new Walkontable.CellCoords(5, 5);
  322. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  323. var bTopLeft = new Walkontable.CellCoords(1, 0);
  324. var bBottomRight = new Walkontable.CellCoords(5, 5);
  325. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  326. expect(a.includesRange(b)).toBe(true);
  327. });
  328. /*
  329. +----------+
  330. | b |
  331. |----------|
  332. | a |
  333. +----------+
  334. */
  335. it('B is included in A, left, top and right borders touch', function () {
  336. var aTopLeft = new Walkontable.CellCoords(0, 0);
  337. var aBottomRight = new Walkontable.CellCoords(5, 5);
  338. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  339. var bTopLeft = new Walkontable.CellCoords(0, 0);
  340. var bBottomRight = new Walkontable.CellCoords(4, 5);
  341. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  342. expect(a.includesRange(b)).toBe(true);
  343. });
  344. });
  345. describe('B has exactly one cell', function () {
  346. /*
  347. +----------+
  348. | a |
  349. | +------+ |
  350. | | b | |
  351. | | | |
  352. | +------+ |
  353. +----------+
  354. */
  355. it('B is included in A, none of borders touch each other', function () {
  356. var aTopLeft = new Walkontable.CellCoords(0, 0);
  357. var aBottomRight = new Walkontable.CellCoords(5, 5);
  358. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  359. var bTopLeft = new Walkontable.CellCoords(1, 1);
  360. var bBottomRight = new Walkontable.CellCoords(1, 1);
  361. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  362. expect(a.includesRange(b)).toBe(true);
  363. });
  364. });
  365. });
  366. describe('expand', function () {
  367. it('should not change range if expander to a cell that fits within the range', function () {
  368. var from = new Walkontable.CellCoords(1, 1);
  369. var to = new Walkontable.CellCoords(3, 3);
  370. var range = new Walkontable.CellRange(from, from, to);
  371. var topLeft = range.getTopLeftCorner();
  372. var bottomRight = range.getBottomRightCorner();
  373. var expander = new Walkontable.CellCoords(3, 1);
  374. var res = range.expand(expander);
  375. expect(res).toBe(false);
  376. var topLeft2 = range.getTopLeftCorner();
  377. var bottomRight2 = range.getBottomRightCorner();
  378. expect(topLeft2).toEqual(topLeft);
  379. expect(bottomRight2).toEqual(bottomRight);
  380. });
  381. it('should change range if expander to a cell outside of the cell range', function () {
  382. var from = new Walkontable.CellCoords(1, 1);
  383. var to = new Walkontable.CellCoords(3, 3);
  384. var range = new Walkontable.CellRange(from, from, to);
  385. var topLeft = range.getTopLeftCorner();
  386. var expander = new Walkontable.CellCoords(4, 4);
  387. var res = range.expand(expander);
  388. expect(res).toBe(true);
  389. var topLeft2 = range.getTopLeftCorner();
  390. var bottomRight2 = range.getBottomRightCorner();
  391. expect(topLeft2).toEqual(topLeft);
  392. expect(bottomRight2).toEqual(expander);
  393. });
  394. it('should change range if expander to a cell outside of the cell range (inverted)', function () {
  395. var from = new Walkontable.CellCoords(1, 1);
  396. var to = new Walkontable.CellCoords(3, 3);
  397. var range = new Walkontable.CellRange(from, from, to);
  398. var topLeft = range.getTopLeftCorner();
  399. var expander = new Walkontable.CellCoords(4, 4);
  400. var res = range.expand(expander);
  401. expect(res).toBe(true);
  402. var topLeft2 = range.getTopLeftCorner();
  403. var bottomRight2 = range.getBottomRightCorner();
  404. expect(topLeft2).toEqual(topLeft);
  405. expect(bottomRight2).toEqual(expander);
  406. });
  407. it('should change range if expander to a cell outside of the cell range (bottom left)', function () {
  408. var from = new Walkontable.CellCoords(1, 1);
  409. var to = new Walkontable.CellCoords(3, 3);
  410. var range = new Walkontable.CellRange(from, from, to);
  411. var expander = new Walkontable.CellCoords(3, 0);
  412. var res = range.expand(expander);
  413. expect(res).toBe(true);
  414. var topLeft2 = range.getTopLeftCorner();
  415. var bottomRight2 = range.getBottomRightCorner();
  416. expect(topLeft2).toEqual(new Walkontable.CellCoords(1, 0));
  417. expect(bottomRight2).toEqual(new Walkontable.CellCoords(3, 3));
  418. });
  419. it('should change range if expander to a cell outside of the cell range (inverted top right)', function () {
  420. var from = new Walkontable.CellCoords(2, 0);
  421. var to = new Walkontable.CellCoords(1, 0);
  422. var range = new Walkontable.CellRange(from, from, to);
  423. var expander = new Walkontable.CellCoords(1, 1);
  424. var res = range.expand(expander);
  425. expect(res).toBe(true);
  426. var topLeft2 = range.getTopLeftCorner();
  427. var bottomRight2 = range.getBottomRightCorner();
  428. expect(topLeft2).toEqual(new Walkontable.CellCoords(1, 0));
  429. expect(bottomRight2).toEqual(new Walkontable.CellCoords(2, 1));
  430. });
  431. it('should change range if expander to a cell outside of the cell range (inverted bottom left)', function () {
  432. var from = new Walkontable.CellCoords(2, 1);
  433. var to = new Walkontable.CellCoords(1, 1);
  434. var range = new Walkontable.CellRange(from, from, to);
  435. var expander = new Walkontable.CellCoords(3, 0);
  436. var res = range.expand(expander);
  437. expect(res).toBe(true);
  438. var topLeft2 = range.getTopLeftCorner();
  439. var bottomRight2 = range.getBottomRightCorner();
  440. expect(topLeft2).toEqual(new Walkontable.CellCoords(1, 0));
  441. expect(bottomRight2).toEqual(new Walkontable.CellCoords(3, 1));
  442. });
  443. });
  444. describe('overlaps', function () {
  445. describe('positive', function () {
  446. /*
  447. +-------+
  448. | |
  449. | b |
  450. +-------+ |
  451. | +-|-----+
  452. | a |
  453. | |
  454. +-------+
  455. */
  456. it('overlapping from NE', function () {
  457. var aTopLeft = new Walkontable.CellCoords(3, 0);
  458. var aBottomRight = new Walkontable.CellCoords(8, 5);
  459. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  460. var bTopLeft = new Walkontable.CellCoords(0, 3);
  461. var bBottomRight = new Walkontable.CellCoords(5, 8);
  462. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  463. expect(a.overlaps(b)).toBe(true);
  464. });
  465. /*
  466. +---------+
  467. | +-------+
  468. | | | |
  469. | a | | b |
  470. | | | |
  471. | +-------+
  472. +---------+
  473. */
  474. it('overlapping from E', function () {
  475. var aTopLeft = new Walkontable.CellCoords(0, 0);
  476. var aBottomRight = new Walkontable.CellCoords(5, 5);
  477. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  478. var bTopLeft = new Walkontable.CellCoords(1, 3);
  479. var bBottomRight = new Walkontable.CellCoords(4, 6);
  480. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  481. expect(a.overlaps(b)).toBe(true);
  482. });
  483. /*
  484. +--------+
  485. | |
  486. | a |
  487. | +---------+
  488. | | | |
  489. +----|---+ |
  490. | b |
  491. | |
  492. +---------+
  493. */
  494. it('overlapping from SE', function () {
  495. var aTopLeft = new Walkontable.CellCoords(0, 0);
  496. var aBottomRight = new Walkontable.CellCoords(5, 5);
  497. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  498. var bTopLeft = new Walkontable.CellCoords(3, 3);
  499. var bBottomRight = new Walkontable.CellCoords(8, 8);
  500. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  501. expect(a.overlaps(b)).toBe(true);
  502. });
  503. /*
  504. +---------+
  505. | a |
  506. | +-----+ |
  507. +-|-----|-+
  508. | b |
  509. +-----+
  510. */
  511. it('overlapping from S', function () {
  512. var aTopLeft = new Walkontable.CellCoords(0, 0);
  513. var aBottomRight = new Walkontable.CellCoords(5, 5);
  514. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  515. var bTopLeft = new Walkontable.CellCoords(3, 1);
  516. var bBottomRight = new Walkontable.CellCoords(6, 4);
  517. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  518. expect(a.overlaps(b)).toBe(true);
  519. });
  520. /*
  521. +--------+
  522. | a |
  523. +--------+ |
  524. | | | |
  525. | +----|---+
  526. | b |
  527. +--------+
  528. */
  529. it('overlapping from SW', function () {
  530. var aTopLeft = new Walkontable.CellCoords(0, 3);
  531. var aBottomRight = new Walkontable.CellCoords(5, 8);
  532. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  533. var bTopLeft = new Walkontable.CellCoords(3, 0);
  534. var bBottomRight = new Walkontable.CellCoords(8, 5);
  535. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  536. expect(a.overlaps(b)).toBe(true);
  537. });
  538. /*
  539. +-------+
  540. +---|--+ |
  541. | | | |
  542. | b | | a |
  543. | | | |
  544. +---|--+ |
  545. +-------+
  546. */
  547. it('overlapping from S', function () {
  548. var aTopLeft = new Walkontable.CellCoords(0, 3);
  549. var aBottomRight = new Walkontable.CellCoords(5, 8);
  550. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  551. var bTopLeft = new Walkontable.CellCoords(1, 1);
  552. var bBottomRight = new Walkontable.CellCoords(4, 4);
  553. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  554. expect(a.overlaps(b)).toBe(true);
  555. });
  556. /*
  557. +------+
  558. | b |
  559. | +-------+
  560. | | | |
  561. +---|--+ a |
  562. | |
  563. +-------+
  564. */
  565. it('overlapping from NW', function () {
  566. var aTopLeft = new Walkontable.CellCoords(3, 3);
  567. var aBottomRight = new Walkontable.CellCoords(8, 8);
  568. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  569. var bTopLeft = new Walkontable.CellCoords(0, 0);
  570. var bBottomRight = new Walkontable.CellCoords(5, 5);
  571. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  572. expect(a.overlaps(b)).toBe(true);
  573. });
  574. /*
  575. +---------+
  576. | b |
  577. | +-----+ |
  578. +-|-----|-+
  579. | a |
  580. +-----+
  581. */
  582. it('overlapping from N', function () {
  583. var aTopLeft = new Walkontable.CellCoords(3, 1);
  584. var aBottomRight = new Walkontable.CellCoords(6, 4);
  585. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  586. var bTopLeft = new Walkontable.CellCoords(0, 0);
  587. var bBottomRight = new Walkontable.CellCoords(5, 5);
  588. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  589. expect(a.overlaps(b)).toBe(true);
  590. });
  591. /*
  592. +----------+
  593. | a |
  594. | +------+ |
  595. | | b | |
  596. | | | |
  597. | +------+ |
  598. +----------+
  599. */
  600. it('overlapping when includes', function () {
  601. var aTopLeft = new Walkontable.CellCoords(0, 0);
  602. var aBottomRight = new Walkontable.CellCoords(5, 5);
  603. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  604. var bTopLeft = new Walkontable.CellCoords(1, 1);
  605. var bBottomRight = new Walkontable.CellCoords(4, 4);
  606. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  607. expect(a.overlaps(b)).toBe(true);
  608. });
  609. /*
  610. +----------+
  611. | b |
  612. | +------+ |
  613. | | a | |
  614. | | | |
  615. | +------+ |
  616. +----------+
  617. */
  618. it('overlapping when included', function () {
  619. var aTopLeft = new Walkontable.CellCoords(1, 1);
  620. var aBottomRight = new Walkontable.CellCoords(4, 4);
  621. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  622. var bTopLeft = new Walkontable.CellCoords(0, 0);
  623. var bBottomRight = new Walkontable.CellCoords(5, 5);
  624. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  625. expect(a.overlaps(b)).toBe(true);
  626. });
  627. /*
  628. b-> +----------+
  629. | a |
  630. | |
  631. | |
  632. +----------+
  633. */
  634. it('overlapping when A includes B and B has only one cell, and this cell is A\'s top left corner', function () {
  635. var aTopLeft = new Walkontable.CellCoords(0, 0);
  636. var aBottomRight = new Walkontable.CellCoords(5, 5);
  637. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  638. var bTopLeft = new Walkontable.CellCoords(0, 0);
  639. var bBottomRight = new Walkontable.CellCoords(0, 0);
  640. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  641. expect(a.overlaps(b)).toBe(true);
  642. });
  643. /*
  644. +----------+ <- b
  645. | a |
  646. | |
  647. | |
  648. +----------+
  649. */
  650. it('overlapping when A includes B and B has only one cell, and this cell is A\'s top right corner', function () {
  651. var aTopLeft = new Walkontable.CellCoords(0, 0);
  652. var aBottomRight = new Walkontable.CellCoords(5, 5);
  653. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  654. var bTopLeft = new Walkontable.CellCoords(0, 5);
  655. var bBottomRight = new Walkontable.CellCoords(0, 5);
  656. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  657. expect(a.overlaps(b)).toBe(true);
  658. });
  659. /*
  660. +----------+
  661. | a |
  662. | |
  663. | |
  664. b -> +----------+
  665. */
  666. it('overlapping when A includes B and B has only one cell, and this cell is A\'s bottom left corner', function () {
  667. var aTopLeft = new Walkontable.CellCoords(0, 0);
  668. var aBottomRight = new Walkontable.CellCoords(5, 5);
  669. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  670. var bTopLeft = new Walkontable.CellCoords(5, 0);
  671. var bBottomRight = new Walkontable.CellCoords(5, 0);
  672. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  673. expect(a.overlaps(b)).toBe(true);
  674. });
  675. /*
  676. +----------+
  677. | a |
  678. | |
  679. | |
  680. +----------+ <- b
  681. */
  682. it('overlapping when A includes B and B has only one cell, and this cell is A\'s bottom right corner', function () {
  683. var aTopLeft = new Walkontable.CellCoords(0, 0);
  684. var aBottomRight = new Walkontable.CellCoords(5, 5);
  685. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  686. var bTopLeft = new Walkontable.CellCoords(5, 5);
  687. var bBottomRight = new Walkontable.CellCoords(5, 5);
  688. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  689. expect(a.overlaps(b)).toBe(true);
  690. });
  691. /*
  692. +----+
  693. |b |
  694. +----+----+
  695. | a|
  696. +----+
  697. */
  698. it('overlapping by touching from NE', function () {
  699. var aTopLeft = new Walkontable.CellCoords(5, 0);
  700. var aBottomRight = new Walkontable.CellCoords(10, 5);
  701. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  702. var bTopLeft = new Walkontable.CellCoords(0, 5);
  703. var bBottomRight = new Walkontable.CellCoords(5, 10);
  704. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  705. expect(a.overlaps(b)).toBe(true);
  706. });
  707. /*
  708. +----+----+
  709. | a| b|
  710. +----+----+
  711. */
  712. it('overlapping by touching from E', function () {
  713. var aTopLeft = new Walkontable.CellCoords(0, 0);
  714. var aBottomRight = new Walkontable.CellCoords(5, 5);
  715. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  716. var bTopLeft = new Walkontable.CellCoords(0, 5);
  717. var bBottomRight = new Walkontable.CellCoords(5, 10);
  718. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  719. expect(a.overlaps(b)).toBe(true);
  720. });
  721. /*
  722. +----+
  723. | a|
  724. +----+----+
  725. | b|
  726. +----+
  727. */
  728. it('overlapping by touching from SE', function () {
  729. var aTopLeft = new Walkontable.CellCoords(0, 0);
  730. var aBottomRight = new Walkontable.CellCoords(5, 5);
  731. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  732. var bTopLeft = new Walkontable.CellCoords(5, 5);
  733. var bBottomRight = new Walkontable.CellCoords(10, 10);
  734. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  735. expect(a.overlaps(b)).toBe(true);
  736. });
  737. /*
  738. +----+
  739. | a|
  740. +----+
  741. | b|
  742. +----+
  743. */
  744. it('overlapping by touching from S', function () {
  745. var aTopLeft = new Walkontable.CellCoords(0, 0);
  746. var aBottomRight = new Walkontable.CellCoords(5, 5);
  747. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  748. var bTopLeft = new Walkontable.CellCoords(5, 5);
  749. var bBottomRight = new Walkontable.CellCoords(10, 5);
  750. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  751. expect(a.overlaps(b)).toBe(true);
  752. });
  753. /*
  754. +----+
  755. | a|
  756. +----+----+
  757. | b|
  758. +----+
  759. */
  760. it('overlapping by touching from SW', function () {
  761. var aTopLeft = new Walkontable.CellCoords(0, 5);
  762. var aBottomRight = new Walkontable.CellCoords(5, 10);
  763. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  764. var bTopLeft = new Walkontable.CellCoords(5, 0);
  765. var bBottomRight = new Walkontable.CellCoords(10, 5);
  766. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  767. expect(a.overlaps(b)).toBe(true);
  768. });
  769. /*
  770. +----+----+
  771. | b| a|
  772. +----+----+
  773. */
  774. it('overlapping by touching from W', function () {
  775. var aTopLeft = new Walkontable.CellCoords(0, 5);
  776. var aBottomRight = new Walkontable.CellCoords(5, 10);
  777. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  778. var bTopLeft = new Walkontable.CellCoords(0, 0);
  779. var bBottomRight = new Walkontable.CellCoords(5, 5);
  780. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  781. expect(a.overlaps(b)).toBe(true);
  782. });
  783. /*
  784. +----+
  785. | b|
  786. +----+----+
  787. | a|
  788. +----+
  789. */
  790. it('overlapping by touching from NW', function () {
  791. var aTopLeft = new Walkontable.CellCoords(5, 5);
  792. var aBottomRight = new Walkontable.CellCoords(10, 10);
  793. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  794. var bTopLeft = new Walkontable.CellCoords(0, 0);
  795. var bBottomRight = new Walkontable.CellCoords(5, 5);
  796. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  797. expect(a.overlaps(b)).toBe(true);
  798. });
  799. /*
  800. +----+
  801. | b|
  802. +----+
  803. | a|
  804. +----+
  805. */
  806. it('overlapping by touching from E', function () {
  807. var aTopLeft = new Walkontable.CellCoords(5, 0);
  808. var aBottomRight = new Walkontable.CellCoords(10, 5);
  809. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  810. var bTopLeft = new Walkontable.CellCoords(0, 0);
  811. var bBottomRight = new Walkontable.CellCoords(5, 5);
  812. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  813. expect(a.overlaps(b)).toBe(true);
  814. });
  815. });
  816. describe('negative', function () {
  817. /*
  818. +---+
  819. | b|
  820. +---+
  821. +------+
  822. | |
  823. | a |
  824. | |
  825. +------+
  826. */
  827. it('not overlapping from NE', function () {
  828. var aTopLeft = new Walkontable.CellCoords(6, 0);
  829. var aBottomRight = new Walkontable.CellCoords(11, 5);
  830. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  831. var bTopLeft = new Walkontable.CellCoords(0, 3);
  832. var bBottomRight = new Walkontable.CellCoords(5, 8);
  833. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  834. expect(a.overlaps(b)).toBe(false);
  835. });
  836. /*
  837. +------+
  838. | | +--+
  839. | a | | b|
  840. | | +--+
  841. +------+
  842. */
  843. it('not overlapping from E', function () {
  844. var aTopLeft = new Walkontable.CellCoords(0, 0);
  845. var aBottomRight = new Walkontable.CellCoords(5, 5);
  846. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  847. var bTopLeft = new Walkontable.CellCoords(1, 6);
  848. var bBottomRight = new Walkontable.CellCoords(4, 9);
  849. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  850. expect(a.overlaps(b)).toBe(false);
  851. });
  852. /*
  853. +----+
  854. |a |
  855. | | +----+
  856. +----+ |b |
  857. | |
  858. +----+
  859. */
  860. it('not overlapping from SE', function () {
  861. var aTopLeft = new Walkontable.CellCoords(0, 0);
  862. var aBottomRight = new Walkontable.CellCoords(5, 5);
  863. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  864. var bTopLeft = new Walkontable.CellCoords(1, 6);
  865. var bBottomRight = new Walkontable.CellCoords(4, 9);
  866. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  867. expect(a.overlaps(b)).toBe(false);
  868. });
  869. /*
  870. +----+
  871. |a |
  872. | |
  873. +----+
  874. +----+
  875. |b |
  876. | |
  877. +----+
  878. */
  879. it('not overlapping from S', function () {
  880. var aTopLeft = new Walkontable.CellCoords(0, 0);
  881. var aBottomRight = new Walkontable.CellCoords(5, 5);
  882. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  883. var bTopLeft = new Walkontable.CellCoords(6, 0);
  884. var bBottomRight = new Walkontable.CellCoords(11, 5);
  885. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  886. expect(a.overlaps(b)).toBe(false);
  887. });
  888. /*
  889. +----+
  890. |a |
  891. | |
  892. +----+
  893. +----+
  894. |b |
  895. | |
  896. +----+
  897. */
  898. it('not overlapping from SW', function () {
  899. var aTopLeft = new Walkontable.CellCoords(0, 3);
  900. var aBottomRight = new Walkontable.CellCoords(5, 8);
  901. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  902. var bTopLeft = new Walkontable.CellCoords(6, 0);
  903. var bBottomRight = new Walkontable.CellCoords(11, 5);
  904. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  905. expect(a.overlaps(b)).toBe(false);
  906. });
  907. /*
  908. +------+
  909. +--+ | |
  910. | b| | a |
  911. +--+ | |
  912. +------+
  913. */
  914. it('not overlapping from W', function () {
  915. var aTopLeft = new Walkontable.CellCoords(0, 6);
  916. var aBottomRight = new Walkontable.CellCoords(5, 11);
  917. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  918. var bTopLeft = new Walkontable.CellCoords(3, 0);
  919. var bBottomRight = new Walkontable.CellCoords(6, 3);
  920. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  921. expect(a.overlaps(b)).toBe(false);
  922. });
  923. /*
  924. +----+
  925. |b |
  926. | | +----+
  927. +----+ | a |
  928. | |
  929. +----+
  930. */
  931. it('not overlapping from NW', function () {
  932. var aTopLeft = new Walkontable.CellCoords(0, 6);
  933. var aBottomRight = new Walkontable.CellCoords(3, 11);
  934. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  935. var bTopLeft = new Walkontable.CellCoords(0, 0);
  936. var bBottomRight = new Walkontable.CellCoords(5, 5);
  937. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  938. expect(a.overlaps(b)).toBe(false);
  939. });
  940. /*
  941. +----+
  942. |b |
  943. +----+
  944. +----+
  945. | a|
  946. +----+
  947. */
  948. it('not overlapping from N', function () {
  949. var aTopLeft = new Walkontable.CellCoords(6, 0);
  950. var aBottomRight = new Walkontable.CellCoords(11, 5);
  951. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  952. var bTopLeft = new Walkontable.CellCoords(0, 0);
  953. var bBottomRight = new Walkontable.CellCoords(5, 5);
  954. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  955. expect(a.overlaps(b)).toBe(false);
  956. });
  957. });
  958. });
  959. describe('expand by range', function () {
  960. it('should not expand range A with range B if A includes B', function () {
  961. var aTopLeft = new Walkontable.CellCoords(0, 0);
  962. var aBottomRight = new Walkontable.CellCoords(5, 5);
  963. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  964. var bTopLeft = new Walkontable.CellCoords(2, 2);
  965. var bBottomRight = new Walkontable.CellCoords(4, 4);
  966. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  967. expect(a.expandByRange(b)).toBe(false);
  968. expect(a.getTopLeftCorner().row).toEqual(0);
  969. expect(a.getTopLeftCorner().col).toEqual(0);
  970. expect(a.getBottomRightCorner().row).toEqual(5);
  971. expect(a.getBottomRightCorner().col).toEqual(5);
  972. });
  973. it('should not expand range A with range B if A and B don\'t overlap', function () {
  974. var aTopLeft = new Walkontable.CellCoords(0, 0);
  975. var aBottomRight = new Walkontable.CellCoords(5, 5);
  976. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  977. var bTopLeft = new Walkontable.CellCoords(10, 10);
  978. var bBottomRight = new Walkontable.CellCoords(15, 15);
  979. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  980. expect(a.expandByRange(b)).toBe(false);
  981. expect(a.getTopLeftCorner().row).toEqual(0);
  982. expect(a.getTopLeftCorner().col).toEqual(0);
  983. expect(a.getBottomRightCorner().row).toEqual(5);
  984. expect(a.getBottomRightCorner().col).toEqual(5);
  985. });
  986. it('should not expand range A with range B', function () {
  987. var aTopLeft = new Walkontable.CellCoords(0, 0);
  988. var aBottomRight = new Walkontable.CellCoords(5, 5);
  989. var a = new Walkontable.CellRange(aTopLeft, aTopLeft, aBottomRight);
  990. var bTopLeft = new Walkontable.CellCoords(2, 2);
  991. var bBottomRight = new Walkontable.CellCoords(7, 7);
  992. var b = new Walkontable.CellRange(bTopLeft, bTopLeft, bBottomRight);
  993. expect(a.expandByRange(b)).toBe(true);
  994. expect(a.getTopLeftCorner().row).toEqual(0);
  995. expect(a.getTopLeftCorner().col).toEqual(0);
  996. expect(a.getBottomRightCorner().row).toEqual(7);
  997. expect(a.getBottomRightCorner().col).toEqual(7);
  998. });
  999. });
  1000. describe('forAll', function () {
  1001. it('callback should be called for all cells in the range', function () {
  1002. var from = new Walkontable.CellCoords(1, 1);
  1003. var to = new Walkontable.CellCoords(3, 3);
  1004. var range = new Walkontable.CellRange(from, from, to);
  1005. var forAllCallback = jasmine.createSpy('beforeColumnSortHandler');
  1006. range.forAll(forAllCallback);
  1007. expect(forAllCallback.calls.count()).toBe(9);
  1008. });
  1009. it('callback should be called with row, column parameters', function () {
  1010. var from = new Walkontable.CellCoords(1, 1);
  1011. var to = new Walkontable.CellCoords(2, 2);
  1012. var range = new Walkontable.CellRange(from, from, to);
  1013. var rows = [];
  1014. var cols = [];
  1015. range.forAll(function (row, col) {
  1016. rows.push(row);
  1017. cols.push(col);
  1018. });
  1019. expect(rows).toEqual([1, 1, 2, 2]);
  1020. expect(cols).toEqual([1, 2, 1, 2]);
  1021. });
  1022. it('iteration should be interrupted when callback returns false', function () {
  1023. var from = new Walkontable.CellCoords(1, 1);
  1024. var to = new Walkontable.CellCoords(2, 2);
  1025. var range = new Walkontable.CellRange(from, from, to);
  1026. var callCount = 0;
  1027. range.forAll(function (row, col) {
  1028. callCount++;
  1029. if (callCount == 2) {
  1030. return false;
  1031. }
  1032. });
  1033. expect(callCount).toBe(2);
  1034. });
  1035. });
  1036. describe('change direction', function () {
  1037. it('should properly change direction on NW-SE', function () {
  1038. var from = new Walkontable.CellCoords(2, 1);
  1039. var to = new Walkontable.CellCoords(1, 2);
  1040. var range = new Walkontable.CellRange(from, from, to);
  1041. expect(range.getDirection()).toBe('SW-NE');
  1042. range.setDirection('NW-SE');
  1043. expect(range.getDirection()).toBe('NW-SE');
  1044. expect(range.from.row).toBe(1);
  1045. expect(range.from.col).toBe(1);
  1046. expect(range.to.row).toBe(2);
  1047. expect(range.to.col).toBe(2);
  1048. });
  1049. it('should properly change direction on NE-SW', function () {
  1050. var from = new Walkontable.CellCoords(2, 1);
  1051. var to = new Walkontable.CellCoords(1, 2);
  1052. var range = new Walkontable.CellRange(from, from, to);
  1053. expect(range.getDirection()).toBe('SW-NE');
  1054. range.setDirection('NE-SW');
  1055. expect(range.getDirection()).toBe('NE-SW');
  1056. expect(range.from.row).toBe(1);
  1057. expect(range.from.col).toBe(2);
  1058. expect(range.to.row).toBe(2);
  1059. expect(range.to.col).toBe(1);
  1060. });
  1061. it('should properly change direction on SE-NW', function () {
  1062. var from = new Walkontable.CellCoords(1, 1);
  1063. var to = new Walkontable.CellCoords(2, 2);
  1064. var range = new Walkontable.CellRange(from, from, to);
  1065. expect(range.getDirection()).toBe('NW-SE');
  1066. range.setDirection('SE-NW');
  1067. expect(range.getDirection()).toBe('SE-NW');
  1068. expect(range.from.row).toBe(2);
  1069. expect(range.from.col).toBe(2);
  1070. expect(range.to.row).toBe(1);
  1071. expect(range.to.col).toBe(1);
  1072. });
  1073. it('should properly change direction on SW-NE', function () {
  1074. var from = new Walkontable.CellCoords(1, 1);
  1075. var to = new Walkontable.CellCoords(2, 2);
  1076. var range = new Walkontable.CellRange(from, from, to);
  1077. expect(range.getDirection()).toBe('NW-SE');
  1078. range.setDirection('SW-NE');
  1079. expect(range.getDirection()).toBe('SW-NE');
  1080. expect(range.from.row).toBe(2);
  1081. expect(range.from.col).toBe(1);
  1082. expect(range.to.row).toBe(1);
  1083. expect(range.to.col).toBe(2);
  1084. });
  1085. });
  1086. });