r14550: Fix tests
[Samba.git] / source / pidl / tests / Util.pm
blob37177cc7300d774897910ad09a77267a9fbd1519
1 # Some simple utility functions for pidl tests
2 # Copyright (C) 2005 Jelmer Vernooij
3 # Published under the GNU General Public License
5 package Util;
7 require Exporter;
8 @ISA = qw(Exporter);
9 @EXPORT_OK = qw(test_samba4_ndr);
11 use strict;
13 use Test::More;
14 use Parse::Pidl::IDL;
15 use Parse::Pidl::NDR;
16 use Parse::Pidl::Samba4::NDR::Parser;
17 use Parse::Pidl::Samba4::Header;
18 use Parse::Pidl::Samba4 qw(is_intree);
20 my $sanecc = 0;
22 # Generate a Samba4 parser for an IDL fragment and run it with a specified
23 # piece of code to check whether the parser works as expected
24 sub test_samba4_ndr($$$)
26 my ($name,$idl,$c) = @_;
27 my $pidl = Parse::Pidl::IDL::parse_string("interface echo { $idl }; ", "<$name>");
29 ok(defined($pidl), "($name) parse idl");
30 my $header = Parse::Pidl::Samba4::Header::Parse($pidl);
31 ok(defined($header), "($name) generate generic header");
32 my $pndr = Parse::Pidl::NDR::Parse($pidl);
33 ok(defined($pndr), "($name) generate NDR tree");
34 my ($ndrheader,$ndrparser) = Parse::Pidl::Samba4::NDR::Parser::Parse($pndr, undef, undef);
35 ok(defined($ndrparser), "($name) generate NDR parser");
36 ok(defined($ndrheader), "($name) generate NDR header");
38 SKIP: {
40 my $link = is_intree() && 0; # FIXME
42 skip "no samba environment available, skipping compilation", 3
43 if not is_intree();
45 skip "no sane C compiler, skipping compilation", 3
46 if not $sanecc;
48 my $test_data_prefix = $ENV{TEST_DATA_PREFIX};
50 my $outfile;
51 if (defined($test_data_prefix)) {
52 $outfile = "$test_data_prefix/test-$name";
53 } else {
54 $outfile = "test-$name";
57 #my $cflags = $ENV{CFLAGS};
58 my $cflags = "-Iinclude -Ilib -I.";
60 if (is_intree() and $link) {
61 open CC, "|cc -x c -o $outfile $cflags -";
62 } elsif (is_intree()) {
63 open CC, "|cc -x c -c -o $outfile $cflags -";
65 print CC "#include \"includes.h\"\n";
66 print CC $header;
67 print CC $ndrheader;
68 print CC $ndrparser;
69 print CC "int main(int argc, const char **argv)
71 TALLOC_CTX *mem_ctx = talloc_init(NULL);
75 talloc_free(mem_ctx);
77 return 0; }\n";
78 close CC;
80 ok(-f $outfile, "($name) compile");
82 unless ($link) {
83 skip "no shared libraries of Samba available yet, can't run test", 2;
84 unlink($outfile);
87 ok(system($outfile), "($name) run");
89 ok(unlink($outfile), "($name) remove");
94 my $outfile = "test"; # FIXME: Somewhat more unique name
96 # Test whether CC is sane. The real 'fix' here would be using the
97 # Samba build system, but unfortunately, we have no way of hooking into that
98 # yet so we're running CC directly for now
99 $sanecc = 1 if system('echo "main() {}"'." | cc -I. -x c -c - -o $outfile") == 0;