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, |
17 | 'HTML::Parser', |
18 | 'SQL::Translator::Parser::MySQL', |
19 | 'SQL::Translator::Producer::HTML'); |
45e4da22 |
20 | } |
21 | |
2d691ec1 |
22 | my ($p, $tables, $classes); |
23 | $p = HTML::Parser->new(api_version => 3); |
24 | $p->strict_names(1); |
25 | |
45e4da22 |
26 | my $create = q| |
27 | CREATE TABLE foo ( |
28 | int id PRIMARY KEY AUTO_INCREMENT NOT NULL, |
29 | name VARCHAR(255) |
30 | ); |
31 | |; |
32 | |
33 | my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML'); |
34 | my $parsed = $tr->translate(data => $create); |
35 | my $status; |
36 | |
37 | eval { |
38 | $status = $p->parse($parsed); |
39 | }; |
40 | if ($@) { |
45e4da22 |
41 | fail("Unable to parse the output!"); |
45e4da22 |
42 | } |
43 | |
45e4da22 |
44 | # General |
45 | ok($parsed, "Parsed table OK"); |
46 | ok($status, "Parsed HTML OK"); |
47 | |
48 | $p->handler(start => @{$HANDLERS{count_tables}}); |
49 | $p->parse($parsed); |
50 | |
8e8f4959 |
51 | is($tables, 3, "One table in the SQL produces 3 <table> tags"); |
45e4da22 |
52 | $tables = $classes = 0; |
53 | |
54 | $p->handler(start => @{$HANDLERS{count_classes}}); |
55 | $p->parse($parsed); |
56 | |
57 | is($classes, 1, "One 'LinkTable' class"); |
58 | $tables = $classes = 0; |
59 | |
60 | $p->handler(start => @{$HANDLERS{sqlfairy}}); |
61 | $p->parse($parsed); |
62 | |
63 | is($classes, 1, "SQLfairy plug is alive and well "); |
64 | $tables = $classes = 0; |
65 | |
66 | # Handler functions for the parser |
67 | BEGIN { |
68 | %HANDLERS = ( |
69 | count_tables => [ |
70 | sub { |
71 | my $tagname = shift; |
72 | $tables++ if ($tagname eq 'table'); |
73 | }, 'tagname', |
74 | ], |
75 | |
76 | count_classes => [ |
77 | sub { |
78 | my ($tagname, $attr) = @_; |
79 | if ($tagname eq 'table' && |
80 | $attr->{'class'} && |
81 | $attr->{'class'} eq 'LinkTable') { |
82 | $classes++; |
83 | } |
84 | }, 'tagname,attr', |
85 | ], |
86 | |
87 | sqlfairy => [ |
88 | sub { |
89 | my ($tagname, $attr) = @_; |
90 | if ($tagname eq 'a' && |
91 | $attr->{'href'} && |
92 | $attr->{'href'} =~ /sqlfairy/i) { |
93 | $classes++; |
94 | } |
95 | }, 'tagname,attr', |
96 | ], |
97 | ); |
98 | } |