PHP and Javascript implementations of a simple markdown parser
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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