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); } }