Commit | Line | Data |
45e4da22 |
1 | #!/usr/local/bin/perl -w |
2 | # vim: set ft=perl: |
3 | |
4 | # This test creates an HTML::Parser instance and uses it to selectively |
5 | # parse the output of the HTML producer. Rather than try to ensure |
6 | # that the produced HTML turns into a particular parse tree or anything |
7 | # like that, it performs some heuristics on the output. |
8 | |
9 | use strict; |
10 | use vars qw(%HANDLERS); |
11 | use Test::More; |
2d691ec1 |
12 | use Test::SQL::Translator qw(maybe_plan); |
45e4da22 |
13 | use SQL::Translator; |
45e4da22 |
14 | |
2d691ec1 |
15 | BEGIN { |
16 | maybe_plan(5, |
ae4a60a5 |
17 | 'CGI', |
2d691ec1 |
18 | 'HTML::Parser', |
19 | 'SQL::Translator::Parser::MySQL', |
20 | 'SQL::Translator::Producer::HTML'); |
45e4da22 |
21 | } |
22 | |
2d691ec1 |
23 | my ($p, $tables, $classes); |
24 | $p = HTML::Parser->new(api_version => 3); |
aee4b66e |
25 | $p->strict_names(1); |
2d691ec1 |
26 | |
45e4da22 |
27 | my $create = q| |
28 | CREATE TABLE foo ( |
29 | int id PRIMARY KEY AUTO_INCREMENT NOT NULL, |
30 | name VARCHAR(255) |
31 | ); |
32 | |; |
33 | |
34 | my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML'); |
f9c96971 |
35 | my $parsed = $tr->translate(data => $create) or die $tr->error; |
45e4da22 |
36 | my $status; |
37 | |
38 | eval { |
aee4b66e |
39 | $status = $p->parse($parsed); |
45e4da22 |
40 | }; |
41 | if ($@) { |
f9c96971 |
42 | daig $@; |
45e4da22 |
43 | fail("Unable to parse the output!"); |
45e4da22 |
44 | } |
45 | |
45e4da22 |
46 | # General |
47 | ok($parsed, "Parsed table OK"); |
48 | ok($status, "Parsed HTML OK"); |
49 | |
50 | $p->handler(start => @{$HANDLERS{count_tables}}); |
51 | $p->parse($parsed); |
52 | |
8e8f4959 |
53 | is($tables, 3, "One table in the SQL produces 3 <table> tags"); |
45e4da22 |
54 | $tables = $classes = 0; |
55 | |
56 | $p->handler(start => @{$HANDLERS{count_classes}}); |
57 | $p->parse($parsed); |
58 | |
59 | is($classes, 1, "One 'LinkTable' class"); |
60 | $tables = $classes = 0; |
61 | |
62 | $p->handler(start => @{$HANDLERS{sqlfairy}}); |
63 | $p->parse($parsed); |
64 | |
65 | is($classes, 1, "SQLfairy plug is alive and well "); |
66 | $tables = $classes = 0; |
67 | |
68 | # Handler functions for the parser |
69 | BEGIN { |
70 | %HANDLERS = ( |
71 | count_tables => [ |
72 | sub { |
73 | my $tagname = shift; |
74 | $tables++ if ($tagname eq 'table'); |
75 | }, 'tagname', |
76 | ], |
77 | |
78 | count_classes => [ |
79 | sub { |
80 | my ($tagname, $attr) = @_; |
81 | if ($tagname eq 'table' && |
82 | $attr->{'class'} && |
83 | $attr->{'class'} eq 'LinkTable') { |
84 | $classes++; |
85 | } |
86 | }, 'tagname,attr', |
87 | ], |
88 | |
89 | sqlfairy => [ |
90 | sub { |
91 | my ($tagname, $attr) = @_; |
92 | if ($tagname eq 'a' && |
93 | $attr->{'href'} && |
94 | $attr->{'href'} =~ /sqlfairy/i) { |
95 | $classes++; |
96 | } |
97 | }, 'tagname,attr', |
aee4b66e |
98 | ], |
45e4da22 |
99 | ); |
100 | } |