Skip HTML tests if CGI is not installed (RT#98027)
[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;
2d691ec1 12use Test::SQL::Translator qw(maybe_plan);
45e4da22 13use SQL::Translator;
45e4da22 14
2d691ec1 15BEGIN {
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 23my ($p, $tables, $classes);
24$p = HTML::Parser->new(api_version => 3);
aee4b66e 25$p->strict_names(1);
2d691ec1 26
45e4da22 27my $create = q|
28CREATE TABLE foo (
29 int id PRIMARY KEY AUTO_INCREMENT NOT NULL,
30 name VARCHAR(255)
31);
32|;
33
34my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML');
f9c96971 35my $parsed = $tr->translate(data => $create) or die $tr->error;
45e4da22 36my $status;
37
38eval {
aee4b66e 39 $status = $p->parse($parsed);
45e4da22 40};
41if ($@) {
f9c96971 42 daig $@;
45e4da22 43 fail("Unable to parse the output!");
45e4da22 44}
45
45e4da22 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
8e8f4959 53is($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
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',
aee4b66e 98 ],
45e4da22 99 );
100}