array_utils_test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var arrayUtils = require('utils/array_utils');
  19. describe('array_utils', function () {
  20. describe('#uniqObjectsbyId', function () {
  21. var arr = [
  22. {
  23. n: 0,
  24. v: 'v0'
  25. },
  26. {
  27. n: 0,
  28. v: 'v01'
  29. },
  30. {
  31. n: 1,
  32. v: 'v1'
  33. },
  34. {
  35. n: '1',
  36. v: 'v11'
  37. },
  38. {
  39. n: 2,
  40. v: 'v2'
  41. }
  42. ],
  43. result = [
  44. {
  45. n: 0,
  46. v: 'v0'
  47. },
  48. {
  49. n: 1,
  50. v: 'v1'
  51. },
  52. {
  53. n: '1',
  54. v: 'v11'
  55. },
  56. {
  57. n: 2,
  58. v: 'v2'
  59. }
  60. ];
  61. it('should return one element for one id', function () {
  62. expect(arrayUtils.uniqObjectsbyId(arr, 'n').toArray()).to.eql(result);
  63. });
  64. });
  65. describe('#intersect', function () {
  66. var cases = [
  67. {
  68. arr1: [Infinity, 0, 1, 2, {a: 1}, {b: 2}, null, undefined],
  69. arr2: ['undefined', null, {b: '2'}, {a: 1}, 2.0, '1', 0, Infinity],
  70. result: [null, 2, 0, Infinity],
  71. title: 'arrays of the same length have common items'
  72. },
  73. {
  74. arr1: [true, false, [0, 1], [2, 3], [4, 5], [6], null, undefined],
  75. arr2: [undefined, 'null', 6, [4, 5], ['2', '3'], [String(0), String(1)], '0,1', false, 'true'],
  76. result: [false, undefined],
  77. title: 'arrays of different length have common items'
  78. },
  79. {
  80. arr1: ['1', function () {}, NaN],
  81. arr2: ['function () {}', Number('1'), NaN],
  82. result: [],
  83. title: 'arrays have no common items'
  84. },
  85. {
  86. arr1: [[0], undefined, null],
  87. arr2: [],
  88. result: [],
  89. title: 'one of arrays is empty'
  90. },
  91. {
  92. arr1: [],
  93. arr2: [],
  94. result: [],
  95. title: 'both arrays are empty'
  96. }
  97. ];
  98. cases.forEach(function (item) {
  99. describe(item.title, function () {
  100. it('arrays intersection', function () {
  101. expect(arrayUtils.intersect(item.arr1, item.arr2)).to.eql(item.result);
  102. });
  103. it('commutativity', function () {
  104. expect(arrayUtils.intersect(item.arr1, item.arr2).sort()).to.eql(arrayUtils.intersect(item.arr2, item.arr1).sort());
  105. });
  106. });
  107. });
  108. });
  109. describe('#sortByIdAsVersion', function () {
  110. Em.A([
  111. {
  112. c: [{id: '1.2.4'}, {id: '1.2.5'}, {id: '1.2.3'}],
  113. m: 'Items without letters and with same length',
  114. e: ['1.2.3', '1.2.4', '1.2.5']
  115. },
  116. {
  117. c: [{id: 'HDP-1.2.4'}, {id: 'HDP-1.2.5'}, {id: 'HDP-1.2.3'}],
  118. m: 'Items with letters and with same length',
  119. e: ['HDP-1.2.3', 'HDP-1.2.4', 'HDP-1.2.5']
  120. },
  121. {
  122. c: [{id: 'HDP-1.2.4.2'}, {id: 'HDP-1.2.4.1'}, {id: 'HDP-1.2.3'}],
  123. m: 'Items with letters and with custom length',
  124. e: ['HDP-1.2.3', 'HDP-1.2.4.1', 'HDP-1.2.4.2']
  125. },
  126. {
  127. c: [{id: 'HDP-1.2.4.2.3'}, {id: 'HDP-1.2.4.11.3'}, {id: 'HDP-1.2.3'}],
  128. m: 'Items with letters and with double digits',
  129. e: ['HDP-1.2.3', 'HDP-1.2.4.2.3', 'HDP-1.2.4.11.3']
  130. },
  131. {
  132. c: [{id: 'HDP-1.2.3.2'}, {id: 'HDP-1.2.4'}, {id: 'HDP-1.2.3'}],
  133. m: 'Items with letters and with custom length (2)',
  134. e: ['HDP-1.2.3', 'HDP-1.2.3.2', 'HDP-1.2.4']
  135. },
  136. {
  137. c: [{id: 'HDP-1.2.3'}, {id: 'HDP-1.2.4'}, {id: 'HDP-1.2.3'}],
  138. m: 'Items with letters and equal ids',
  139. e: ['HDP-1.2.3', 'HDP-1.2.3', 'HDP-1.2.4']
  140. }
  141. ]).forEach(function (test) {
  142. it(test.m, function () {
  143. expect(test.c.sort(arrayUtils.sortByIdAsVersion).mapProperty('id')).to.be.eql(test.e);
  144. });
  145. });
  146. });
  147. });