Added list of constants exported to the docs.
[dbsrgits/SQL-Translator.git] / t / 29html.t
CommitLineData
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
9use strict;
10use vars qw(%HANDLERS);
11use Test::More;
12use SQL::Translator;
45e4da22 13
14my ($p, $tables, $classes);
15eval {
16 require HTML::Parser;
17 $p = HTML::Parser->new(api_version => 3);
18 $p->strict_names(1);
19};
20if ($@) {
21 plan skip_all => "Missing HTML::Parser";
22}
23
24my $create = q|
25CREATE TABLE foo (
26 int id PRIMARY KEY AUTO_INCREMENT NOT NULL,
27 name VARCHAR(255)
28);
29|;
30
31my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML');
32my $parsed = $tr->translate(data => $create);
33my $status;
34
35eval {
36 $status = $p->parse($parsed);
37};
38if ($@) {
39 plan tests => 1;
40 fail("Unable to parse the output!");
41 exit 1;
42}
43
44plan tests => 5;
45
46# General
47ok($parsed, "Parsed table OK");
48ok($status, "Parsed HTML OK");
49
50$p->handler(start => @{$HANDLERS{count_tables}});
51$p->parse($parsed);
52
53is($tables, 2, "One table in the SQL produces 2 <table> tags");
54$tables = $classes = 0;
55
56$p->handler(start => @{$HANDLERS{count_classes}});
57$p->parse($parsed);
58
59is($classes, 1, "One 'LinkTable' class");
60$tables = $classes = 0;
61
62$p->handler(start => @{$HANDLERS{sqlfairy}});
63$p->parse($parsed);
64
65is($classes, 1, "SQLfairy plug is alive and well ");
66$tables = $classes = 0;
67
68# Handler functions for the parser
69BEGIN {
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',
98 ],
99 );
100}