string_utils_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. describe('string_utils', function () {
  20. describe('#underScoreToCamelCase', function () {
  21. var tests = [
  22. {m:'a_b_c to aBC',i:'a_b_c',e:'aBC'},
  23. {m:'a_bc to aBc',i:'a_bc',e:'aBc'},
  24. {m:'ab_c to abC',i:'ab_c',e:'abC'},
  25. {m:'_b_c to BC',i:'_b_c',e:'BC'}
  26. ];
  27. tests.forEach(function(test) {
  28. it(test.m + ' ', function () {
  29. expect(string_utils.underScoreToCamelCase(test.i)).to.equal(test.e);
  30. });
  31. });
  32. });
  33. describe('#pad', function () {
  34. var tests = [
  35. {m: '"name" to " name"', i: 'name', l: 8, a: 1, f: ' ', e: ' name'},
  36. {m: '"name" to "name "', i: 'name', l: 8, a: 2, f: ' ', e: 'name '},
  37. {m: '"name" to " name "', i: 'name', l: 8, a: 3, f: ' ', e: ' name '},
  38. {m: '"name" to "name "', i: 'name', l: 8, a: 0, f: ' ', e: 'name '},
  39. {m: '"name" to "name "', i: 'name', l: 8, a:-1, f: ' ', e: 'name '},
  40. {m: '"name" to "name"', i: 'name', l: 4, a: 1, f: ' ', e: 'name'},
  41. {m: '"name" to "||||||||name"', i: 'name', l: 8, a:1, f: '||', e: '||||||||name'},
  42. {m: '"name" to "||||name||||"', i: 'name', l: 8, a:3, f: '||', e: '||||name||||'},
  43. {m: '"name" to "name||||||||"', i: 'name', l: 8, a:2, f: '||', e: 'name||||||||'}
  44. ];
  45. tests.forEach(function(test) {
  46. it(test.m + ' ', function () {
  47. expect(string_utils.pad(test.i, test.l, test.f, test.a)).to.equal(test.e);
  48. });
  49. });
  50. });
  51. describe('#compareVersions', function () {
  52. var tests = [
  53. {m: '1.2 equal to 1.2', v1:'1.2', v2:'1.2', e: 0},
  54. {m: '1.2 lower than 1.3', v1:'1.2', v2:'1.3', e: -1},
  55. {m: '1.3 higher than 1.2', v1:'1.3', v2:'1.2', e: 1},
  56. {m: '1.2.1 higher than 1.2', v1:'1.2.1', v2:'1.2', e: 1},
  57. {m: '11.2 higher than 2.2', v1:'11.2', v2:'2.2', e: 1},
  58. {m: '0.9 higher than 0.8', v1:'0.9', v2:'0.8', e: 1}
  59. ];
  60. tests.forEach(function(test) {
  61. it(test.m + ' ', function () {
  62. expect(string_utils.compareVersions(test.v1, test.v2)).to.equal(test.e);
  63. });
  64. });
  65. });
  66. describe('#isSingleLine', function () {
  67. var tests = [
  68. {m: 'is single line text', t: 'a b', e: true},
  69. {m: 'is single line text', t: 'a b\n', e: true},
  70. {m: 'is single line text', t: '\na b', e: true},
  71. {m: 'is not single line text', t: 'a\nb', e: false}
  72. ];
  73. tests.forEach(function(test) {
  74. it(test.t + ' ' + test.m + ' ', function () {
  75. expect(string_utils.isSingleLine(test.t)).to.equal(test.e);
  76. });
  77. });
  78. });
  79. describe('#arrayToCSV', function() {
  80. var test = [{a: 1, b:2, c:3}, {a: 1, b:2, c:3}, {a: 1, b:2, c:3}];
  81. it('array of object to csv-string', function () {
  82. expect(string_utils.arrayToCSV(test)).to.equal("1,2,3\n1,2,3\n1,2,3\n");
  83. });
  84. });
  85. describe('#getFileFromPath', function() {
  86. var tests = [
  87. {t: undefined, e: ''},
  88. {t: {}, e: ''},
  89. {t: [], e: ''},
  90. {t: '', e: ''},
  91. {t: function(){}, e: ''},
  92. {t: '/path/to/file.ext', e: 'file.ext'},
  93. {t: 'file.ext', e: 'file.ext'},
  94. {t: 'file', e: 'file'},
  95. {t: '/path/to/file', e: 'file'}
  96. ];
  97. tests.forEach(function(test) {
  98. it('Check ' + typeof test.t, function () {
  99. expect(string_utils.getFileFromPath(test.t)).to.equal(test.e);
  100. });
  101. });
  102. });
  103. });