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