~bigbes/tarantool

tarantool-protobuf

ref: c77ecc62ad571ab1f5abfae7cde31f006399804a tarantool-protobuf/test/doc_test.lua -rw-r--r-- 4.2 KiB
c77ecc62 — Eugene Blikh codegen: inline 1+2-byte varint fast path in packed-scalar decode inner loop (ozn) 2 months ago
                                                                                
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
-- Smoke test for protoc-gen-tarantool-doc.
--
-- Builds the doc plugin (if not already built), runs it against
-- examples/proto/hello.proto, and asserts the expected Markdown sections
-- and entries appear in the output.
local t = require('luatest')
local fio = require('fio')

local REPO_ROOT = fio.abspath(fio.pathjoin(
    fio.dirname(debug.getinfo(1, 'S').source:sub(2)), '..'))
local PROTO_DIR = fio.pathjoin(REPO_ROOT, 'examples', 'proto')
local OPTIONS_DIR = fio.pathjoin(REPO_ROOT, 'options')
local PLUGIN = fio.pathjoin(REPO_ROOT, 'protoc-gen-tarantool-doc')

local function slurp(path)
    local f = assert(io.open(path, 'rb'))
    local s = f:read('*a')
    f:close()
    return s
end

local function ensure_plugin()
    if fio.path.exists(PLUGIN) then return end
    local cmd = string.format('cd %q && go build -o %s ./cmd/protoc-gen-tarantool-doc',
                              REPO_ROOT, fio.basename(PLUGIN))
    assert(os.execute(cmd) == 0 or os.execute(cmd) == true,
           'failed to build doc plugin: ' .. cmd)
end

local OUT_DIR = fio.tempdir()
local OUT_FILE = fio.pathjoin(OUT_DIR, 'hello.md')

do
    ensure_plugin()
    local cmd = string.format(
        'protoc --plugin=%q --tarantool-doc_out=%q -I %q -I %q %q',
        PLUGIN, OUT_DIR, PROTO_DIR, OPTIONS_DIR,
        fio.pathjoin(PROTO_DIR, 'hello.proto'))
    local ok = os.execute(cmd)
    assert(ok == 0 or ok == true, 'doc plugin failed: ' .. cmd)
end

local g = t.group('doc')

g.test_header = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '# hello.proto')
    t.assert_str_contains(md, '**Package:** `hello`')
end

g.test_imports_listed = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '**Imports:**')
    t.assert_str_contains(md, '`google/protobuf/timestamp.proto`')
end

g.test_message_section = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '## Messages')
    t.assert_str_contains(md, '### `hello.Person`')
    t.assert_str_contains(md, '### `hello.Address`')
end

g.test_field_table = function()
    local md = slurp(OUT_FILE)
    -- Header row.
    t.assert_str_contains(md, '| # | Field | Type | Label | Description |')
    -- A scalar field.
    t.assert_str_contains(md, '| 1 | `name` | `string` |')
    -- An enum reference.
    t.assert_str_contains(md, '`hello.Status`')
    -- A WKT reference.
    t.assert_str_contains(md, '`google.protobuf.Timestamp`')
end

g.test_optional_label = function()
    local md = slurp(OUT_FILE)
    -- apartment is explicit-optional.
    t.assert_str_contains(md, '| 4 | `apartment` | `string` | optional |')
end

g.test_repeated_label = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '`lucky_numbers` | `int32` | repeated |')
end

g.test_oneof_label = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, 'oneof `outcome`')
end

g.test_map_field_type = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '`map<string, int32>`')
    t.assert_str_contains(md, '`map<string, hello.Address>`')
end

g.test_no_map_entry_message = function()
    -- Synthetic map entry messages must not appear as their own section.
    local md = slurp(OUT_FILE)
    t.assert_equals(md:find('AgesByNicknameEntry'), nil)
end

g.test_enum_table = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '## Enums')
    t.assert_str_contains(md, '### `hello.Status`')
    t.assert_str_contains(md, '| 0 | `UNKNOWN`')
    t.assert_str_contains(md, '| 2 | `ERROR`')
end

g.test_service_table = function()
    local md = slurp(OUT_FILE)
    t.assert_str_contains(md, '## Services')
    t.assert_str_contains(md, '### `hello.Greeter`')
    t.assert_str_contains(md, '| `SayHello` | `hello.HelloRequest` | `hello.HelloReply` | unary |')
    t.assert_str_contains(md, '| `Chat` | `hello.HelloRequest` | `hello.HelloReply` | bidi |')
    t.assert_str_contains(md, '| `StreamHellos` | `hello.HelloRequest` | `hello.HelloReply` | server |')
    t.assert_str_contains(md, '| `CollectHellos` | `hello.HelloRequest` | `hello.HelloReply` | client |')
end

g.test_leading_comment_preserved = function()
    local md = slurp(OUT_FILE)
    -- The leading comment on `apartment` should appear in the table row.
    t.assert_str_contains(md, 'Explicit-optional: presence is meaningful')
end