string_utils_test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 stringUtils = require('utils/string_utils');
  19. require('utils/helper');
  20. describe('stringUtils', function () {
  21. describe('#underScoreToCamelCase', function () {
  22. var tests = [
  23. {m:'a_b_c to aBC',i:'a_b_c',e:'aBC'},
  24. {m:'a_bc to aBc',i:'a_bc',e:'aBc'},
  25. {m:'ab_c to abC',i:'ab_c',e:'abC'},
  26. {m:'_b_c to BC',i:'_b_c',e:'BC'}
  27. ];
  28. tests.forEach(function(test) {
  29. it(test.m + ' ', function () {
  30. expect(stringUtils.underScoreToCamelCase(test.i)).to.equal(test.e);
  31. });
  32. });
  33. });
  34. describe('#pad', function () {
  35. var tests = [
  36. {m: '"name" to " name"', i: 'name', l: 8, a: 1, f: ' ', e: ' name'},
  37. {m: '"name" to "name "', i: 'name', l: 8, a: 2, f: ' ', e: 'name '},
  38. {m: '"name" to " name "', i: 'name', l: 8, a: 3, f: ' ', e: ' name '},
  39. {m: '"name" to "name "', i: 'name', l: 8, a: 0, f: ' ', e: 'name '},
  40. {m: '"name" to "name "', i: 'name', l: 8, a:-1, f: ' ', e: 'name '},
  41. {m: '"name" to "name"', i: 'name', l: 4, a: 1, f: ' ', e: 'name'},
  42. {m: '"name" to "||||||||name"', i: 'name', l: 8, a:1, f: '||', e: '||||||||name'},
  43. {m: '"name" to "||||name||||"', i: 'name', l: 8, a:3, f: '||', e: '||||name||||'},
  44. {m: '"name" to "name||||||||"', i: 'name', l: 8, a:2, f: '||', e: 'name||||||||'},
  45. {m: '"name" to "name" `str` param passed only', i: 'name', e: 'name'}
  46. ];
  47. tests.forEach(function(test) {
  48. it(test.m + ' ', function () {
  49. expect(stringUtils.pad(test.i, test.l, test.f, test.a)).to.equal(test.e);
  50. });
  51. });
  52. });
  53. describe('#compareVersions', function () {
  54. var tests = [
  55. {m: '1.2 equal to 1.2', v1:'1.2', v2:'1.2', e: 0},
  56. {m: '1.2 lower than 1.3', v1:'1.2', v2:'1.3', e: -1},
  57. {m: '1.3 higher than 1.2', v1:'1.3', v2:'1.2', e: 1},
  58. {m: '1.2.1 higher than 1.2', v1:'1.2.1', v2:'1.2', e: 1},
  59. {m: '11.2 higher than 2.2', v1:'11.2', v2:'2.2', e: 1},
  60. {m: '0.9 higher than 0.8', v1:'0.9', v2:'0.8', e: 1},
  61. {m: '1.1-2 equal to 1.1-2 ', v1:'1.1-2', v2:'1.1-2', e: 0},
  62. {m: '1.1-2 higher than 1.1-1', v1:'1.1-2', v2:'1.1-1', e: 1},
  63. {m: '1.1-4 lower than 1.1-46', v1:'1.1-4', v2:'1.1-46', e: -1},
  64. {m: 'return false if no string passed', v1: '0.9', e: -1}
  65. ];
  66. tests.forEach(function(test) {
  67. it(test.m + ' ', function () {
  68. expect(stringUtils.compareVersions(test.v1, test.v2)).to.equal(test.e);
  69. });
  70. });
  71. });
  72. describe('#isSingleLine', function () {
  73. var tests = [
  74. {m: 'is single line text', t: 'a b', e: true},
  75. {m: 'is single line text', t: 'a b\n', e: true},
  76. {m: 'is single line text', t: '\na b', e: true},
  77. {m: 'is not single line text', t: 'a\nb', e: false}
  78. ];
  79. tests.forEach(function(test) {
  80. it(test.t + ' ' + test.m + ' ', function () {
  81. expect(stringUtils.isSingleLine(test.t)).to.equal(test.e);
  82. });
  83. });
  84. });
  85. describe('#arrayToCSV', function() {
  86. var test = [{a: 1, b:2, c:3}, {a: 1, b:2, c:3}, {a: 1, b:2, c:3}];
  87. it('array of object to csv-string', function () {
  88. expect(stringUtils.arrayToCSV(test)).to.equal("1,2,3\n1,2,3\n1,2,3\n");
  89. });
  90. });
  91. describe('#getFileFromPath', function() {
  92. var tests = [
  93. {t: undefined, e: ''},
  94. {t: {}, e: ''},
  95. {t: [], e: ''},
  96. {t: '', e: ''},
  97. {t: function(){}, e: ''},
  98. {t: '/path/to/file.ext', e: 'file.ext'},
  99. {t: 'file.ext', e: 'file.ext'},
  100. {t: 'file', e: 'file'},
  101. {t: '/path/to/file', e: 'file'}
  102. ];
  103. tests.forEach(function(test) {
  104. it('Check ' + typeof test.t, function () {
  105. expect(stringUtils.getFileFromPath(test.t)).to.equal(test.e);
  106. });
  107. });
  108. });
  109. describe('#getPath', function() {
  110. var tests = [
  111. {t: undefined, e: ''},
  112. {t: {}, e: ''},
  113. {t: [], e: ''},
  114. {t: '', e: ''},
  115. {t: function(){}, e: ''},
  116. {t: '/path/to/filename', e: '/path/to'},
  117. {t: '/path/to/', e: '/path/to'},
  118. {t: '/filename', e: '/'},
  119. {t: 'filename', e: ''},
  120. {t: '/path/', e: '/path'},
  121. {t: 'filename/', e: ''}
  122. ];
  123. tests.forEach(function(test) {
  124. it('Check ' + typeof test.t, function () {
  125. expect(stringUtils.getPath(test.t)).to.equal(test.e);
  126. });
  127. });
  128. });
  129. describe('#getCamelCase', function () {
  130. var tests = [
  131. {i:'a',e:'A'},
  132. {i:'aB',e:'Ab'},
  133. {i:'a b',e:'A B'},
  134. {i:'a.b',e:'A.B'},
  135. {i:'a,b',e:'A,B'},
  136. {i:'a;b',e:'A;B'},
  137. {i:'a. b',e:'A. B'},
  138. {i:'a b',e:'A B'},
  139. {i:'aaa. bbb',e:'Aaa. Bbb'},
  140. {i:'aAA. bBB',e:'Aaa. Bbb'},
  141. {i:'STARTING',e:'Starting'},
  142. {i:'starting',e:'Starting'},
  143. {i:'starting,ending',e:'Starting,Ending'},
  144. {i: null, e: null},
  145. {i: undefined, e: undefined}
  146. ];
  147. tests.forEach(function(test) {
  148. it(test.i + ' to ' + test.e + ' ', function () {
  149. expect(stringUtils.getCamelCase(test.i)).to.equal(test.e);
  150. });
  151. });
  152. });
  153. describe('#findIn', function () {
  154. var tests = [
  155. {
  156. obj: {
  157. a: '1',
  158. b: '2'
  159. },
  160. key: 'a',
  161. index: 0,
  162. e: '1'
  163. }, {
  164. obj: {
  165. a: '1',
  166. b: '2'
  167. },
  168. key: 'a',
  169. index: 1,
  170. e: null
  171. }, {
  172. obj: {
  173. a: '1',
  174. b: '2',
  175. c: {
  176. a: '11',
  177. aa: '12'
  178. }
  179. },
  180. key: 'a',
  181. index: 1,
  182. e: '11'
  183. }, {
  184. obj: {
  185. a: '1',
  186. b: '2',
  187. c: {
  188. a: '11',
  189. aa: {
  190. a: '22'
  191. }
  192. }
  193. },
  194. key: 'a',
  195. index: 2,
  196. e: '22'
  197. }, {
  198. obj: {
  199. a: '1',
  200. b: '2',
  201. c: {
  202. a: '11',
  203. aa: {
  204. a: '22'
  205. }
  206. }
  207. },
  208. key: 'a',
  209. index: 0,
  210. e: '1'
  211. }, {
  212. obj: {
  213. a: '1',
  214. b: '2',
  215. c: {
  216. a: '11',
  217. aa: {
  218. a: '22'
  219. }
  220. }
  221. },
  222. key: 'g',
  223. index: 0,
  224. e: null
  225. }
  226. ];
  227. tests.forEach(function(test) {
  228. it(test.key + ' @ ' + test.index + ' = ' + test.e, function () {
  229. expect(test.key.findIn(test.obj, test.index)).to.equal(test.e);
  230. });
  231. });
  232. });
  233. describe("#htmlEntities()", function() {
  234. var tests = [
  235. {t: undefined, e: ''},
  236. {t: '', e: ''},
  237. {t: 'abc', e: 'abc'},
  238. {t: 'abc<script>abc', e: 'abc&lt;script&gt;abc'}
  239. ];
  240. tests.forEach(function(test) {
  241. it('Check ' + typeof test.t, function () {
  242. expect(stringUtils.htmlEntities(test.t)).to.equal(test.e);
  243. });
  244. });
  245. });
  246. });