PHP and Javascript implementations of a simple markdown parser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CellValueTests.js 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. class CellValueTests extends BaseTest {
  2. test_fromCellString_blank() {
  3. var value;
  4. value = CellValue.fromCellString('');
  5. this.assertEqual(CellValue.TYPE_BLANK, value.type);
  6. this.assertEqual('', value.formattedValue);
  7. this.assertEqual(null, value.value);
  8. value = CellValue.fromCellString(' ');
  9. this.assertEqual(CellValue.TYPE_BLANK, value.type);
  10. this.assertEqual('', value.formattedValue);
  11. this.assertEqual(null, value.value);
  12. }
  13. test_fromCellString_number() {
  14. var value;
  15. value = CellValue.fromCellString('123');
  16. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  17. this.assertEqual('123', value.formattedValue);
  18. this.assertEqual(123, value.value);
  19. this.assertEqual(0, value.decimals);
  20. value = CellValue.fromCellString('-0');
  21. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  22. this.assertEqual('-0', value.formattedValue);
  23. this.assertEqual(0, value.value);
  24. this.assertEqual(0, value.decimals);
  25. value = CellValue.fromCellString('1,234');
  26. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  27. this.assertEqual('1,234', value.formattedValue);
  28. this.assertEqual(1234, value.value);
  29. this.assertEqual(0, value.decimals);
  30. value = CellValue.fromCellString('-1,234,567.89');
  31. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  32. this.assertEqual('-1,234,567.89', value.formattedValue);
  33. this.assertEqual(-1234567.89, value.value);
  34. this.assertEqual(2, value.decimals);
  35. }
  36. test_fromCellString_percent() {
  37. var value;
  38. value = CellValue.fromCellString('123%');
  39. this.assertEqual(CellValue.TYPE_PERCENT, value.type);
  40. this.assertEqual('123%', value.formattedValue);
  41. this.assertEqual(1.23, value.value);
  42. this.assertEqual(0, value.decimals);
  43. value = CellValue.fromCellString('-12.3%');
  44. this.assertEqual(CellValue.TYPE_PERCENT, value.type);
  45. this.assertEqual('-12.3%', value.formattedValue);
  46. this.assertEqual(-0.123, value.value);
  47. this.assertEqual(1, value.decimals);
  48. }
  49. test_fromCellString_currency() {
  50. var value;
  51. value = CellValue.fromCellString('$123');
  52. this.assertEqual(CellValue.TYPE_CURRENCY, value.type);
  53. this.assertEqual('$123', value.formattedValue);
  54. this.assertEqual(123, value.value);
  55. this.assertEqual(0, value.decimals);
  56. value = CellValue.fromCellString('-$12.34');
  57. this.assertEqual(CellValue.TYPE_CURRENCY, value.type);
  58. this.assertEqual('-$12.34', value.formattedValue);
  59. this.assertEqual(value.value, -12.34);
  60. this.assertEqual(2, value.decimals);
  61. }
  62. test_fromCellString_boolean() {
  63. var value;
  64. value = CellValue.fromCellString('true');
  65. this.assertEqual(CellValue.TYPE_BOOLEAN, value.type);
  66. this.assertEqual('TRUE', value.formattedValue);
  67. this.assertEqual(true, value.value);
  68. value = CellValue.fromCellString('false');
  69. this.assertEqual(CellValue.TYPE_BOOLEAN, value.type);
  70. this.assertEqual('FALSE', value.formattedValue);
  71. this.assertEqual(false, value.value);
  72. }
  73. test_fromCellString_string() {
  74. var value;
  75. value = CellValue.fromCellString('some text');
  76. this.assertEqual(CellValue.TYPE_STRING, value.type);
  77. this.assertEqual('some text', value.formattedValue);
  78. this.assertEqual('some text', value.value);
  79. value = CellValue.fromCellString("'0123");
  80. this.assertEqual(CellValue.TYPE_STRING, value.type);
  81. this.assertEqual('0123', value.formattedValue);
  82. this.assertEqual('0123', value.value);
  83. value = CellValue.fromCellString("'=123");
  84. this.assertEqual(CellValue.TYPE_STRING, value.type);
  85. this.assertEqual('=123', value.formattedValue);
  86. this.assertEqual('=123', value.value);
  87. }
  88. test_fromCellString_formula() {
  89. var value;
  90. value = CellValue.fromCellString('=A*B');
  91. this.assertEqual(CellValue.TYPE_FORMULA, value.type);
  92. this.assertEqual('=A*B', value.formattedValue);
  93. this.assertEqual('=A*B', value.value);
  94. value = CellValue.fromCellString('=MAX(A, 3)');
  95. this.assertEqual(CellValue.TYPE_FORMULA, value.type);
  96. this.assertEqual('=MAX(A, 3)', value.formattedValue);
  97. this.assertEqual('=MAX(A, 3)', value.value);
  98. }
  99. test_fromValue_null() {
  100. var value;
  101. value = CellValue.fromValue(null);
  102. this.assertEqual(CellValue.TYPE_BLANK, value.type);
  103. }
  104. test_fromValue_number() {
  105. var value;
  106. value = CellValue.fromValue(123);
  107. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  108. this.assertEqual('123', value.formattedValue);
  109. value = CellValue.fromValue(3.141592);
  110. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  111. this.assertEqual('3.141592', value.formattedValue);
  112. value = CellValue.fromValue(123456789);
  113. this.assertEqual(CellValue.TYPE_NUMBER, value.type);
  114. this.assertEqual('123,456,789', value.formattedValue);
  115. }
  116. test_fromValue_boolean() {
  117. var value;
  118. value = CellValue.fromValue(true);
  119. this.assertEqual(CellValue.TYPE_BOOLEAN, value.type);
  120. this.assertEqual('TRUE', value.formattedValue);
  121. value = CellValue.fromValue(false);
  122. this.assertEqual(CellValue.TYPE_BOOLEAN, value.type);
  123. this.assertEqual('FALSE', value.formattedValue);
  124. }
  125. test_fromValue_string() {
  126. var value;
  127. value = CellValue.fromValue('foo');
  128. this.assertEqual(CellValue.TYPE_STRING, value.type);
  129. this.assertEqual('foo', value.formattedValue);
  130. value = CellValue.fromValue('0123456');
  131. this.assertEqual(CellValue.TYPE_STRING, value.type);
  132. this.assertEqual('0123456', value.formattedValue);
  133. }
  134. test_fromValue_formula() {
  135. var value;
  136. value = CellValue.fromValue('=A*B');
  137. this.assertEqual(CellValue.TYPE_FORMULA, value.type);
  138. this.assertEqual('=A*B', value.formattedValue);
  139. }
  140. test_operation_add() {
  141. var a, b, expected, actual;
  142. a = CellValue.fromValue(3);
  143. b = CellValue.fromValue(4);
  144. actual = a.add(b)
  145. expected = new CellValue('7', 7, CellValue.TYPE_NUMBER, 0);
  146. this.assertEqual(expected, actual);
  147. a = CellValue.fromCellString('100%');
  148. b = CellValue.fromCellString('50%');
  149. actual = a.add(b);
  150. expected = new CellValue('150%', 1.5, CellValue.TYPE_PERCENT, 0);
  151. this.assertEqual(expected, actual);
  152. a = CellValue.fromCellString('$123');
  153. b = CellValue.fromCellString('$321');
  154. actual = a.add(b);
  155. expected = new CellValue('$444.00', 444, CellValue.TYPE_CURRENCY, 2);
  156. this.assertEqual(expected, actual);
  157. }
  158. test_operation_subtract() {
  159. var a, b, expected, actual;
  160. a = CellValue.fromValue(9);
  161. b = CellValue.fromValue(4);
  162. actual = a.subtract(b)
  163. expected = new CellValue('5', 5, CellValue.TYPE_NUMBER, 0);
  164. this.assertEqual(expected, actual);
  165. a = CellValue.fromCellString('100%');
  166. b = CellValue.fromCellString('50%');
  167. actual = a.subtract(b);
  168. expected = new CellValue('50%', 0.5, CellValue.TYPE_PERCENT, 0);
  169. this.assertEqual(expected, actual);
  170. a = CellValue.fromCellString('$321');
  171. b = CellValue.fromCellString('$123');
  172. actual = a.subtract(b);
  173. expected = new CellValue('$198.00', 198, CellValue.TYPE_CURRENCY, 2);
  174. this.assertEqual(expected, actual);
  175. }
  176. test_operation_multiply() {
  177. var a, b, expected, actual;
  178. a = CellValue.fromValue(3);
  179. b = CellValue.fromValue(4);
  180. actual = a.multiply(b)
  181. expected = new CellValue('12', 12, CellValue.TYPE_NUMBER, 0);
  182. this.assertEqual(expected, actual);
  183. a = CellValue.fromCellString('150%');
  184. b = CellValue.fromCellString('50%');
  185. actual = a.multiply(b);
  186. expected = new CellValue('75%', 0.75, CellValue.TYPE_PERCENT, 0);
  187. this.assertEqual(expected, actual);
  188. a = CellValue.fromCellString('$321');
  189. b = CellValue.fromCellString('50%');
  190. actual = a.multiply(b);
  191. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  192. this.assertEqual(expected, actual);
  193. }
  194. test_operation_divide() {
  195. var a, b, expected, actual;
  196. a = CellValue.fromValue(12);
  197. b = CellValue.fromValue(4);
  198. actual = a.divide(b)
  199. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  200. this.assertEqual(expected, actual);
  201. a = CellValue.fromCellString('150%');
  202. b = CellValue.fromCellString('50%');
  203. actual = a.divide(b);
  204. expected = new CellValue('300%', 3.0, CellValue.TYPE_PERCENT, 0);
  205. this.assertEqual(expected, actual);
  206. a = CellValue.fromCellString('$321');
  207. b = CellValue.fromCellString('200%');
  208. actual = a.divide(b);
  209. expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
  210. this.assertEqual(expected, actual);
  211. }
  212. test_operation_modulo() {
  213. var a, b, expected, actual;
  214. a = CellValue.fromValue(7);
  215. b = CellValue.fromValue(4);
  216. actual = a.modulo(b)
  217. expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
  218. this.assertEqual(expected, actual);
  219. a = CellValue.fromCellString('175%');
  220. b = CellValue.fromCellString('50%');
  221. actual = a.modulo(b);
  222. expected = new CellValue('25%', 0.25, CellValue.TYPE_PERCENT, 0);
  223. this.assertEqual(expected, actual);
  224. a = CellValue.fromCellString('$327');
  225. b = CellValue.fromCellString('$20');
  226. actual = a.modulo(b);
  227. expected = new CellValue('$7.00', 7.00, CellValue.TYPE_CURRENCY, 2);
  228. this.assertEqual(expected, actual);
  229. }
  230. test_operation_unaryNot() {
  231. this.assertEqual(CellValue.fromValue(false), CellValue.fromValue(true).not());
  232. this.assertEqual(CellValue.fromValue(true), CellValue.fromValue(false).not());
  233. }
  234. test_operation_comparators() {
  235. const a = CellValue.fromValue(3);
  236. const b = CellValue.fromValue(4);
  237. const t = CellValue.fromValue(true);
  238. const f = CellValue.fromValue(false);
  239. this.assertEqual(t, a.lt(b));
  240. this.assertEqual(t, a.lte(b));
  241. this.assertEqual(f, a.gt(b));
  242. this.assertEqual(f, a.gte(b));
  243. this.assertEqual(f, a.eq(b));
  244. this.assertEqual(t, a.neq(b));
  245. this.assertEqual(f, b.lt(a));
  246. this.assertEqual(f, b.lte(a));
  247. this.assertEqual(t, b.gt(a));
  248. this.assertEqual(t, b.gte(a));
  249. this.assertEqual(f, b.eq(a));
  250. this.assertEqual(t, b.neq(a));
  251. this.assertEqual(f, a.lt(a));
  252. this.assertEqual(t, a.lte(a));
  253. this.assertEqual(f, a.gt(a));
  254. this.assertEqual(t, a.gte(a));
  255. this.assertEqual(t, a.eq(a));
  256. this.assertEqual(f, a.neq(a));
  257. }
  258. test_operation_concatenate() {
  259. const a = CellValue.fromValue('abc');
  260. const b = CellValue.fromValue('xyz');
  261. const actual = a.concatenate(b);
  262. const expected = CellValue.fromValue('abcxyz');
  263. this.assertEqual(expected, actual);
  264. }
  265. }