base_unit_convert_mixin_test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 App = require('app');
  19. describe('App.BaseUnitConvertMixin', function() {
  20. beforeEach(function() {
  21. this.mixin = Em.Object.create(App.BaseUnitConvertMixin, {});
  22. });
  23. describe('#convertValue', function() {
  24. var tests = [
  25. {
  26. value: "10",
  27. fromUnit: "b",
  28. toUnit: "b",
  29. e: 10
  30. },
  31. {
  32. value: "2048",
  33. fromUnit: "b",
  34. toUnit: "kb",
  35. e: 2
  36. },
  37. {
  38. value: "2097152",
  39. fromUnit: "b",
  40. toUnit: "mb",
  41. e: 2
  42. },
  43. {
  44. value: "1",
  45. fromUnit: "mb",
  46. toUnit: "kb",
  47. e: 1024
  48. },
  49. {
  50. value: "107374182400",
  51. fromUnit: "b",
  52. toUnit: "gb",
  53. e: 100
  54. },
  55. {
  56. value: "100",
  57. fromUnit: "gb",
  58. toUnit: "b",
  59. e: 107374182400
  60. },
  61. {
  62. value: "1294336",
  63. fromUnit: "b",
  64. toUnit: ["mb", "kb"],
  65. e: [
  66. { type: "mb", value: 1},
  67. { type: "kb", value: 240}
  68. ]
  69. },
  70. {
  71. value: [
  72. { type: "mb", value: 1},
  73. { type: "kb", value: 240}
  74. ],
  75. fromUnit: ["mb", "kb"],
  76. toUnit: "b",
  77. e: 1294336
  78. },
  79. {
  80. value: [
  81. { type: "mb", value: 1},
  82. { type: "kb", value: 240}
  83. ],
  84. fromUnit: "mb,kb",
  85. toUnit: "b",
  86. e: 1294336
  87. },
  88. {
  89. value: 60000,
  90. fromUnit: 'milliseconds',
  91. toUnit: "days,hours,minutes",
  92. e: [
  93. { type: 'days', value: 0},
  94. { type: 'hours', value: 0},
  95. { type: 'minutes', value: 1}
  96. ]
  97. },
  98. {
  99. value: 80,
  100. fromUnit: 'percent',
  101. toUnit: 'int',
  102. currentDimensionType: 'percent.percent_int',
  103. e: 80
  104. },
  105. {
  106. value: 80,
  107. fromUnit: 'int',
  108. toUnit: 'percent',
  109. currentDimensionType: 'percent.percent_int',
  110. e: 80
  111. },
  112. {
  113. value: 0.89,
  114. fromUnit: 'float',
  115. toUnit: 'percent',
  116. currentDimensionType: 'percent.percent_float',
  117. e: 89
  118. },
  119. {
  120. value: 89,
  121. fromUnit: 'percent',
  122. toUnit: 'float',
  123. currentDimensionType: 'percent.percent_float',
  124. e: 0.89
  125. },
  126. {
  127. value: 100,
  128. fromUnit: 'percent',
  129. toUnit: 'float',
  130. currentDimensionType: 'percent.percent_float',
  131. e: 1
  132. },
  133. {
  134. value: 1,
  135. fromUnit: 'float',
  136. toUnit: 'percent',
  137. currentDimensionType: 'percent.percent_float',
  138. e: 100
  139. }
  140. ];
  141. tests.forEach(function(test) {
  142. it('should convert {0} {1} to {2} {3}'.format(JSON.stringify(test.value), test.fromUnit, JSON.stringify(test.e), test.toUnit), function() {
  143. if (test.currentDimensionType) {
  144. this.mixin.set('currentDimensionType', test.currentDimensionType);
  145. }
  146. var result = this.mixin.convertValue(test.value, test.fromUnit, test.toUnit);
  147. if (Em.isArray(result)) {
  148. result = result.map(function(item) {
  149. return App.permit(item, ['type', 'value']);
  150. });
  151. }
  152. expect(result).to.be.eql(test.e);
  153. });
  154. }, this);
  155. });
  156. });