Tab/WS crusade
[dbsrgits/SQL-Translator.git] / t / 18ttschema-producer.t
1 #!/usr/bin/perl -w
2 # vim:filetype=perl
3
4 # Before `make install' is performed this script should be runnable with
5 # `make test'. After `make install' it should work as `perl test.pl'
6
7 use strict;
8 use Test::More;
9 use Test::Exception;
10 use Test::SQL::Translator qw(maybe_plan);
11
12 use Data::Dumper;
13 use FindBin qw/$Bin/;
14
15 # Testing 1,2,3,4...
16 #=============================================================================
17
18 BEGIN {
19     maybe_plan(6,
20         'SQL::Translator::Parser::XML::SQLFairy',
21         'Template 2.20',
22         'Test::Differences'
23     );
24 }
25 use Test::Differences;
26
27 use SQL::Translator;
28 use SQL::Translator::Producer::TTSchema;
29
30 # Main test. Template whole schema and test tt_vars
31 {
32     my $obj;
33     $obj = SQL::Translator->new(
34         show_warnings  => 0,
35         from           => "XML-SQLFairy",
36         filename       => "$Bin/data/xml/schema.xml",
37         to             => "TTSchema",
38         producer_args  => {
39             ttfile  => "$Bin/data/template/basic.tt",
40             tt_vars => {
41                 foo   => 'bar',
42                 hello => 'world',
43             },
44         },
45     );
46     my $out;
47     lives_ok { $out = $obj->translate; }  "Translate ran";
48     ok $out ne ""                        ,"Produced something!";
49     eq_or_diff
50       $out,
51       do { local (@ARGV, $/) = "$Bin/data/template/testresult_basic.txt"; <> },
52       "Output looks right"
53     ;
54 }
55
56 # Test passing of Template config
57 {
58     my $tmpl = q{
59     [%- FOREACH table = schema.get_tables %]
60     Table: $table
61     [%- END %]};
62     my $obj;
63     $obj = SQL::Translator->new(
64         show_warnings  => 0,
65         from           => "XML-SQLFairy",
66         filename       => "$Bin/data/xml/schema.xml",
67         to             => "TTSchema",
68         producer_args  => {
69             ttfile  => \$tmpl,
70             tt_conf => {
71                 INTERPOLATE => 1,
72             },
73             tt_vars => {
74                 foo   => 'bar',
75                 hello => 'world',
76             },
77         },
78     );
79     my $out;
80     lives_ok { $out = $obj->translate; }  "Translate ran";
81     ok $out ne ""                        ,"Produced something!";
82     local $/ = undef; # slurp
83     eq_or_diff $out, q{
84     Table: Basic
85     Table: Another}
86     ,"Output looks right";
87 }