File: 29html.t

package info (click to toggle)
libsql-translator-perl 0.11024-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,572 kB
  • sloc: perl: 67,471; sql: 3,809; xml: 258; makefile: 2
file content (100 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/local/bin/perl -w
# vim: set ft=perl:

# This test creates an HTML::Parser instance and uses it to selectively
# parse the output of the HTML producer.  Rather than try to ensure
# that the produced HTML turns into a particular parse tree or anything
# like that, it performs some heuristics on the output.

use strict;
use vars qw(%HANDLERS);
use Test::More;
use Test::SQL::Translator qw(maybe_plan);
use SQL::Translator;

BEGIN {
    maybe_plan(5,
        'CGI',
        'HTML::Parser',
        'SQL::Translator::Parser::MySQL',
        'SQL::Translator::Producer::HTML');
}

my ($p, $tables, $classes);
$p = HTML::Parser->new(api_version => 3);
$p->strict_names(1);

my $create = q|
CREATE TABLE foo (
    int id PRIMARY KEY AUTO_INCREMENT NOT NULL,
    name VARCHAR(255)
);
|;

my $tr = SQL::Translator->new(parser => 'MySQL', producer => 'HTML');
my $parsed = $tr->translate(data => $create) or die $tr->error;
my $status;

eval {
    $status = $p->parse($parsed);
};
if ($@) {
    daig $@;
    fail("Unable to parse the output!");
}

# General
ok($parsed, "Parsed table OK");
ok($status, "Parsed HTML OK");

$p->handler(start => @{$HANDLERS{count_tables}});
$p->parse($parsed);

is($tables, 3, "One table in the SQL produces 3 <table> tags");
$tables = $classes = 0;

$p->handler(start => @{$HANDLERS{count_classes}});
$p->parse($parsed);

is($classes, 1, "One 'LinkTable' class");
$tables = $classes = 0;

$p->handler(start => @{$HANDLERS{sqlfairy}});
$p->parse($parsed);

is($classes, 1, "SQLfairy plug is alive and well ");
$tables = $classes = 0;

# Handler functions for the parser
BEGIN {
    %HANDLERS = (
        count_tables => [
            sub {
                my $tagname = shift;
                $tables++ if ($tagname eq 'table');
            }, 'tagname',
        ],

        count_classes => [
            sub {
                my ($tagname, $attr) = @_;
                if ($tagname eq 'table' &&
                    $attr->{'class'} &&
                    $attr->{'class'} eq 'LinkTable') {
                    $classes++;
                }
            }, 'tagname,attr',
        ],

        sqlfairy => [
            sub {
                my ($tagname, $attr) = @_;
                if ($tagname eq 'a' &&
                    $attr->{'href'} &&
                    $attr->{'href'} =~ /sqlfairy/i) {
                    $classes++;
                }
            }, 'tagname,attr',
        ],
    );
}