| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- declare(strict_types=1);
-
- use PHPUnit\Framework\TestCase;
-
- require_once __DIR__ . '/../../php/markdown.php';
- require_once __DIR__ . '/../../php/spreadsheet.php';
-
- final class CellValueTests extends TestCase {
- public function test_fromCellString_blank() {
- $value = CellValue::fromCellString('');
- $this->assertSame(CellValue::TYPE_BLANK, $value->type);
- $this->assertSame('', $value->formattedValue);
- $this->assertSame(null, $value->value);
- $value = CellValue::fromCellString(' ');
- $this->assertSame(CellValue::TYPE_BLANK, $value->type);
- $this->assertSame('', $value->formattedValue);
- $this->assertSame(null, $value->value);
- }
-
- public function test_fromCellString_number() {
- $value = CellValue::fromCellString('123');
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('123', $value->formattedValue);
- $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
- $this->assertSame(0, $value->decimals);
- $value = CellValue::fromCellString('-0');
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('0', $value->formattedValue);
- $this->assertEqualsWithDelta(0, $value->value, 0.000_001);
- $this->assertSame(0, $value->decimals);
- $value = CellValue::fromCellString('1,234');
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('1,234', $value->formattedValue);
- $this->assertEqualsWithDelta(1234, $value->value, 0.000_001);
- $this->assertSame(0, $value->decimals);
- $value = CellValue::fromCellString('-1,234,567.89');
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('-1,234,567.89', $value->formattedValue);
- $this->assertEqualsWithDelta(-1234567.89, $value->value, 0.000_001);
- $this->assertSame(2, $value->decimals);
- }
-
- public function test_fromCellString_percent() {
- $value = CellValue::fromCellString('123%');
- $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
- $this->assertSame('123%', $value->formattedValue);
- $this->assertEqualsWithDelta(1.23, $value->value, 0.000_001);
- $this->assertSame(0, $value->decimals);
- $value = CellValue::fromCellString('-12.3%');
- $this->assertSame(CellValue::TYPE_PERCENT, $value->type);
- $this->assertSame('-12.3%', $value->formattedValue);
- $this->assertEqualsWithDelta(-0.123, $value->value, 0.000_001);
- $this->assertSame(1, $value->decimals);
- }
-
- public function test_fromCellString_currency() {
- $value = CellValue::fromCellString('$123');
- $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
- $this->assertSame('$123', $value->formattedValue);
- $this->assertEqualsWithDelta(123, $value->value, 0.000_001);
- $this->assertSame(0, $value->decimals);
- $value = CellValue::fromCellString('-$12.34');
- $this->assertSame(CellValue::TYPE_CURRENCY, $value->type);
- $this->assertSame('-$12.34', $value->formattedValue);
- $this->assertEqualsWithDelta(-12.34, $value->value, 0.000_001);
- $this->assertSame(2, $value->decimals);
- }
-
- public function test_fromCellString_boolean() {
- $value = CellValue::fromCellString('true');
- $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
- $this->assertSame('TRUE', $value->formattedValue);
- $this->assertSame(true, $value->value);
- $value = CellValue::fromCellString('false');
- $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
- $this->assertSame('FALSE', $value->formattedValue);
- $this->assertSame(false, $value->value);
- }
-
- public function test_fromCellString_string() {
- $value = CellValue::fromCellString('some text');
- $this->assertSame(CellValue::TYPE_STRING, $value->type);
- $this->assertSame('some text', $value->formattedValue);
- $this->assertSame('some text', $value->value);
- $value = CellValue::fromCellString("'0123");
- $this->assertSame(CellValue::TYPE_STRING, $value->type);
- $this->assertSame('0123', $value->formattedValue);
- $this->assertSame('0123', $value->value);
- $value = CellValue::fromCellString("'=123");
- $this->assertSame(CellValue::TYPE_STRING, $value->type);
- $this->assertSame('=123', $value->formattedValue);
- $this->assertSame('=123', $value->value);
- }
-
- public function test_fromCellString_formula() {
- $value = CellValue::fromCellString('=A*B');
- $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
- $this->assertSame('=A*B', $value->formattedValue);
- $this->assertSame('=A*B', $value->value);
- $value = CellValue::fromCellString('=MAX(A, 3)');
- $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
- $this->assertSame('=MAX(A, 3)', $value->formattedValue);
- $this->assertSame('=MAX(A, 3)', $value->value);
- }
-
- public function test_fromValue_null() {
- $value = CellValue::fromValue(null);
- $this->assertSame(CellValue::TYPE_BLANK, $value->type);
- }
-
- public function test_fromValue_number() {
- $value = CellValue::fromValue(123);
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('123', $value->formattedValue);
- $value = CellValue::fromValue(3.141592);
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('3.141592', $value->formattedValue);
- $value = CellValue::fromValue(123456789);
- $this->assertSame(CellValue::TYPE_NUMBER, $value->type);
- $this->assertSame('123,456,789', $value->formattedValue);
- }
-
- public function test_fromValue_boolean() {
- $value = CellValue::fromValue(true);
- $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
- $this->assertSame('TRUE', $value->formattedValue);
- $value = CellValue::fromValue(false);
- $this->assertSame(CellValue::TYPE_BOOLEAN, $value->type);
- $this->assertSame('FALSE', $value->formattedValue);
- }
-
- public function test_fromValue_string() {
- $value = CellValue::fromValue('foo');
- $this->assertSame(CellValue::TYPE_STRING, $value->type);
- $this->assertSame('foo', $value->formattedValue);
- $value = CellValue::fromValue('0123456');
- $this->assertSame(CellValue::TYPE_STRING, $value->type);
- $this->assertSame('0123456', $value->formattedValue);
- }
-
- public function test_fromValue_formula() {
- $value = CellValue::fromValue('=A*B');
- $this->assertSame(CellValue::TYPE_FORMULA, $value->type);
- $this->assertSame('=A*B', $value->formattedValue);
- }
-
- private function assertSameCellValue(CellValue $expected, CellValue $actual) {
- $this->assertSame($expected->formattedValue, $actual->formattedValue);
- $this->assertSame($expected->value, $actual->value);
- $this->assertSame($expected->type, $actual->type);
- $this->assertSame($expected->decimals, $actual->decimals);
- }
-
- public function test_operation_add() {
- $a = CellValue::fromValue(3);
- $b = CellValue::fromValue(4);
- $actual = $a->add($b);
- $expected = new CellValue('7', 7, CellValue::TYPE_NUMBER, 0);
- $this->assertSameCellValue($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->assertSameCellValue($expected, $actual);
-
- $a = CellValue::fromCellString('$123');
- $b = CellValue::fromCellString('$321');
- $actual = $a->add($b);
- $expected = new CellValue('$444.00', 444.0, CellValue::TYPE_CURRENCY, 2);
- $this->assertSameCellValue($expected, $actual);
- }
-
- public function test_operation_subtract() {
- $a = CellValue::fromValue(9);
- $b = CellValue::fromValue(4);
- $actual = $a->subtract($b);
- $expected = new CellValue('5', 5, CellValue::TYPE_NUMBER, 0);
- $this->assertSameCellValue($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->assertSameCellValue($expected, $actual);
-
- $a = CellValue::fromCellString('$321');
- $b = CellValue::fromCellString('$123');
- $actual = $a->subtract($b);
- $expected = new CellValue('$198.00', 198.0, CellValue::TYPE_CURRENCY, 2);
- $this->assertSameCellValue($expected, $actual);
- }
-
- public function test_operation_multiply() {
- $a = CellValue::fromValue(3);
- $b = CellValue::fromValue(4);
- $actual = $a->multiply($b);
- $expected = new CellValue('12', 12, CellValue::TYPE_NUMBER, 0);
- $this->assertSameCellValue($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->assertSameCellValue($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->assertSameCellValue($expected, $actual);
- }
-
- public function test_operation_divide() {
- $a = CellValue::fromValue(12);
- $b = CellValue::fromValue(4);
- $actual = $a->divide($b);
- $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
- $this->assertSameCellValue($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->assertSameCellValue($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->assertSameCellValue($expected, $actual);
- }
-
- public function test_operation_modulo() {
- $a = CellValue::fromValue(7);
- $b = CellValue::fromValue(4);
- $actual = $a->modulo($b);
- $expected = new CellValue('3', 3, CellValue::TYPE_NUMBER, 0);
- $this->assertSameCellValue($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->assertSameCellValue($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->assertSameCellValue($expected, $actual);
- }
-
- public function test_operation_unaryNot() {
- $this->assertSameCellValue(CellValue::fromValue(false), CellValue::fromValue(true)->not());
- $this->assertSameCellValue(CellValue::fromValue(true), CellValue::fromValue(false)->not());
- }
-
- public function test_operation_comparators() {
- $a = CellValue::fromValue(3);
- $b = CellValue::fromValue(4);
- $t = CellValue::fromValue(true);
- $f = CellValue::fromValue(false);
-
- $this->assertSameCellValue($t, $a->lt($b));
- $this->assertSameCellValue($t, $a->lte($b));
- $this->assertSameCellValue($f, $a->gt($b));
- $this->assertSameCellValue($f, $a->gte($b));
- $this->assertSameCellValue($f, $a->eq($b));
- $this->assertSameCellValue($t, $a->neq($b));
-
- $this->assertSameCellValue($f, $b->lt($a));
- $this->assertSameCellValue($f, $b->lte($a));
- $this->assertSameCellValue($t, $b->gt($a));
- $this->assertSameCellValue($t, $b->gte($a));
- $this->assertSameCellValue($f, $b->eq($a));
- $this->assertSameCellValue($t, $b->neq($a));
-
- $this->assertSameCellValue($f, $a->lt($a));
- $this->assertSameCellValue($t, $a->lte($a));
- $this->assertSameCellValue($f, $a->gt($a));
- $this->assertSameCellValue($t, $a->gte($a));
- $this->assertSameCellValue($t, $a->eq($a));
- $this->assertSameCellValue($f, $a->neq($a));
- }
-
- public function test_operation_concatenate() {
- $a = CellValue::fromValue('abc');
- $b = CellValue::fromValue('xyz');
- $actual = $a->concatenate($b);
- $expected = CellValue::fromValue('abcxyz');
- $this->assertSameCellValue($expected, $actual);
- }
- }
- ?>
|