Skip to content

Commit ae9ceb8

Browse files
committed
Add support for readonly
See #7 (comment)
1 parent 7d584c4 commit ae9ceb8

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ XP records for PHP - ChangeLog
33

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

6+
## 2.3.0 / 2022-02-05
7+
8+
* Add support for `readonly` record parameters. See issue #7 - @thekid
9+
610
## 2.2.0 / 2022-02-03
711

812
* Merged PR #8: Replace `__init()` function with `init { }` blocks

src/test/php/lang/ast/syntax/php/unittest/RecordsTest.class.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ public function __construct() { }
3434
}');
3535
}
3636

37+
#[Test]
38+
public function fields_are_private_by_default() {
39+
$t= $this->type('record <T>(string $name) { }');
40+
Assert::equals(MODIFIER_PRIVATE, $t->getField('name')->getModifiers());
41+
}
42+
43+
#[Test]
44+
public function can_declare_field_modifiers() {
45+
$t= $this->type('record <T>(public string $name) { }');
46+
Assert::equals(MODIFIER_PUBLIC, $t->getField('name')->getModifiers());
47+
}
48+
49+
#[Test]
50+
public function can_have_readonly_fields() {
51+
$t= $this->type('record <T>(public readonly string $name) { }');
52+
Assert::equals(MODIFIER_PUBLIC | MODIFIER_READONLY, $t->getField('name')->getModifiers());
53+
}
54+
55+
#[Test, Expect(class: Error::class, withMessage: '/Cannot modify readonly property .+name/')]
56+
public function writing_to_readonly_field() {
57+
$t= $this->type('record <T>(public readonly string $name) { }');
58+
$t->newInstance('Test')->name= 'Modified';
59+
}
60+
3761
#[Test]
3862
public function point_record() {
3963
$p= $this->type('record <T>(int $x, int $y) { }')->newInstance(1, 10);

0 commit comments

Comments
 (0)