summaryrefslogtreecommitdiff
path: root/test/plugins/ffi/parser.lua
blob: 983b64c318026c82afd1066f58b27e6596204a16 (plain)
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
local utility = require 'utility'
local cdriver = require 'plugins.ffi.c-parser.cdriver'

rawset(_G, 'TEST', true)
local ctypes = require 'plugins.ffi.c-parser.ctypes'
ctypes.TESTMODE = true

function TEST(wanted, full)
    return function (script)
        local rrr = cdriver.process_context(script .. "$EOF$")
        assert(rrr)
        if full then
            for i, v in ipairs(rrr) do
                assert(utility.equal(v, wanted[i]))
            end
        else
            assert(utility.equal(rrr[1], wanted))
        end
    end
end


TEST {
    name = 'union@a',
    type = {
        name = 'a',
        type = 'union',
        fields = {
            { name = 'b', type = { 'int' } },
            { name = 'c', type = { 'int8_t' } }
        }
    }
} [[
    union a{
        int b;
        int8_t c;
    };
]]

TEST {
    name = 'union@a',
    type = {
        name = 'a',
        type = 'union',
    }
} [[
    union a{};
]]

TEST {
    name = 'enum@anonymous',
    type = {
        type = 'enum',
        values = {
            { name = 'a', value = { '1' } },
            { name = 'b', value = { 'a' } },
        }
    }
} [[
    enum {
        a = 1,
        b = a,
    };
]]

TEST {
    name = 'enum@a',
    type = {
        name = 'a',
        type = 'enum',
        values = {
            { name = 'b', value = { op = '|', { '1' }, { '2' } } },
        }
    }
} [[
    enum a{
        b = 1|2,
    };
]]

TEST {
    name = 'enum@a',
    type = {
        name = 'a',
        type = 'enum',
    }
} [[
    enum a{};
]]

TEST {
    name = 'struct@a',
    type = {
        name = 'a',
        type = 'struct',
        fields = {
            { name = 'f', type = { 'int' } },
            { name = 'b', type = { 'int', '*', '*' } }
        }
    }
} [[
    struct a {int f,**b;};
]]

TEST {
    name = 'struct@a',
    type = {
        name = 'a',
        type = 'struct',
        fields = {
            { name = 'f', type = { 'int' } },
        }
    }
} [[
    struct a {int f;};
]]

TEST({
    { name = "struct@anonymous", type = { type = 'struct' } },
    {
        name = 'a',
        type = {
            name = 'a',
            type = 'typedef',
            def = {
                { type = 'struct', }
            }
        }
    }
}, true) [[
    typedef struct {} a;
]]

TEST {
    name = 'a',
    type = {
        name = 'a',
        type = 'function',
        params = {
            { type = { 'int' }, name = 'b' },
        },
        ret = {
            type = { 'int' }
        },
        vararg = false
    },

} [[
    int a(int b);
]]