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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
package documentation_tester
import "core:os"
import "core:io"
import "core:fmt"
import "core:strings"
import "core:odin/ast"
import "core:odin/parser"
import "core:c/libc"
import doc "core:odin/doc-format"
Example_Test :: struct {
entity_name: string,
package_name: string,
example_code: []string,
expected_output: []string,
}
g_header: ^doc.Header
g_bad_doc: bool
g_examples_to_verify: [dynamic]Example_Test
g_path_to_odin: string
array :: proc(a: $A/doc.Array($T)) -> []T {
return doc.from_array(g_header, a)
}
str :: proc(s: $A/doc.String) -> string {
return doc.from_string(g_header, s)
}
common_prefix :: proc(strs: []string) -> string {
if len(strs) == 0 {
return ""
}
n := max(int)
for str in strs {
n = min(n, len(str))
}
prefix := strs[0][:n]
for str in strs[1:] {
for len(prefix) != 0 && str[:len(prefix)] != prefix {
prefix = prefix[:len(prefix)-1]
}
if len(prefix) == 0 {
break
}
}
return prefix
}
errorf :: proc(format: string, args: ..any) -> ! {
fmt.eprintf("%s ", os.args[0])
fmt.eprintf(format, ..args)
fmt.eprintln()
os.exit(1)
}
main :: proc() {
if len(os.args) != 2 {
errorf("expected path to odin executable")
}
g_path_to_odin = os.args[1]
data, ok := os.read_entire_file("all.odin-doc")
if !ok {
errorf("unable to read file: all.odin-doc")
}
err: doc.Reader_Error
g_header, err = doc.read_from_bytes(data)
switch err {
case .None:
case .Header_Too_Small:
errorf("file is too small for the file format")
case .Invalid_Magic:
errorf("invalid magic for the file format")
case .Data_Too_Small:
errorf("data is too small for the file format")
case .Invalid_Version:
errorf("invalid file format version")
}
pkgs := array(g_header.pkgs)
entities := array(g_header.entities)
path_prefix: string
{
fullpaths: [dynamic]string
defer delete(fullpaths)
for pkg in pkgs[1:] {
append(&fullpaths, str(pkg.fullpath))
}
path_prefix = common_prefix(fullpaths[:])
}
for pkg in pkgs[1:] {
entries_array := array(pkg.entries)
fullpath := str(pkg.fullpath)
path := strings.trim_prefix(fullpath, path_prefix)
if ! strings.has_prefix(path, "core/") {
continue
}
trimmed_path := strings.trim_prefix(path, "core/")
if strings.has_prefix(trimmed_path, "sys") {
continue
}
if strings.contains(trimmed_path, "/_") {
continue
}
for entry in entries_array {
entity := entities[entry.entity]
find_and_add_examples(
docs = str(entity.docs),
package_name = str(pkg.name),
entity_name = str(entity.name),
)
}
}
write_test_suite(g_examples_to_verify[:])
if g_bad_doc {
errorf("We created bad documentation!")
}
if ! run_test_suite() {
errorf("Test suite failed!")
}
fmt.println("Examples verified")
}
// NOTE: this is a pretty close copy paste from the website pkg documentation on parsing the docs
find_and_add_examples :: proc(docs: string, package_name: string, entity_name: string) {
if docs == "" {
return
}
Block_Kind :: enum {
Other,
Example,
Output,
}
Block :: struct {
kind: Block_Kind,
lines: []string,
}
lines := strings.split_lines(docs)
curr_block_kind := Block_Kind.Other
start := 0
example_block: Block // when set the kind should be Example
output_block: Block // when set the kind should be Output
// rely on zii that the kinds have not been set
assert(example_block.kind != .Example)
assert(output_block.kind != .Output)
insert_block :: proc(block: Block, example: ^Block, output: ^Block, name: string) {
switch block.kind {
case .Other:
case .Example:
if example.kind == .Example {
fmt.eprintf("The documentation for %q has multiple examples which is not allowed\n", name)
g_bad_doc = true
}
example^ = block
case .Output: output^ = block
if example.kind == .Output {
fmt.eprintf("The documentation for %q has multiple output which is not allowed\n", name)
g_bad_doc = true
}
output^ = block
}
}
for line, i in lines {
text := strings.trim_space(line)
next_block_kind := curr_block_kind
switch curr_block_kind {
case .Other:
switch {
case strings.has_prefix(line, "Example:"): next_block_kind = .Example
case strings.has_prefix(line, "Output:"): next_block_kind = .Output
}
case .Example:
switch {
case strings.has_prefix(line, "Output:"): next_block_kind = .Output
case ! (text == "" || strings.has_prefix(line, "\t")): next_block_kind = .Other
}
case .Output:
switch {
case strings.has_prefix(line, "Example:"): next_block_kind = .Example
case ! (text == "" || strings.has_prefix(line, "\t")): next_block_kind = .Other
}
}
if i-start > 0 && (curr_block_kind != next_block_kind) {
insert_block(Block{curr_block_kind, lines[start:i]}, &example_block, &output_block, entity_name)
curr_block_kind, start = next_block_kind, i
}
}
if start < len(lines) {
insert_block(Block{curr_block_kind, lines[start:]}, &example_block, &output_block, entity_name)
}
if output_block.kind == .Output && example_block.kind != .Example {
fmt.eprintf("The documentation for %q has an output block but no example\n", entity_name)
g_bad_doc = true
}
// Write example and output block if they're both present
if example_block.kind == .Example && output_block.kind == .Output {
{
// Example block starts with
// `Example:` and a number of white spaces,
lines := &example_block.lines
for len(lines) > 0 && (strings.trim_space(lines[0]) == "" || strings.has_prefix(lines[0], "Example:")) {
lines^ = lines[1:]
}
}
{
// Output block starts with
// `Output:` and a number of white spaces,
lines := &output_block.lines
for len(lines) > 0 && (strings.trim_space(lines[0]) == "" || strings.has_prefix(lines[0], "Output:")) {
lines^ = lines[1:]
}
// Additionally we need to strip all empty lines at the end of output to not include those in the expected output
for len(lines) > 0 && (strings.trim_space(lines[len(lines) - 1]) == "") {
lines^ = lines[:len(lines) - 1]
}
}
// Remove first layer of tabs which are always present
for line in &example_block.lines {
line = strings.trim_prefix(line, "\t")
}
for line in &output_block.lines {
line = strings.trim_prefix(line, "\t")
}
append(&g_examples_to_verify, Example_Test {
entity_name = entity_name,
package_name = package_name,
example_code = example_block.lines,
expected_output = output_block.lines,
})
}
}
write_test_suite :: proc(example_tests: []Example_Test) {
TEST_SUITE_DIRECTORY :: "verify"
os.remove_directory(TEST_SUITE_DIRECTORY)
os.make_directory(TEST_SUITE_DIRECTORY)
example_build := strings.builder_make()
test_runner := strings.builder_make()
strings.write_string(&test_runner,
`//+private
package documentation_verification
import "core:os"
import "core:mem"
import "core:io"
import "core:fmt"
import "core:thread"
import "core:sync"
import "core:intrinsics"
@(private="file")
_read_pipe: os.Handle
@(private="file")
_write_pipe: os.Handle
@(private="file")
_pipe_reader_semaphore: sync.Sema
@(private="file")
_out_data: string
@(private="file")
_out_buffer: [mem.Megabyte]byte
@(private="file")
_bad_test_found: bool
@(private="file")
_spawn_pipe_reader :: proc() {
thread.create_and_start(proc(^thread.Thread) {
stream := os.stream_from_handle(_read_pipe)
reader := io.to_reader(stream)
sync.post(&_pipe_reader_semaphore) // notify thread is ready
for {
n_read := 0
read_to_null_byte := 0
finished_reading := false
for ! finished_reading {
just_read, err := io.read(reader, _out_buffer[n_read:], &n_read); if err != .None {
panic("We got an IO error!")
}
for b in _out_buffer[n_read - just_read: n_read] {
if b == 0 {
finished_reading = true
break
}
read_to_null_byte += 1
}
}
intrinsics.volatile_store(&_out_data, transmute(string)_out_buffer[:read_to_null_byte])
sync.post(&_pipe_reader_semaphore) // notify we read the null byte
}
})
sync.wait(&_pipe_reader_semaphore) // wait for thread to be ready
}
@(private="file")
_check :: proc(test_name: string, expected: string) {
null_byte: [1]byte
os.write(_write_pipe, null_byte[:])
os.flush(_write_pipe)
sync.wait(&_pipe_reader_semaphore)
output := intrinsics.volatile_load(&_out_data) // wait for thread to read null byte
if expected != output {
fmt.eprintf("Test %q got unexpected output:\n%q\n", test_name, output)
fmt.eprintf("Expected:\n%q\n", expected)
_bad_test_found = true
}
}
main :: proc() {
_read_pipe, _write_pipe, _ = os.pipe()
os.stdout = _write_pipe
_spawn_pipe_reader()
`)
for test in example_tests {
strings.builder_reset(&example_build)
strings.write_string(&example_build, "package documentation_verification\n\n")
for line in test.example_code {
strings.write_string(&example_build, line)
strings.write_byte(&example_build, '\n')
}
code_string := strings.to_string(example_build)
example_ast := ast.File { src = code_string }
odin_parser := parser.default_parser()
if ! parser.parse_file(&odin_parser, &example_ast) {
g_bad_doc = true
continue
}
if odin_parser.error_count > 0 {
fmt.eprintf("Errors on the following code generated for %q:\n%v\n", test.entity_name, code_string)
g_bad_doc = true
continue
}
enforced_name := fmt.tprintf("%v_example", test.entity_name)
index_of_proc_name: int
code_test_name: string
for d in example_ast.decls {
value_decl, is_value := d.derived.(^ast.Value_Decl); if ! is_value {
continue
}
if len(value_decl.values) != 1 {
continue
}
proc_lit, is_proc_lit := value_decl.values[0].derived_expr.(^ast.Proc_Lit); if ! is_proc_lit {
continue
}
if len(proc_lit.type.params.list) > 0 {
continue
}
this_procedure_name := code_string[value_decl.names[0].pos.offset:value_decl.names[0].end.offset]
if this_procedure_name != enforced_name {
continue
}
index_of_proc_name = value_decl.names[0].pos.offset
code_test_name = this_procedure_name
break
}
if code_test_name == "" {
fmt.eprintf("We could not any find procedure literals with no arguments with the identifier %q for the example for %q\n", enforced_name, test.entity_name)
g_bad_doc = true
continue
}
fmt.sbprintf(&test_runner, "\t%v_%v()\n", test.package_name, code_test_name)
fmt.sbprintf(&test_runner, "\t_check(%q, `", code_test_name)
for line in test.expected_output {
strings.write_string(&test_runner, line)
strings.write_string(&test_runner, "\n")
}
strings.write_string(&test_runner, "`)\n")
save_path := fmt.tprintf("verify/test_%v_%v.odin", test.package_name, code_test_name)
test_file_handle, err := os.open(save_path, os.O_WRONLY | os.O_CREATE); if err != 0 {
fmt.eprintf("We could not open the file to the path %q for writing\n", save_path)
g_bad_doc = true
continue
}
defer os.close(test_file_handle)
stream := os.stream_from_handle(test_file_handle)
writer, ok := io.to_writer(stream); if ! ok {
fmt.eprintf("We could not make the writer for the path %q\n", save_path)
g_bad_doc = true
continue
}
fmt.wprintf(writer, "%v%v_%v", code_string[:index_of_proc_name], test.package_name, code_string[index_of_proc_name:])
}
strings.write_string(&test_runner,
`
if _bad_test_found {
fmt.eprintln("One or more tests failed")
os.exit(1)
}
}`)
os.write_entire_file("verify/main.odin", transmute([]byte)strings.to_string(test_runner))
}
run_test_suite :: proc() -> bool {
return libc.system(fmt.caprintf("%v run verify", g_path_to_odin)) == 0
}
|