| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- class CellValueTests extends BaseTest {
- test_fromCellString_blank() {
- var value;
- value = CellValue.fromCellString('');
- this.assertEqual(value.type, CellValue.TYPE_BLANK);
- this.assertEqual(value.formattedValue, '');
- this.assertEqual(value.value, null);
- value = CellValue.fromCellString(' ');
- this.assertEqual(value.type, CellValue.TYPE_BLANK);
- this.assertEqual(value.formattedValue, '');
- this.assertEqual(value.value, null);
- }
-
- test_fromCellString_number() {
- var value;
- value = CellValue.fromCellString('123');
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '123');
- this.assertEqual(value.value, 123);
- this.assertEqual(value.decimals, 0);
- value = CellValue.fromCellString('-0');
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '-0');
- this.assertEqual(value.value, 0);
- this.assertEqual(value.decimals, 0);
- value = CellValue.fromCellString('1,234');
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '1,234');
- this.assertEqual(value.value, 1234);
- this.assertEqual(value.decimals, 0);
- value = CellValue.fromCellString('-1,234,567.89');
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '-1,234,567.89');
- this.assertEqual(value.value, -1234567.89);
- this.assertEqual(value.decimals, 2);
- }
-
- test_fromCellString_percent() {
- var value;
- value = CellValue.fromCellString('123%');
- this.assertEqual(value.type, CellValue.TYPE_PERCENT);
- this.assertEqual(value.formattedValue, '123%');
- this.assertEqual(value.value, 1.23, 0.0001);
- this.assertEqual(value.decimals, 0);
- value = CellValue.fromCellString('-12.3%');
- this.assertEqual(value.type, CellValue.TYPE_PERCENT);
- this.assertEqual(value.formattedValue, '-12.3%');
- this.assertEqual(value.value, -0.123, 0.0001);
- this.assertEqual(value.decimals, 1);
- }
-
- test_fromCellString_currency() {
- var value;
- value = CellValue.fromCellString('$123');
- this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
- this.assertEqual(value.formattedValue, '$123');
- this.assertEqual(value.value, 123);
- this.assertEqual(value.decimals, 0);
- value = CellValue.fromCellString('-$12.34');
- this.assertEqual(value.type, CellValue.TYPE_CURRENCY);
- this.assertEqual(value.formattedValue, '-$12.34');
- this.assertEqual(value.value, -12.34, 0.0001);
- this.assertEqual(value.decimals, 2);
- }
-
- test_fromCellString_boolean() {
- var value;
- value = CellValue.fromCellString('true');
- this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
- this.assertEqual(value.formattedValue, 'TRUE');
- this.assertEqual(value.value, true);
- value = CellValue.fromCellString('false');
- this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
- this.assertEqual(value.formattedValue, 'FALSE');
- this.assertEqual(value.value, false);
- }
-
- test_fromCellString_string() {
- var value;
- value = CellValue.fromCellString('some text');
- this.assertEqual(value.type, CellValue.TYPE_STRING);
- this.assertEqual(value.formattedValue, 'some text');
- this.assertEqual(value.value, 'some text');
- value = CellValue.fromCellString("'0123");
- this.assertEqual(value.type, CellValue.TYPE_STRING);
- this.assertEqual(value.formattedValue, '0123');
- this.assertEqual(value.value, '0123');
- value = CellValue.fromCellString("'=123");
- this.assertEqual(value.type, CellValue.TYPE_STRING);
- this.assertEqual(value.formattedValue, '=123');
- this.assertEqual(value.value, '=123');
- }
-
- test_fromCellString_formula() {
- var value;
- value = CellValue.fromCellString('=A*B');
- this.assertEqual(value.type, CellValue.TYPE_FORMULA);
- this.assertEqual(value.formattedValue, '=A*B');
- this.assertEqual(value.value, '=A*B');
- value = CellValue.fromCellString('=MAX(A, 3)');
- this.assertEqual(value.type, CellValue.TYPE_FORMULA);
- this.assertEqual(value.formattedValue, '=MAX(A, 3)');
- this.assertEqual(value.value, '=MAX(A, 3)');
- }
-
- test_fromValue_null() {
- var value;
- value = CellValue.fromValue(null);
- this.assertEqual(value.type, CellValue.TYPE_BLANK);
- }
-
- test_fromValue_number() {
- var value;
- value = CellValue.fromValue(123);
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '123');
- value = CellValue.fromValue(3.141592);
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '3.141592');
- value = CellValue.fromValue(123456789);
- this.assertEqual(value.type, CellValue.TYPE_NUMBER);
- this.assertEqual(value.formattedValue, '123,456,789');
- }
-
- test_fromValue_boolean() {
- var value;
- value = CellValue.fromValue(true);
- this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
- this.assertEqual(value.formattedValue, 'TRUE');
- value = CellValue.fromValue(false);
- this.assertEqual(value.type, CellValue.TYPE_BOOLEAN);
- this.assertEqual(value.formattedValue, 'FALSE');
- }
-
- test_fromValue_string() {
- var value;
- value = CellValue.fromValue('foo');
- this.assertEqual(value.type, CellValue.TYPE_STRING);
- this.assertEqual(value.formattedValue, 'foo');
- value = CellValue.fromValue('123');
- this.assertEqual(value.type, CellValue.TYPE_STRING);
- this.assertEqual(value.formattedValue, '123');
- }
-
- test_fromValue_formula() {
- var value;
- value = CellValue.fromValue('=A*B');
- this.assertEqual(value.type, CellValue.TYPE_FORMULA);
- this.assertEqual(value.formattedValue, '=A*B');
- }
-
- test_operation_add() {
- var a, b, result, expected;
- a = CellValue.fromValue(3);
- b = CellValue.fromValue(4);
- result = a.add(b)
- expected = new CellValue('7', 7, CellValue.TYPE_NUMBER, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('100%');
- b = CellValue.fromCellString('50%');
- result = a.add(b);
- expected = new CellValue('150%', 1.5, CellValue.TYPE_PERCENT, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('$123');
- b = CellValue.fromCellString('$321');
- result = a.add(b);
- expected = new CellValue('$444.00', 444, CellValue.TYPE_CURRENCY, 2);
- this.assertEqual(result, expected);
- }
-
- test_operation_subtract() {
- var a, b, result, expected;
- a = CellValue.fromValue(9);
- b = CellValue.fromValue(4);
- result = a.subtract(b)
- expected = new CellValue('5', 5, CellValue.TYPE_NUMBER, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('100%');
- b = CellValue.fromCellString('50%');
- result = a.subtract(b);
- expected = new CellValue('50%', 0.5, CellValue.TYPE_PERCENT, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('$321');
- b = CellValue.fromCellString('$123');
- result = a.subtract(b);
- expected = new CellValue('$198.00', 198, CellValue.TYPE_CURRENCY, 2);
- this.assertEqual(result, expected);
- }
-
- test_operation_multiply() {
- var a, b, result, expected;
- a = CellValue.fromValue(3);
- b = CellValue.fromValue(4);
- result = a.multiply(b)
- expected = new CellValue('12', 12, CellValue.TYPE_NUMBER, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('150%');
- b = CellValue.fromCellString('50%');
- result = a.multiply(b);
- expected = new CellValue('75%', 0.75, CellValue.TYPE_PERCENT, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('$321');
- b = CellValue.fromCellString('50%');
- result = a.multiply(b);
- expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
- this.assertEqual(result, expected);
- }
-
- test_operation_divide() {
- var a, b, result, expected;
- a = CellValue.fromValue(12);
- b = CellValue.fromValue(4);
- result = a.divide(b)
- expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('150%');
- b = CellValue.fromCellString('50%');
- result = a.divide(b);
- expected = new CellValue('300%', 3.0, CellValue.TYPE_PERCENT, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('$321');
- b = CellValue.fromCellString('200%');
- result = a.divide(b);
- expected = new CellValue('$160.50', 160.50, CellValue.TYPE_CURRENCY, 2);
- this.assertEqual(result, expected);
- }
-
- test_operation_modulo() {
- var a, b, result, expected;
- a = CellValue.fromValue(7);
- b = CellValue.fromValue(4);
- result = a.modulo(b)
- expected = new CellValue('3', 3, CellValue.TYPE_NUMBER, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('175%');
- b = CellValue.fromCellString('50%');
- result = a.modulo(b);
- expected = new CellValue('25%', 0.25, CellValue.TYPE_PERCENT, 0);
- this.assertEqual(result, expected);
-
- a = CellValue.fromCellString('$327');
- b = CellValue.fromCellString('$20');
- result = a.modulo(b);
- expected = new CellValue('$7.00', 7.00, CellValue.TYPE_CURRENCY, 2);
- this.assertEqual(result, expected);
- }
-
- test_operation_unaryNot() {
- this.assertEqual(CellValue.fromValue(true).not(), CellValue.fromValue(false));
- this.assertEqual(CellValue.fromValue(false).not(), CellValue.fromValue(true));
- }
-
- test_operation_comparators() {
- const a = CellValue.fromValue(3);
- const b = CellValue.fromValue(4);
- const t = CellValue.fromValue(true);
- const f = CellValue.fromValue(false);
-
- this.assertEqual(a.lt(b), t);
- this.assertEqual(a.lte(b), t);
- this.assertEqual(a.gt(b), f);
- this.assertEqual(a.gte(b), f);
- this.assertEqual(a.eq(b), f);
- this.assertEqual(a.neq(b), t);
-
- this.assertEqual(b.lt(a), f);
- this.assertEqual(b.lte(a), f);
- this.assertEqual(b.gt(a), t);
- this.assertEqual(b.gte(a), t);
- this.assertEqual(b.eq(a), f);
- this.assertEqual(b.neq(a), t);
-
- this.assertEqual(a.lt(a), f);
- this.assertEqual(a.lte(a), t);
- this.assertEqual(a.gt(a), f);
- this.assertEqual(a.gte(a), t);
- this.assertEqual(a.eq(a), t);
- this.assertEqual(a.neq(a), f);
- }
-
- test_operation_concatenate() {
- const a = CellValue.fromValue('abc');
- const b = CellValue.fromValue('xyz');
- const result = a.concatenate(b);
- const expected = CellValue.fromValue('abcxyz');
- this.assertEqual(result, expected);
- }
- }
|