You are here

public function MeasurementTest::testArithmetic in Physical Fields 8

Tests the arithmetic methods.

::covers add ::covers subtract ::covers multiply ::covers divide.

File

tests/src/Unit/MeasurementTest.php, line 63

Class

MeasurementTest
Tests the measurement base class.

Namespace

Drupal\Tests\physical\Unit

Code

public function testArithmetic() {
  $result = $this->measurement
    ->add(new Length('5', 'm'));
  $this
    ->assertEquals(new Length('15', 'm'), $result);
  $result = $this->measurement
    ->subtract(new Length('5', 'm'));
  $this
    ->assertEquals(new Length('5', 'm'), $result);
  $result = $this->measurement
    ->multiply('5');
  $this
    ->assertEquals(new Length('50', 'm'), $result);
  $result = $this->measurement
    ->divide('10');
  $this
    ->assertEquals(new Length('1', 'm'), $result);

  // Test mismatched units.
  $result = $this->measurement
    ->add(new Length('200', 'cm'));
  $this
    ->assertEquals(new Length('12', 'm'), $result);
  $result = $this->measurement
    ->subtract(new Length('2.5', 'ft'));
  $this
    ->assertEquals(new Length('9.238', 'm'), $result);
}