string_utils_test.js 6.7 KB

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