-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_test.go
239 lines (230 loc) · 4.75 KB
/
print_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package phpdoc_test
import (
"strings"
"testing"
"mibk.dev/phpdoc"
)
var printTests = []struct {
name string
test string
}{
{"basic", `
/**
@param array & $x
@param string& ... $bar
@return ? float
*/
----
/**
* @param array &$x
* @param string &...$bar
* @return ?float
*/
`},
{"oneline", `
/**@var \ DateTime $date */
----
/** @var \DateTime $date */
`},
{"single line", `
/**
@var \ Traversable*/
----
/**
* @var \Traversable
*/
`},
{"more params", `
/**
@author Name <not known>
@param DateTime | string|null $bar Must be from this century
@param mixed $foo
@param bool... $opts
*@return float Always positive
*/
----
/**
* @author Name <not known>
* @param DateTime|string|null $bar Must be from this century
* @param mixed $foo
* @param bool ...$opts
* @return float Always positive
*/
`},
{"tags and text", `
/**
This function does
* * this and
* * that.
* @author Jack
It's deprecated now.
@deprecated Don't use
@return bool
@throws \ InvalidArgumentException
@implements Comparable <self ,>
@extends \ DateTime
@uses \ HandyOne
*/
----
/**
* This function does
* * this and
* * that.
*
* @author Jack
* It's deprecated now.
*
* @deprecated Don't use
* @return bool
* @throws \InvalidArgumentException
* @implements Comparable<self>
* @extends \DateTime
* @uses \HandyOne
*/
`},
{"properties", `
/**
@property \ Foo $a Foo
@property-read array<int,string> $b bar
@property-write int [] $c
@property array {0 :int ,foo?:\ Foo ,}$d
*/
----
/**
* @property \Foo $a Foo
* @property-read array<int, string> $b bar
* @property-write int[] $c
* @property array{0: int, foo?: \Foo} $d
*/
`},
{"array shapes", `
/**
@var array{0 ? :int, one :string,}
@var array{'foo': string ,'bar\'' ?: string}
*/
----
/**
* @var array{0?: int, one: string}
* @var array{'foo': string, 'bar\''?: string}
*/
`},
{"object shapes", `
/**
@var object {name :string ,role ?: int ,}
@return object
*/
----
/**
* @var object{name: string, role?: int}
* @return object
*/
`},
{"template", `
/**
@template T foo
@template U of \ Traversable bar
@template WW as \ Countable */
----
/**
* @template T foo
* @template U of \Traversable bar
* @template WW of \Countable
*/
`},
{"callable", `
/**
@param callable$m
@param callable(): string $k
@param callable ( int $a ,string& ...$b, ): string |int$x
@param callable (bool ,string ) $n
@param callable (string$s='false' ,bool $b =true, ) $nn
@param callable( ) :$this$m
@param int... $y
@return callable ( int $a , int $b=3 ) :void
*/
----
/**
* @param callable $m
* @param callable(): string $k
* @param callable(int $a, string &...$b): string|int $x
* @param callable(bool, string) $n
* @param callable(string $s = 'false', bool $b = true) $nn
* @param callable(): $this $m
* @param int ...$y
* @return callable(int $a, int $b = 3): void
*/
`},
{"method tag", `
/**
@method ? \ DateTime getDate( int| string$c ,) the date of x
@method translate (mixed& ... $args)does that for y
@method static void clean ( )
*/
----
/**
* @method ?\DateTime getDate(int|string $c) the date of x
* @method translate(mixed &...$args) does that for y
* @method static void clean()
*/
`},
{"const exprs", `
/**
@param self :: ALL_*$a
@param self::ANY_* [] $xx
@param value-of < That :: ConstVal >$b
@return BAR :: *
*/
----
/**
* @param self::ALL_* $a
* @param self::ANY_*[] $xx
* @param value-of<That::ConstVal> $b
* @return BAR::*
*/
`},
{"types", `
/**
@phpstan-type Foo array{ 'bar' :string } For sure
@param Foo [] $foos
*/
----
/**
* @phpstan-type Foo array{'bar': string} For sure
* @param Foo[] $foos
*/
`},
{"string lit", `
/**
@param 'foo' | 7 | 'bar' [] $xyz
*/
----
/**
* @param 'foo'|7|'bar'[] $xyz
*/
`},
}
func TestPrinting(t *testing.T) {
for _, tt := range printTests {
t.Run(tt.name, func(t *testing.T) {
s := strings.Split(tt.test, "----\n")
if len(s) != 2 {
t.Fatal("invalid test format")
}
input, want := s[0], s[1]
printerTestCase(t, input, want)
})
}
}
func printerTestCase(t *testing.T, input, want string) {
doc, err := phpdoc.Parse(strings.NewReader(input))
if err != nil {
t.Fatal(err)
}
got := new(strings.Builder)
if err := phpdoc.Fprint(got, doc); err != nil {
t.Fatalf("printing: unexpected err: %v", err)
}
if got.String() != want {
t.Errorf("\n got: %s\nwant: %s", got, want)
}
}