From d51acdc8b976ea86b140b78ddc57128e9f614e83 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 7 Sep 2014 21:24:01 +0200 Subject: [PATCH 1/3] Refactor to use type-mapping instead of if/else cascade --- .../php/unittest/assert/TypeMap.class.php | 102 +++++++++++++++++ src/main/php/unittest/assert/Value.class.php | 45 ++++---- .../unittest/InstanceExtractorTest.class.php | 2 +- .../assert/unittest/TypeMapTest.class.php | 105 ++++++++++++++++++ 4 files changed, 229 insertions(+), 25 deletions(-) create mode 100755 src/main/php/unittest/assert/TypeMap.class.php create mode 100755 src/test/php/unittest/assert/unittest/TypeMapTest.class.php diff --git a/src/main/php/unittest/assert/TypeMap.class.php b/src/main/php/unittest/assert/TypeMap.class.php new file mode 100755 index 0000000..da66fb0 --- /dev/null +++ b/src/main/php/unittest/assert/TypeMap.class.php @@ -0,0 +1,102 @@ +getName(); + if (isset($this->types[$class])) { + return $class; + } else { + foreach ($this->types as $mapped => $to) { + if (Type::forName($mapped)->isAssignableFrom($type)) return $mapped; + } + } + return null; + } + + /** + * Maps a given type to a given value + * + * @param lang.Type $type + * @param var $value + * @return self + */ + public function map(Type $type, $value) { + $this->types[$type->getName()]= $value; + return $this; + } + + /** + * Gets mapping for a given type + * + * @param lang.Type $type + * @param var $default + * @return var $value + */ + public function get($type, $default= null) { + if ($found= $this->find($type)) { + return $this->types[$found]; + } else { + return $default; + } + } + + /** + * list[]= overloading + * + * @param lang.Type $type + * @param var $value + * @throws lang.IllegalArgumentException if key is neither a string (set) nor NULL (add) + */ + public function offsetSet($type, $value) { + $this->types[$type->getName()]= $value; + } + + /** + * = list[] overloading + * + * @param lang.Type $type + * @return var + * @throws lang.IndexOutOfBoundsException if key does not exist + */ + public function offsetGet($type) { + if ($found= $this->find($type)) { + return $this->types[$found]; + } else { + raise('lang.IndexOutOfBoundsException', 'No element for type '.$type); + } + } + + /** + * isset() overloading + * + * @param lang.Type $type + * @return bool + */ + public function offsetExists($type) { + return null !== $this->find($type); + } + + /** + * unset() overloading + * + * @param lang.Type $type + */ + public function offsetUnset($type) { + unset($this->types[$type->getName()]); + } +} \ No newline at end of file diff --git a/src/main/php/unittest/assert/Value.class.php b/src/main/php/unittest/assert/Value.class.php index 37dab6f..cbd1115 100755 --- a/src/main/php/unittest/assert/Value.class.php +++ b/src/main/php/unittest/assert/Value.class.php @@ -1,22 +1,32 @@ map(Primitive::$STRING, $ctor->cast('unittest\assert\StringPrimitiveValue::new')) + ->map(ArrayType::forName('var[]'), $ctor->cast('unittest\assert\ArrayPrimitiveValue::new')) + ->map(MapType::forName('[:var]'), $ctor->cast('unittest\assert\MapPrimitiveValue::new')) + ->map(XPClass::forName('lang.types.String'), $ctor->cast('unittest\assert\StringValue::new')) + ->map(XPClass::forName('lang.types.ArrayList'), $ctor->cast('unittest\assert\ArrayValue::new')) + ->map(XPClass::forName('lang.types.ArrayMap'), $ctor->cast('unittest\assert\MapValue::new')) + ->map(Type::$VAR, $ctor->cast('unittest\assert\Value::new')) + ; + } + + public function __construct($value) { $this->value= $value; } @@ -27,21 +37,8 @@ protected function __construct($value) { * @return unittest.assert.Value */ public static function of($value) { - if (is_array($value) && 0 === key($value)) { - return new ArrayPrimitiveValue($value); - } else if (is_array($value)) { - return new MapPrimitiveValue($value); - } else if (is_string($value)) { - return new StringPrimitiveValue($value); - } else if ($value instanceof ArrayList) { - return new ArrayValue($value); - } else if ($value instanceof ArrayMap) { - return new MapValue($value); - } else if ($value instanceof String) { - return new StringValue($value); - } else { - return new Value($value); - } + $ctor= self::$types[typeof($value)]; + return $ctor($value); } /** diff --git a/src/test/php/unittest/assert/unittest/InstanceExtractorTest.class.php b/src/test/php/unittest/assert/unittest/InstanceExtractorTest.class.php index c14e44c..91e9217 100755 --- a/src/test/php/unittest/assert/unittest/InstanceExtractorTest.class.php +++ b/src/test/php/unittest/assert/unittest/InstanceExtractorTest.class.php @@ -5,7 +5,7 @@ /** * Tests the `InstanceExtractor` class. */ -class InstanceExtractorTest extends AbstractAssertionsTest { +class InstanceExtractorTest extends \unittest\TestCase { /** @return lang.Object */ protected function fixture($def) { diff --git a/src/test/php/unittest/assert/unittest/TypeMapTest.class.php b/src/test/php/unittest/assert/unittest/TypeMapTest.class.php new file mode 100755 index 0000000..0ce1835 --- /dev/null +++ b/src/test/php/unittest/assert/unittest/TypeMapTest.class.php @@ -0,0 +1,105 @@ +assertFalse(isset($map[XPClass::forName('lang.Object')])); + } + + #[@test] + public function test_direct_type_returns_true() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertTrue(isset($map[XPClass::forName('lang.Object')])); + } + + #[@test] + public function test_parent_type_returns_true() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertTrue(isset($map[XPClass::forName('unittest.TestCase')])); + } + + #[@test] + public function test_interface_type_returns_true() { + $map= (new TypeMap())->map(XPClass::forName('lang.Generic'), self::FOUND); + $this->assertTrue(isset($map[XPClass::forName('unittest.TestCase')])); + } + + #[@test] + public function lookup_non_existant_returns_null() { + $this->assertEquals(null, (new TypeMap())->get(XPClass::forName('lang.Object'))); + } + + #[@test, @expect('lang.IndexOutOfBoundsException')] + public function read_non_existant_throws_exception() { + $this->assertEquals(null, (new TypeMap())[XPClass::forName('lang.Object')]); + } + + #[@test] + public function lookup_direct_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertEquals(self::FOUND, $map->get(XPClass::forName('lang.Object'))); + } + + #[@test] + public function read_direct_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertEquals(self::FOUND, $map[XPClass::forName('lang.Object')]); + } + + #[@test] + public function lookup_parent_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertEquals(self::FOUND, $map->get(XPClass::forName('unittest.TestCase'))); + } + + #[@test] + public function read_parent_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Object'), self::FOUND); + $this->assertEquals(self::FOUND, $map[XPClass::forName('unittest.TestCase')]); + } + + #[@test] + public function lookup_interface_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Generic'), self::FOUND); + $this->assertEquals(self::FOUND, $map->get(XPClass::forName('unittest.TestCase'))); + } + + #[@test] + public function read_interface_type() { + $map= (new TypeMap())->map(XPClass::forName('lang.Generic'), self::FOUND); + $this->assertEquals(self::FOUND, $map[XPClass::forName('unittest.TestCase')]); + } + + #[@test] + public function test_var_type() { + $map= (new TypeMap())->map(Type::$VAR, self::FOUND); + $this->assertTrue(isset($map[XPClass::forName('lang.Object')])); + } + + #[@test] + public function lookup_var_type() { + $map= (new TypeMap())->map(Type::$VAR, self::FOUND); + $this->assertEquals(self::FOUND, $map->get(XPClass::forName('lang.Object'))); + } + + #[@test] + public function read_var_type() { + $map= (new TypeMap())->map(Type::$VAR, self::FOUND); + $this->assertEquals(self::FOUND, $map[XPClass::forName('lang.Object')]); + } +} \ No newline at end of file From 48f32394bdff09554a6f0f16c96916e440a58217 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 28 Sep 2014 14:37:51 +0200 Subject: [PATCH 2/3] Re-add constructor apidoc See xp-framework/core#37 --- src/main/php/unittest/assert/ArrayPrimitiveValue.class.php | 2 ++ src/main/php/unittest/assert/ArrayValue.class.php | 2 ++ src/main/php/unittest/assert/MapPrimitiveValue.class.php | 2 ++ src/main/php/unittest/assert/MapValue.class.php | 2 ++ src/main/php/unittest/assert/StringPrimitiveValue.class.php | 2 ++ src/main/php/unittest/assert/StringValue.class.php | 2 ++ src/main/php/unittest/assert/Value.class.php | 5 +++++ 7 files changed, 17 insertions(+) diff --git a/src/main/php/unittest/assert/ArrayPrimitiveValue.class.php b/src/main/php/unittest/assert/ArrayPrimitiveValue.class.php index f3b39fc..33305cd 100755 --- a/src/main/php/unittest/assert/ArrayPrimitiveValue.class.php +++ b/src/main/php/unittest/assert/ArrayPrimitiveValue.class.php @@ -4,6 +4,8 @@ class ArrayPrimitiveValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return sizeof($this->value) === $size; }, diff --git a/src/main/php/unittest/assert/ArrayValue.class.php b/src/main/php/unittest/assert/ArrayValue.class.php index f89f835..a9d279a 100755 --- a/src/main/php/unittest/assert/ArrayValue.class.php +++ b/src/main/php/unittest/assert/ArrayValue.class.php @@ -4,6 +4,8 @@ class ArrayValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return $value->length === $size; }, diff --git a/src/main/php/unittest/assert/MapPrimitiveValue.class.php b/src/main/php/unittest/assert/MapPrimitiveValue.class.php index 2245bb4..40754b0 100755 --- a/src/main/php/unittest/assert/MapPrimitiveValue.class.php +++ b/src/main/php/unittest/assert/MapPrimitiveValue.class.php @@ -4,6 +4,8 @@ class MapPrimitiveValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return sizeof($this->value) === $size; }, diff --git a/src/main/php/unittest/assert/MapValue.class.php b/src/main/php/unittest/assert/MapValue.class.php index da5dc06..ce49fd8 100755 --- a/src/main/php/unittest/assert/MapValue.class.php +++ b/src/main/php/unittest/assert/MapValue.class.php @@ -2,6 +2,8 @@ class MapValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return $value->size === $size; }, diff --git a/src/main/php/unittest/assert/StringPrimitiveValue.class.php b/src/main/php/unittest/assert/StringPrimitiveValue.class.php index 8f67c21..fd389dd 100755 --- a/src/main/php/unittest/assert/StringPrimitiveValue.class.php +++ b/src/main/php/unittest/assert/StringPrimitiveValue.class.php @@ -2,6 +2,8 @@ class StringPrimitiveValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return strlen($value) === $size;; }, diff --git a/src/main/php/unittest/assert/StringValue.class.php b/src/main/php/unittest/assert/StringValue.class.php index a932c66..8499f0d 100755 --- a/src/main/php/unittest/assert/StringValue.class.php +++ b/src/main/php/unittest/assert/StringValue.class.php @@ -2,6 +2,8 @@ class StringValue extends Value { + static function __static() { } + public function hasSize($size) { return $this->is(new Match( function($value) use($size) { return $value->length() === $size; }, diff --git a/src/main/php/unittest/assert/Value.class.php b/src/main/php/unittest/assert/Value.class.php index cbd1115..69b6ec4 100755 --- a/src/main/php/unittest/assert/Value.class.php +++ b/src/main/php/unittest/assert/Value.class.php @@ -26,6 +26,11 @@ static function __static() { ; } + /** + * Creates a new instance + * + * @param var $value + */ public function __construct($value) { $this->value= $value; } From a03d6f2018b44f9f475b72a06c3e8906b555b86c Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 13 Oct 2014 21:43:29 +0200 Subject: [PATCH 3/3] Upgrade to use XP 6.0.0 beta 1 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2a07d49..081672a 100755 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ php: before_script: - wget 'https://github.com/xp-framework/xp-runners/releases/download/v5.0.0/setup' -O - | php - - wget 'https://github.com/xp-framework/core/releases/download/v6.0.0alpha6/xp-rt-6.0.0alpha6.xar' + - wget 'https://github.com/xp-framework/core/releases/download/v6.0.0beta1/xp-rt-6.0.0beta1.xar' - ls -1 *.xar > boot.pth - echo "[runtime]" >> xp.ini - echo "date.timezone=Europe/Berlin" >> xp.ini