Update tests to use maybe_plan.
[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,
17 'HTML::Parser',
18 'SQL::Translator::Parser::MySQL',
19 'SQL::Translator::Producer::HTML');
45e4da22 20}
21
2d691ec1 22my ($p, $tables, $classes);
23$p = HTML::Parser->new(api_version => 3);
24$p->strict_names(1);
25
45e4da22 26my $create = q|
27CREATE TABLE foo (
28 int id PRIMARY KEY AUTO_INCREMENT NOT NULL,
29 name VARCHAR(255)
30);
31|;
32
33my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML');
34my $parsed = $tr->translate(data => $create);
35my $status;
36
37eval {
38 $status = $p->parse($parsed);
39};
40if ($@) {
45e4da22 41 fail("Unable to parse the output!");
45e4da22 42}
43
45e4da22 44# General
45ok($parsed, "Parsed table OK");
46ok($status, "Parsed HTML OK");
47
48$p->handler(start => @{$HANDLERS{count_tables}});
49$p->parse($parsed);
50
8e8f4959 51is($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
57is($classes, 1, "One 'LinkTable' class");
58$tables = $classes = 0;
59
60$p->handler(start => @{$HANDLERS{sqlfairy}});
61$p->parse($parsed);
62
63is($classes, 1, "SQLfairy plug is alive and well ");
64$tables = $classes = 0;
65
66# Handler functions for the parser
67BEGIN {
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}