Skip to content

Commit 7b2686f

Browse files
committed
Check XP core for nullable support and change assertions
Fixes issue #103
1 parent 1ccdc1d commit 7b2686f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed issue #103: Nullable types - @thekid
7+
68
## 6.1.0 / 2021-01-04
79

810
* Included languages and emitters in `xp compile` output - @thekid
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace lang\ast\unittest\emit;
2+
3+
use lang\{Nullable, Type};
4+
5+
trait NullableSupport {
6+
7+
/** Creates a nullable type if XP core supports it */
8+
private function nullable(Type $t): Type {
9+
static $support= null;
10+
11+
return ($support ?? $support= class_exists(Nullable::class)) ? new Nullable($t) : $t;
12+
}
13+
}

src/test/php/lang/ast/unittest/emit/ParameterTest.class.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use unittest\{Assert, Test, Values, Action};
66

77
class ParameterTest extends EmittingTest {
8+
use NullableSupport;
89

910
/**
1011
* Helper to declare a type and return a parameter reflection object
@@ -49,12 +50,12 @@ public function value_typed() {
4950

5051
#[Test]
5152
public function value_type_with_null() {
52-
Assert::equals(new XPClass(Value::class), $this->param('Value $param= null')->getType());
53+
Assert::equals($this->nullable(new XPClass(Value::class)), $this->param('Value $param= null')->getType());
5354
}
5455

5556
#[Test]
5657
public function nullable_value_type() {
57-
Assert::equals(new XPClass(Value::class), $this->param('?Value $param')->getType());
58+
Assert::equals($this->nullable(new XPClass(Value::class)), $this->param('?Value $param')->getType());
5859
}
5960

6061
#[Test]
@@ -64,17 +65,17 @@ public function string_typed() {
6465

6566
#[Test]
6667
public function string_typed_with_null() {
67-
Assert::equals(Primitive::$STRING, $this->param('string $param= null')->getType());
68+
Assert::equals($this->nullable(Primitive::$STRING), $this->param('string $param= null')->getType());
6869
}
6970

7071
#[Test]
7172
public function nullable_string_type() {
72-
Assert::equals(Primitive::$STRING, $this->param('?string $param')->getType());
73+
Assert::equals($this->nullable(Primitive::$STRING), $this->param('?string $param')->getType());
7374
}
7475

7576
#[Test, Action(eval: 'new RuntimeVersion(">=7.1")')]
7677
public function nullable_string_type_restriction() {
77-
Assert::equals(Primitive::$STRING, $this->param('?string $param')->getTypeRestriction());
78+
Assert::equals($this->nullable(Primitive::$STRING), $this->param('?string $param')->getTypeRestriction());
7879
}
7980

8081
#[Test]

0 commit comments

Comments
 (0)