2
2
3
3
use unittest \{Assert , Test , Values };
4
4
5
+ /**
6
+ * Tests for first-class callable syntax
7
+ *
8
+ * @see https://wiki.php.net/rfc/first_class_callable_syntax#proposal
9
+ */
5
10
class CallableSyntaxTest extends EmittingTest {
6
11
12
+ /**
13
+ * Verification helper
14
+ *
15
+ * @param string $code
16
+ * @return void
17
+ * @throws unittest.AssertionFailedError
18
+ */
19
+ private function verify ($ code ) {
20
+ Assert::equals (4 , $ this ->run ($ code )('Test ' ));
21
+ }
22
+
7
23
#[Test]
8
24
public function native_function () {
9
- $ f = $ this ->run ('class <T> {
25
+ $ this ->verify ('class <T> {
10
26
public function run() { return strlen(...); }
11
27
} ' );
12
-
13
- Assert::equals (4 , $ f ('Test ' ));
14
28
}
15
29
16
30
#[Test]
17
31
public function instance_method () {
18
- $ f = $ this ->run ('class <T> {
32
+ $ this ->verify ('class <T> {
19
33
public function length($arg) { return strlen($arg); }
20
34
public function run() { return $this->length(...); }
21
35
} ' );
22
-
23
- Assert::equals (4 , $ f ('Test ' ));
24
36
}
25
37
26
38
#[Test]
27
39
public function class_method () {
28
- $ f = $ this ->run ('class <T> {
40
+ $ this ->verify ('class <T> {
29
41
public static function length($arg) { return strlen($arg); }
30
42
public function run() { return self::length(...); }
31
43
} ' );
32
-
33
- Assert::equals (4 , $ f ('Test ' ));
34
44
}
35
45
36
46
#[Test]
37
47
public function private_method () {
38
- $ f = $ this ->run ('class <T> {
48
+ $ this ->verify ('class <T> {
39
49
private function length($arg) { return strlen($arg); }
40
50
public function run() { return $this->length(...); }
41
51
} ' );
42
-
43
- Assert::equals (4 , $ f ('Test ' ));
44
52
}
45
53
46
54
#[Test]
47
55
public function variable_function () {
48
- $ f = $ this ->run ('class <T> {
56
+ $ this ->verify ('class <T> {
49
57
public function run() {
50
58
$func= "strlen";
51
59
return $func(...);
52
60
}
53
61
} ' );
54
-
55
- Assert::equals (4 , $ f ('Test ' ));
56
62
}
57
63
58
64
#[Test]
59
65
public function instance_method_reference () {
60
- $ f = $ this ->run ('class <T> {
66
+ $ this ->verify ('class <T> {
61
67
private $func= "strlen";
62
68
public function run() {
63
69
return ($this->func)(...);
64
70
}
65
71
} ' );
66
-
67
- Assert::equals (4 , $ f ('Test ' ));
68
72
}
69
73
70
74
#[Test, Values(['$this->$func(...) ' , '$this->{$func}(...) ' ])]
71
75
public function variable_instance_method ($ expr ) {
72
- $ f = $ this ->run ('class <T> {
76
+ $ this ->verify ('class <T> {
73
77
private function length($arg) { return strlen($arg); }
74
78
public function run() {
75
79
$func= "length";
76
80
return ' .$ expr .';
77
81
}
78
82
} ' );
79
-
80
- Assert::equals (4 , $ f ('Test ' ));
81
83
}
82
84
83
85
#[Test, Values(['self::$func(...) ' , 'self::{$func}(...) ' ])]
84
86
public function variable_class_method ($ expr ) {
85
- $ f = $ this ->run ('class <T> {
87
+ $ this ->verify ('class <T> {
86
88
private static function length($arg) { return strlen($arg); }
87
89
public function run() {
88
90
$func= "length";
89
91
return ' .$ expr .';
90
92
}
91
93
} ' );
92
-
93
- Assert::equals (4 , $ f ('Test ' ));
94
94
}
95
95
96
96
#[Test]
97
97
public function variable_class_method_with_variable_class () {
98
- $ f = $ this ->run ('class <T> {
98
+ $ this ->verify ('class <T> {
99
99
private static function length($arg) { return strlen($arg); }
100
100
public function run() {
101
101
$func= "length";
102
102
$class= __CLASS__;
103
103
return $class::$func(...);
104
104
}
105
105
} ' );
106
-
107
- Assert::equals (4 , $ f ('Test ' ));
108
106
}
109
107
110
108
#[Test]
111
109
public function string_function_reference () {
112
- $ f = $ this ->run ('class <T> {
110
+ $ this ->verify ('class <T> {
113
111
public function run() { return "strlen"(...); }
114
112
} ' );
115
-
116
- Assert::equals (4 , $ f ('Test ' ));
117
113
}
118
114
119
115
#[Test]
120
116
public function array_instance_method_reference () {
121
- $ f = $ this ->run ('class <T> {
117
+ $ this ->verify ('class <T> {
122
118
public function length($arg) { return strlen($arg); }
123
119
public function run() { return [$this, "length"](...); }
124
120
} ' );
125
-
126
- Assert::equals (4 , $ f ('Test ' ));
127
121
}
128
122
129
123
#[Test]
130
124
public function array_class_method_reference () {
131
- $ f = $ this ->run ('class <T> {
125
+ $ this ->verify ('class <T> {
132
126
public static function length($arg) { return strlen($arg); }
133
127
public function run() { return [self::class, "length"](...); }
134
128
} ' );
135
-
136
- Assert::equals (4 , $ f ('Test ' ));
137
129
}
138
130
}
0 commit comments