s3: Fix an uninitialized variable reference
[Samba.git] / source / pidl / tests / parse_idl.pl
blob4a6db4a924da8f28a694039f3144faaf1bb1c0f8
1 #!/usr/bin/perl
2 # Some simple tests for pidls parsing routines
3 # (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4 # Published under the GNU General Public License
5 use strict;
7 use Test::More tests => 65 * 2 + 7;
8 use FindBin qw($RealBin);
9 use lib "$RealBin";
10 use Util qw(test_errors);
11 use Parse::Pidl::IDL;
12 use Parse::Pidl::NDR;
14 sub testok($$)
16 my ($name, $data) = @_;
18 test_errors("", sub {
19 my $pidl = Parse::Pidl::IDL::parse_string($data, "<$name>");
20 ok (defined($pidl), $name);
21 });
24 sub testfail($$$)
26 my ($name, $data, $error) = @_;
28 test_errors($error, sub {
29 my $pidl = Parse::Pidl::IDL::parse_string($data, "<$name>");
31 ok ((not defined $pidl), $name);
32 });
35 testfail "unknowntag", "bla test {};",
36 "<unknowntag>:0: Syntax error near 'bla'\n";
37 testok "test1", "interface test { void Test(); }; ";
38 testok "voidtest", "interface test { int Testx(void); }; ";
39 testfail "voidtest", "interface test { Test(); }; ",
40 "<voidtest>:0: Syntax error near '('\n";
41 testok "argtest", "interface test { int Test(int a, long b, uint32 c); }; ";
42 testok "array1", "interface test { int Test(int a[]); };";
43 testok "array2", "interface test { int Test(int a[2]); };";
44 testok "array3", "interface test { int Test(int a[b]); };";
45 testfail "array4", "interface test { int Test(int[] a); };",
46 "<array4>:0: Syntax error near '['\n";
47 testok "ptr1", "interface test { int Test(int *a); };";
48 testok "ptr2", "interface test { int Test(int **a); };";
49 testok "ptr3", "interface test { int Test(int ***a); };";
50 testfail "empty1", "interface test { };", "<empty1>:0: Syntax error near '}'\n";
51 testfail "empty2", "", "";
52 testok "attr1", "[uuid(\"myuuid\"),attr] interface test { int Test(int ***a); };";
53 testok "attr2", "interface test { [public] int Test(); };";
54 testok "attr3", "[attr1] [attr2] interface test { [public] int Test(); };";
55 testok "multfn", "interface test { int test1(); int test2(); };";
56 testok "multif", "interface test { int test1(); }; interface test2 { int test2(); };";
57 testok "tdstruct1", "interface test { typedef struct { } foo; };";
58 testok "tdstruct2", "interface test { typedef struct { int a; } foo; };";
59 testok "tdstruct3", "interface test { typedef struct { int a; int b; } foo; };";
60 testfail "tdstruct4", "interface test { typedef struct { int a, int b; } foo; };",
61 "<tdstruct4>:0: Syntax error near ','\n";
62 testok "struct1", "interface test { struct x { }; };";
63 testok "struct2", "interface test { struct x { int a; }; };";
64 testok "struct3", "interface test { struct x { int a; int b; }; };";
65 testfail "struct4", "interface test { struct x { int a, int b; }; };",
66 "<struct4>:0: Syntax error near ','\n";
67 testfail "struct5", "interface test { struct { int a; } x; };",
68 "<struct5>:0: Syntax error near 'x'\n";
69 testok "tdunion1", "interface test { typedef union { } a; };";
70 testok "tdunion2", "interface test { typedef union { int a; } a; };";
71 testok "union1", "interface test { union a { }; };";
72 testok "union2", "interface test { union x { int a; }; };";
73 testfail "union3", "interface test { union { int a; } x; };",
74 "<union3>:0: Syntax error near 'x'\n";
75 testok "typedef1", "interface test { typedef int a; };";
76 testfail "typedef2", "interface test { typedef x; };",
77 "<typedef2>:0: Syntax error near ';'\n";
78 testok "tdenum1", "interface test { typedef enum { A=1, B=2, C} a; };";
79 testok "enum1", "interface test { enum a { A=1, B=2, C}; };";
80 testfail "enum2", "interface test { enum { A=1, B=2, C} a; };",
81 "<enum2>:0: Syntax error near 'a'\n";
82 testok "nested1", "interface test { struct x { struct { int a; } z; }; };";
83 testok "nested2", "interface test { struct x { struct y { int a; } z; }; };";
84 testok "bitmap1", "interface test { bitmap x { a=1 }; };";
85 testok "unsigned", "interface test { struct x { unsigned short y; }; };";
86 testok "struct-property", "interface test { [public] struct x { short y; }; };";
87 testok "signed", "interface test { struct x { signed short y; }; };";
88 testok "declarg", "interface test { void test(struct { int x; } a); };";
89 testok "structarg", "interface test { void test(struct a b); };";
90 testfail "structargmissing", "interface test { void test(struct a); };",
91 "<structargmissing>:0: Syntax error near ')'\n";
92 testok "structqual", "interface test { struct x { struct y z; }; };";
93 testok "unionqual", "interface test { struct x { union y z; }; };";
94 testok "enumqual", "interface test { struct x { enum y z; }; };";
95 testok "bitmapqual", "interface test { struct x { bitmap y z; }; };";
96 testok "emptystructdecl", "interface test { struct x; };";
97 testok "emptyenumdecl", "interface test { enum x; };";
98 testok "emptytdstructdecl", "interface test { typedef struct x y; };";
99 testok "import", "import \"foo.idl\";";
100 testok "include", "include \"foo.h\";";
101 testfail "import-noquotes", "import foo.idl;",
102 "<import-noquotes>:0: Syntax error near 'foo'\n";
103 testfail "include-noquotes", "include foo.idl;",
104 "<include-noquotes>:0: Syntax error near 'foo'\n";
105 testok "importlib", "importlib \"foo.idl\";";
106 testfail "import-nosemicolon", "import \"foo.idl\"",
107 "<import-nosemicolon>:0: Syntax error near 'foo.idl'\n";
108 testok "import-multiple", "import \"foo.idl\", \"bar.idl\";";
109 testok "include-multiple", "include \"foo.idl\", \"bar.idl\";";
110 testok "empty-struct", "interface test { struct foo { }; }";
111 testok "typedef-double", "interface test { typedef struct foo { } foo; }";
112 testok "cpp-quote", "cpp_quote(\"bla\")";
114 my $x = Parse::Pidl::IDL::parse_string("interface foo { struct x {}; }", "<foo>");
116 is_deeply($x,
117 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
118 { 'NAME' => 'x', 'TYPE' => 'STRUCT', ELEMENTS => [] } ],
119 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);
121 $x = Parse::Pidl::IDL::parse_string("interface foo { struct x; }", "<foo>");
122 is_deeply($x,
123 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
124 { 'NAME' => 'x', 'TYPE' => 'STRUCT' } ],
125 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);
127 $x = Parse::Pidl::IDL::parse_string("cpp_quote(\"foobar\")", "<quote>");
128 is_deeply($x,
129 [ { 'FILE' => '<quote>', 'DATA' => '"foobar"',
130 'TYPE' => 'CPP_QUOTE', 'LINE' => 0 } ]);
132 # A typedef of a struct without body
133 $x = Parse::Pidl::IDL::parse_string("interface foo { typedef struct x y; }", "<foo>");
135 is_deeply($x,
136 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
137 { 'FILE' => '<foo>', 'LINE' => 0, 'NAME' => 'y', 'TYPE' => 'TYPEDEF', DATA => {
138 TYPE => 'STRUCT', NAME => 'x' } } ],
139 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);
141 # A typedef of a struct with empty body
142 $x = Parse::Pidl::IDL::parse_string("interface foo { typedef struct {} y; }", "<foo>");
144 is_deeply($x,
145 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
146 { 'FILE' => '<foo>', 'LINE' => 0, 'NAME' => 'y', 'TYPE' => 'TYPEDEF', DATA => { TYPE => 'STRUCT', ELEMENTS => [] } } ],
147 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);
149 # A typedef of a bitmap with no body
150 $x = Parse::Pidl::IDL::parse_string("interface foo { typedef bitmap x y; }", "<foo>");
152 is_deeply($x,
153 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
154 { 'FILE' => '<foo>', 'LINE' => 0, 'NAME' => 'y', 'TYPE' => 'TYPEDEF', DATA => { TYPE => 'BITMAP', NAME => 'x' } } ],
155 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);
158 # A typedef of a union with no body
159 $x = Parse::Pidl::IDL::parse_string("interface foo { typedef union x y; }", "<foo>");
161 is_deeply($x,
162 [ { 'FILE' => '<foo>', 'NAME' => 'foo', 'DATA' => [
163 { 'FILE' => '<foo>', 'LINE' => 0, 'NAME' => 'y', 'TYPE' => 'TYPEDEF', DATA => { TYPE => 'UNION', NAME => 'x' } } ],
164 'TYPE' => 'INTERFACE', 'LINE' => 0 } ]);