Re-add CQLLexer#main
[cql-java-moved-to-github.git] / test / regression / runcanon
1 #!/usr/bin/perl -w
2
3 #
4 # Tests that all sample queries can be rendered into idempotent
5 # canoncial form.
6
7 use IO::File;
8 use strict;
9
10 $ENV{CLASSPATH} .= ":../../lib/cql-java.jar";
11
12 my($ntests, $ncorrect) = (0, 0);
13
14 while (<sections/*>) {
15     my $sdir = $_;
16     s@sections/@@;
17     next if /^CVS$/ || /^10$/;
18     print "testing section $_ - ", read_file("$sdir/name"), "\n";
19
20     while (<$sdir/*.cql>) {
21         my $qfile = $_;
22         s@sections/([0-9]+/.*)\.cql@$1@;
23         my $query = read_file($qfile);
24         my $canonical = `CQLParser -c '$query'`;
25         chomp($canonical);
26         my $maybe = `CQLParser -c '$canonical'`;
27         chomp($maybe);
28         print "$query // $canonical ";
29         $ntests++;
30         if ($maybe eq $canonical) {
31             $ncorrect++;
32             print " OK\n";
33         } else {
34             print "### $maybe\n";
35         }
36     }
37 }
38
39 print sprintf("%d of %d passed: %d%%\n",
40               $ncorrect, $ntests, (100 * $ncorrect) / $ntests);
41
42 sub read_file {
43     my($name) = @_;
44
45     $name = "<$name" if $name !~ /\|$/;
46     my $fh = new IO::File("$name")
47         or die "can't read '$name': $!";
48     my $contents = join('', <$fh>);
49     $fh->close();
50     return $contents;
51 }