Skip to content

Enums wip #1

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

Closed
wants to merge 5 commits into from
Closed
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
12 changes: 0 additions & 12 deletions Zend/tests/enum/013.phpt

This file was deleted.

20 changes: 0 additions & 20 deletions Zend/tests/enum/021.phpt

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions Zend/tests/enum/constant-aliases.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
Enum constants can alias cases
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Foo {
case Bar;
const Other = self::Bar;
const Again = static::Bar;
}

function test(Foo $var) {
print "works";
}

test(Foo:Other);
test(Foo:Again);

?>
--EXPECTF--
works
works
18 changes: 18 additions & 0 deletions Zend/tests/enum/constants.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Enum allows constants
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Foo {
const BAR = 'Bar';
}

print Foo::BAR;

?>
--EXPECTF--
Bar
File renamed without changes.
22 changes: 22 additions & 0 deletions Zend/tests/enum/enum-as-constant.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Enum cases can be referenced by constants
--FILE--
<?php

enum Foo {
case Bar;
}

const Beep = Foo::Bar;

class Test {
const Beep = Foo::Bar;
}

var_dump(Beep);
var_dump(Test::Beep);

?>
--EXPECTF--
enum(Foo::Bar)
enum(Foo::Bar)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Enum type hints
Enum types as parameters
--FILE--
<?php

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions Zend/tests/enum/is_enum.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
is_enum
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Foo {
case Bar;
}

class Baz {}

$enum_var = Foo::Bar;
$not_enum_var = 'narf';

// I'm not sure this one is correct?
var_dump(is_enum(Foo::class));

var_dump(is_enum(Foo::Bar::class));
var_dump(is_enum(Baz::class));

var_dump(is_enum('beep'));
var_dump(is_enum($enum_var));
var_dump(is_enum($not_enum_var));

?>
--EXPECT--
bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(false)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions Zend/tests/enum/scalar-cases.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Scalar enums can list cases
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Suit: string {
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}

var_dump(Suit::cases());

?>
--EXPECTF--
array(4) {
["H"]=>
enum(Suit::Hearts)
["D"]=>
enum(Suit::Diamonds)
["C"]=>
enum(Suit::Clubs)
["S"]=>
enum(Suit::Spades)
}
21 changes: 21 additions & 0 deletions Zend/tests/enum/scalar-from-invalid.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Scalar enums reject upcasting from invalid input
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Suit: string {
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}

var_dump(Suit::from('A'));

?>
--EXPECTF--
Fatal Error: 'A' is not a valid scalar value for 'Suit' enumeration in %s on line %d
40 changes: 40 additions & 0 deletions Zend/tests/enum/scalar-from.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Scalar enums can be upcast from a scalar
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Suit: string {
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
}

var_dump(Suit::from('H'));
var_dump(Suit::from('D'));
var_dump(Suit::from('C'));
var_dump(Suit::from('S'));

enum Foo: int {
case Bar = 1;
case Baz = 2;
case Beep = 3;
}

var_dump(Foo::from(1));
var_dump(Foo::from(2));
var_dump(Foo::from(3));

?>
--EXPECTF--
enum(Suit::Hearts)
enum(Suit::Diamonds)
enum(Suit::Clubs)
enum(Suit::Spades)
enum(Foo::Bar)
enum(Foo::Baz)
enum(Foo::Beep)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions Zend/tests/enum/static-methods.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Enum supports static methods
--FILE--
<?php

enum Size {
case Small;
case Medium;
case Large;

public static function fromLength(int $cm) {
return match(true) {
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::Large,
};
}
}

var_dump(Size::fromLength(23));
var_dump(Size::fromLength(63));
var_dump(Size::fromLength(123));

?>
--EXPECTF--
enum(Size::Small)
enum(Size::Medium)
enum(Size::Large)
29 changes: 29 additions & 0 deletions Zend/tests/enum/traits-no-properties.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Enum cannot have properties, even via traits
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

trait Rectangle {
protected string $shape = "Rectangle";

public function shape(): string {
return $this->shape;
}
}

enum Suit {
use Rectangle;

case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

?>
--EXPECTF--
Fatal error: Traits used in enumerations may not contain properties.
31 changes: 31 additions & 0 deletions Zend/tests/enum/traits.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
Enum can use traits
--FILE--
<?php

trait Rectangle {
public function shape(): string {
return "Rectangle";
}
}

enum Suit {
use Rectangle;

case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

print Suit::Hearts->shape() . PHP_EOL;
print Suit::Diamonds->shape() . PHP_EOL;
print Suit::Clubs->shape() . PHP_EOL;
print Suit::Spades->shape() . PHP_EOL;

?>
--EXPECTF--
Rectangle
Rectangle
Rectangle
Rectangle
30 changes: 30 additions & 0 deletions Zend/tests/enum/unit-cases.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Unit enums can list cases
--SKIPIF--
<?php
die("skip, not yet implemented");
?>
--FILE--
<?php

enum Suit {
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}

var_dump(Suit::cases());

?>
--EXPECTF--
array(4) {
[0]=>
enum(Suit::Hearts)
[1]=>
enum(Suit::Diamonds)
[2]=>
enum(Suit::Clubs)
[3]=>
enum(Suit::Spades)
}
File renamed without changes.
File renamed without changes.