Skip to content

ADTs #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

ADTs #141

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Zend/tests/enum/adt/argument_count_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
ADT argument count error
--FILE--
<?php

enum E {
case C($x, $y);
}

function test($c) {
try {
var_dump($c());
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
}

test(fn() => E::C());
test(fn() => E::C(1));
test(fn() => E::C(1, 2));
test(fn() => E::C(1, 2, 3));
test(fn() => E::C(x: 1));
test(fn() => E::C(y: 2));
test(fn() => E::C(x: 1, y: 2));
test(fn() => E::C(y: 2, x: 1));
test(fn() => E::C(1, 2, z: 3));

?>
--EXPECT--
ArgumentCountError: E::C() expects exactly 2 arguments, 0 given
ArgumentCountError: E::C() expects exactly 2 arguments, 1 given
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
ArgumentCountError: E::C() expects exactly 2 arguments, 3 given
ArgumentCountError: E::C() expects exactly 2 arguments, 1 given
ArgumentCountError: E::C(): Argument #1 ($x) not passed
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
Error: Unknown named parameter $z
65 changes: 65 additions & 0 deletions Zend/tests/enum/adt/argument_type_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
--TEST--
ADT argument type error
--FILE--
<?php

enum E {
case C(int $x, int $y);
}

function test($c) {
try {
var_dump($c());
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
}

test(fn() => E::C(1, 2));

test(fn() => E::C('1', 2));
test(fn() => E::C('1.1', 2));
test(fn() => E::C([], []));

test(fn() => E::C(1, '2'));
test(fn() => E::C(1, '2.2'));
test(fn() => E::C(1, []));

?>
--EXPECTF--
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}

Deprecated: Implicit conversion from float-string "1.1" to int loses precision in %s on line %d
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
TypeError: E::C(): Argument #1 ($x) must be of type int, array given, called in %s on line %d
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}

Deprecated: Implicit conversion from float-string "2.2" to int loses precision in %s on line %d
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
TypeError: E::C(): Argument #2 ($y) must be of type int, array given, called in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/enum/adt/assoc_values_on_backed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Backed enum must not have assoc values
--FILE--
<?php

enum E: int {
case A($value) = 42;
}

?>
--EXPECTF--
Fatal error: Case A of backed enum E must not have associated values in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/enum/adt/ast-dumper.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
ADT AST dumper
--FILE--
<?php

try {
assert(false && (function () {
enum Foo {
case Bar($x, int $y);
}
}));
} catch (Error $e) {
echo $e->getMessage();
}

?>
--EXPECT--
assert(false && function () {
enum Foo {
case Bar($x, int $y);
}

})
44 changes: 44 additions & 0 deletions Zend/tests/enum/adt/basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
--TEST--
Basic ADT
--FILE--
<?php

enum E {
case A;
case B($x);
case C(mixed $x);
case D(string $x, int $y);
}

var_dump(E::A);

$b = E::B(42);
var_dump($b, $b->x);

$c = E::C('foo');
var_dump($c, $c->x);

$d = E::D('bar', 43);
var_dump($d, $d->x, $d->y);

?>
--EXPECT--
enum(E::A)
enum(E::B) (1) {
["x"]=>
int(42)
}
int(42)
enum(E::C) (1) {
["x"]=>
string(3) "foo"
}
string(3) "foo"
enum(E::D) (2) {
["x"]=>
string(3) "bar"
["y"]=>
int(43)
}
string(3) "bar"
int(43)
12 changes: 12 additions & 0 deletions Zend/tests/enum/adt/ctor_cpp.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
ADT assoc value must not be promoted
--FILE--
<?php

enum E {
case A(public $x);
}

?>
--EXPECTF--
Fatal error: Associated value $x of enum case E::A must not be promoted in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/enum/adt/ctor_hooks.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
ADT assoc value must not contain hooks
--FILE--
<?php

enum E {
case A($x { get => 42; });
}

?>
--EXPECTF--
Fatal error: Associated value $x of enum case E::A must not be promoted in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/enum/adt/ctor_ref.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
ADT assoc value must not be pass-by-reference
--FILE--
<?php

enum E {
case A(&$x);
}

?>
--EXPECTF--
Fatal error: Associated value $x of enum case E::A must not be pass-by-reference in %s on line %d
12 changes: 12 additions & 0 deletions Zend/tests/enum/adt/ctor_variadics.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
ADT ctor must not contain variadics
--FILE--
<?php

enum E {
case A(...$args);
}

?>
--EXPECTF--
Fatal error: Associated value $args of enum case E::A must not be variadic in %s on line %d
26 changes: 26 additions & 0 deletions Zend/tests/enum/adt/identity.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ADT identity
--FILE--
<?php

enum E {
case A;
case B($x);
case C($x);
}

var_dump(E::A === E::A);
var_dump(E::A === E::B(1));
var_dump(E::B(1) === E::B(1));
var_dump(E::B(1) === E::B(2));
var_dump(E::B(1) === E::C(1));
var_dump(E::B(1) === E::C(2));

?>
--EXPECT--
bool(true)
bool(false)
bool(true)
bool(false)
bool(false)
bool(false)
21 changes: 21 additions & 0 deletions Zend/tests/enum/adt/instanceof.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
ADT instanceof
--FILE--
<?php

enum Option {
case None();
case Some($value);
}

var_dump(Option::None() instanceof Option::None);
var_dump(Option::None() instanceof Option::Some);
var_dump(Option::Some(42) instanceof Option::None);
var_dump(Option::Some(42) instanceof Option::Some);

?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(true)
17 changes: 17 additions & 0 deletions Zend/tests/enum/adt/match_error_message.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
ADTs in match error message
--FILE--
<?php

enum E {
case A($x);
}

match (E::A(42)) {};

?>
--EXPECTF--
Fatal error: Uncaught UnhandledMatchError: Unhandled match case E::A in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
53 changes: 53 additions & 0 deletions Zend/tests/enum/adt/parameter_count_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--TEST--
ADT parameter count validation
--FILE--
<?php

enum E {
case C($x, $y);
}

function test($c) {
try {
var_dump($c());
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
}

test(fn() => E::C());
test(fn() => E::C(1));
test(fn() => E::C(1, 2));
test(fn() => E::C(1, 2, 3));
test(fn() => E::C(x: 1));
test(fn() => E::C(y: 2));
test(fn() => E::C(x: 1, y: 2));
test(fn() => E::C(y: 2, x: 1));
test(fn() => E::C(1, 2, z: 3));

?>
--EXPECT--
ArgumentCountError: E::C() expects exactly 2 arguments, 0 given
ArgumentCountError: E::C() expects exactly 2 arguments, 1 given
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
ArgumentCountError: E::C() expects exactly 2 arguments, 3 given
ArgumentCountError: E::C() expects exactly 2 arguments, 1 given
ArgumentCountError: E::C(): Argument #1 ($x) not passed
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
enum(E::C) (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
Error: Unknown named parameter $z
Loading
Loading