number_utils_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 numberUtils = require('utils/number_utils');
  19. describe('', function() {
  20. describe('#bytesToSize', function() {
  21. describe('check bytes', function() {
  22. var tests = Em.A([
  23. {
  24. bytes: null,
  25. precision: null,
  26. parseType: null,
  27. multiplyBy: null,
  28. e: 'n/a',
  29. m: '"n/a" if bytes is null'
  30. },
  31. {
  32. bytes: undefined,
  33. precision: null,
  34. parseType: null,
  35. multiplyBy: null,
  36. e: 'n/a',
  37. m: '"n/a" if bytes is undefined'
  38. },
  39. {
  40. bytes: 200,
  41. precision: null,
  42. parseType: undefined,
  43. multiplyBy: null,
  44. e: '0 Bytes',
  45. m: '0 if multiply is `null`'
  46. },
  47. {
  48. bytes: 200,
  49. precision: null,
  50. parseType: undefined,
  51. multiplyBy: undefined,
  52. e: '200 Bytes',
  53. m: '"200 Bytes" if `multiplyBy` and `parseType` are `undefined`'
  54. },
  55. {
  56. bytes: 200,
  57. precision: null,
  58. parseType: undefined,
  59. multiplyBy: 1,
  60. e: '200 Bytes',
  61. m: '`200 Bytes` if `parsetype` is `undefined`'
  62. }
  63. ]);
  64. tests.forEach(function(test) {
  65. it(test.m, function() {
  66. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy)).to.equal(test.e);
  67. });
  68. });
  69. });
  70. describe('check sizes', function() {
  71. var tests = Em.A([
  72. {
  73. bytes: 12,
  74. precision: null,
  75. parseType: 'parseInt',
  76. multiplyBy: 1,
  77. e: 'Bytes',
  78. m: 'Bytes'
  79. },
  80. {
  81. bytes: 1024 + 12,
  82. precision: null,
  83. parseType: 'parseInt',
  84. multiplyBy: 1,
  85. e: 'KB',
  86. m: 'KB'
  87. },
  88. {
  89. bytes: 1024 * 1024 + 12,
  90. precision: null,
  91. parseType: 'parseInt',
  92. multiplyBy: 1,
  93. e: 'MB',
  94. m: 'MB'
  95. },
  96. {
  97. bytes: 1024 * 1024 * 1024 + 12,
  98. precision: null,
  99. parseType: 'parseInt',
  100. multiplyBy: 1,
  101. e: 'GB',
  102. m: 'GB'
  103. },
  104. {
  105. bytes: 1024 * 1024 * 1024 * 1024 + 12,
  106. precision: null,
  107. parseType: 'parseInt',
  108. multiplyBy: 1,
  109. e: 'TB',
  110. m: 'TB'
  111. },
  112. {
  113. bytes: 1024 * 1024 * 1024 * 1024 * 1024 + 12,
  114. precision: null,
  115. parseType: 'parseInt',
  116. multiplyBy: 1,
  117. e: 'PB',
  118. m: 'PB'
  119. }
  120. ]);
  121. tests.forEach(function(test) {
  122. it(test.m, function() {
  123. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).endsWith(test.e)).to.equal(true);
  124. });
  125. });
  126. });
  127. describe('check calculated result', function() {
  128. var tests = Em.A([
  129. {
  130. bytes: 42,
  131. precision: null,
  132. parseType: 'parseInt',
  133. multiplyBy: 1,
  134. e: '42',
  135. m: 'Bytes'
  136. },
  137. {
  138. bytes: 1024 * 12,
  139. precision: null,
  140. parseType: 'parseInt',
  141. multiplyBy: 1,
  142. e: '12',
  143. m: 'KB'
  144. },
  145. {
  146. bytes: 1024 * 1024 * 23,
  147. precision: null,
  148. parseType: 'parseInt',
  149. multiplyBy: 1,
  150. e: '23',
  151. m: 'MB'
  152. },
  153. {
  154. bytes: 1024 * 1024 * 1024 * 34,
  155. precision: null,
  156. parseType: 'parseInt',
  157. multiplyBy: 1,
  158. e: '34',
  159. m: 'GB'
  160. },
  161. {
  162. bytes: 1024 * 1024 * 1024 * 1024 * 45,
  163. precision: null,
  164. parseType: 'parseInt',
  165. multiplyBy: 1,
  166. e: '45',
  167. m: 'TB'
  168. },
  169. {
  170. bytes: 1024 * 1024 * 1024 * 1024 * 1024 * 56,
  171. precision: null,
  172. parseType: 'parseInt',
  173. multiplyBy: 1,
  174. e: '56',
  175. m: 'PB'
  176. }
  177. ]);
  178. tests.forEach(function(test) {
  179. it(test.m, function() {
  180. expect(numberUtils.bytesToSize(test.bytes, test.precision, test.parseType, test.multiplyBy).startsWith(test.e)).to.equal(true);
  181. });
  182. });
  183. });
  184. });
  185. describe('#validateInteger()', function() {
  186. var tests = [
  187. {
  188. str: null,
  189. min: null,
  190. max: null,
  191. m: 'all params null to' + Em.I18n.t('number.validate.empty'),
  192. e: Em.I18n.t('number.validate.empty')
  193. },
  194. {
  195. str: "string",
  196. min: null,
  197. max: null,
  198. m: 'try to validate `string` should return ' + Em.I18n.t('number.validate.empty'),
  199. e: Em.I18n.t('number.validate.notValidNumber')
  200. },
  201. {
  202. str: "string",
  203. min: null,
  204. max: null,
  205. m: 'try to validate `string` should return ' + Em.I18n.t('number.validate.notValidNumber'),
  206. e: Em.I18n.t('number.validate.notValidNumber')
  207. },
  208. {
  209. str: "1abc",
  210. min: null,
  211. max: null,
  212. m: 'try to validate `1abc` should return ' + Em.I18n.t('number.validate.notValidNumber'),
  213. e: Em.I18n.t('number.validate.notValidNumber')
  214. },
  215. {
  216. str: "1",
  217. min: null,
  218. max: null,
  219. m: 'try to validate `1` should return ' + Em.I18n.t('number.validate.moreThanMaximum').format(null),
  220. e: Em.I18n.t('number.validate.moreThanMaximum').format(null)
  221. },
  222. {
  223. str: "1",
  224. min: 2,
  225. max: 0,
  226. m: 'try to validate `1` with max = 0 and min = 2 should return ' + Em.I18n.t('number.validate.lessThanMinumum').format(2),
  227. e: Em.I18n.t('number.validate.lessThanMinumum').format(2)
  228. }
  229. ];
  230. tests.forEach(function(test) {
  231. it(test.m, function(){
  232. expect(numberUtils.validateInteger(test.str, test.min, test.max)).to.eql(test.e);
  233. });
  234. });
  235. });
  236. });