string_utils_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 string_utils = require('utils/string_utils');
  19. require('utils/helper');
  20. describe('string_utils', 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(string_utils.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(string_utils.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: 'return false if no string passed', v1: '0.9', e: false}
  62. ];
  63. tests.forEach(function(test) {
  64. it(test.m + ' ', function () {
  65. expect(string_utils.compareVersions(test.v1, test.v2)).to.equal(test.e);
  66. });
  67. });
  68. });
  69. describe('#isSingleLine', function () {
  70. var tests = [
  71. {m: 'is single line text', t: 'a b', e: true},
  72. {m: 'is single line text', t: 'a b\n', e: true},
  73. {m: 'is single line text', t: '\na b', e: true},
  74. {m: 'is not single line text', t: 'a\nb', e: false}
  75. ];
  76. tests.forEach(function(test) {
  77. it(test.t + ' ' + test.m + ' ', function () {
  78. expect(string_utils.isSingleLine(test.t)).to.equal(test.e);
  79. });
  80. });
  81. });
  82. describe('#arrayToCSV', function() {
  83. var test = [{a: 1, b:2, c:3}, {a: 1, b:2, c:3}, {a: 1, b:2, c:3}];
  84. it('array of object to csv-string', function () {
  85. expect(string_utils.arrayToCSV(test)).to.equal("1,2,3\n1,2,3\n1,2,3\n");
  86. });
  87. });
  88. describe('#getFileFromPath', function() {
  89. var tests = [
  90. {t: undefined, e: ''},
  91. {t: {}, e: ''},
  92. {t: [], e: ''},
  93. {t: '', e: ''},
  94. {t: function(){}, e: ''},
  95. {t: '/path/to/file.ext', e: 'file.ext'},
  96. {t: 'file.ext', e: 'file.ext'},
  97. {t: 'file', e: 'file'},
  98. {t: '/path/to/file', e: 'file'}
  99. ];
  100. tests.forEach(function(test) {
  101. it('Check ' + typeof test.t, function () {
  102. expect(string_utils.getFileFromPath(test.t)).to.equal(test.e);
  103. });
  104. });
  105. });
  106. describe('#getPath', function() {
  107. var tests = [
  108. {t: undefined, e: ''},
  109. {t: {}, e: ''},
  110. {t: [], e: ''},
  111. {t: '', e: ''},
  112. {t: function(){}, e: ''},
  113. {t: '/path/to/filename', e: '/path/to'},
  114. {t: '/path/to/', e: '/path/to'},
  115. {t: '/filename', e: '/'},
  116. {t: 'filename', e: ''},
  117. {t: '/path/', e: '/path'},
  118. {t: 'filename/', e: ''}
  119. ];
  120. tests.forEach(function(test) {
  121. it('Check ' + typeof test.t, function () {
  122. expect(string_utils.getPath(test.t)).to.equal(test.e);
  123. });
  124. });
  125. });
  126. describe('#getCamelCase', function () {
  127. var tests = [
  128. {i:'a',e:'A'},
  129. {i:'aB',e:'Ab'},
  130. {i:'a b',e:'A B'},
  131. {i:'a.b',e:'A.B'},
  132. {i:'a,b',e:'A,B'},
  133. {i:'a;b',e:'A;B'},
  134. {i:'a. b',e:'A. B'},
  135. {i:'a b',e:'A B'},
  136. {i:'aaa. bbb',e:'Aaa. Bbb'},
  137. {i:'aAA. bBB',e:'Aaa. Bbb'},
  138. {i:'STARTING',e:'Starting'},
  139. {i:'starting',e:'Starting'},
  140. {i:'starting,ending',e:'Starting,Ending'},
  141. {i: null, e: null},
  142. {i: undefined, e: undefined}
  143. ];
  144. tests.forEach(function(test) {
  145. it(test.i + ' to ' + test.e + ' ', function () {
  146. expect(string_utils.getCamelCase(test.i)).to.equal(test.e);
  147. });
  148. });
  149. });
  150. describe('#findIn', function () {
  151. var tests = [
  152. {
  153. obj: {
  154. a: '1',
  155. b: '2'
  156. },
  157. key: 'a',
  158. index: 0,
  159. e: '1'
  160. }, {
  161. obj: {
  162. a: '1',
  163. b: '2'
  164. },
  165. key: 'a',
  166. index: 1,
  167. e: null
  168. }, {
  169. obj: {
  170. a: '1',
  171. b: '2',
  172. c: {
  173. a: '11',
  174. aa: '12'
  175. }
  176. },
  177. key: 'a',
  178. index: 1,
  179. e: '11'
  180. }, {
  181. obj: {
  182. a: '1',
  183. b: '2',
  184. c: {
  185. a: '11',
  186. aa: {
  187. a: '22'
  188. }
  189. }
  190. },
  191. key: 'a',
  192. index: 2,
  193. e: '22'
  194. }, {
  195. obj: {
  196. a: '1',
  197. b: '2',
  198. c: {
  199. a: '11',
  200. aa: {
  201. a: '22'
  202. }
  203. }
  204. },
  205. key: 'a',
  206. index: 0,
  207. e: '1'
  208. }, {
  209. obj: {
  210. a: '1',
  211. b: '2',
  212. c: {
  213. a: '11',
  214. aa: {
  215. a: '22'
  216. }
  217. }
  218. },
  219. key: 'g',
  220. index: 0,
  221. e: null
  222. }
  223. ];
  224. tests.forEach(function(test) {
  225. it(test.key + ' @ ' + test.index + ' = ' + test.e, function () {
  226. expect(test.key.findIn(test.obj, test.index)).to.equal(test.e);
  227. });
  228. });
  229. });
  230. });