You are here

public function UnitsWebTestCase::testConvert in Units of Measurement 7

Same name and namespace in other branches
  1. 7.2 units.test \UnitsWebTestCase::testConvert()

Testing unit conversion correctness.

To be specific, the following gets tested:

  • converting within the same unit yields the same amount
  • converting an arbitrary number happens according to the logic of conversion
  • if $from and $to are from different measures, FALSE must be returned
  • if non-existing $from is provided, FALSE must be returned
  • if non-existing $to is provided, FALSE must be returned

File

./units.test, line 144
Tests for Units module.

Class

UnitsWebTestCase
@file Tests for Units module.

Code

public function testConvert() {
  $measure = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => 'test_measure',
    'converter' => 'linear',
  ), 'units_measure');
  $measure
    ->save();
  $unit_from = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit1',
    'symbol' => $this
      ->randomName(2),
    'factor' => 1,
  ), 'units_unit');
  $unit_from
    ->save();
  $unit_to = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit2',
    'symbol' => $this
      ->randomName(2),
    'factor' => 100,
  ), 'units_unit');
  $unit_to
    ->save();
  $quantity = rand(1, 1000);
  $this
    ->assertEqual($quantity, units_convert($quantity, $unit_from->machine_name, $unit_from->machine_name), 'Converting within the same unit yields the same amount.');
  $this
    ->assertEqual($quantity * $unit_from->factor / $unit_to->factor, units_convert($quantity, $unit_from->machine_name, $unit_to->machine_name), 'Converting an arbitrary amount happens according to the logic of conversion.');
  $another_measure = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => 'another_test_measure',
    'converter' => 'linear',
  ), 'units_measure');
  $another_measure
    ->save();
  $another_unit_to = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $another_measure->measure,
    'machine_name' => 'test_unit3',
    'symbol' => $this
      ->randomName(2),
    'factor' => 100,
  ), 'units_unit');
  $another_unit_to
    ->save();
  $this
    ->assertIdentical(FALSE, units_convert($quantity, $unit_from->machine_name, $another_unit_to->machine_name), 'Converting from a unit from one measure into a unit from another measure yields FALSE.');
  $another_unit = new Entity(array(
    'label' => $this
      ->randomName(),
    'measure' => $measure->measure,
    'machine_name' => 'test_unit4',
    'symbol' => $this
      ->randomName(2),
    'factor' => 100,
  ), 'units_unit');
  $this
    ->assertIdentical(FALSE, units_convert($quantity, $unit_from->machine_name, $another_unit->machine_name), 'Converting into a non-existing unit yields FALSE.');
  $this
    ->assertIdentical(FALSE, units_convert($quantity, $another_unit->machine_name, $unit_to->machine_name), 'Converting from a non-existing unit yields FALSE.');
}