Add author test for whitespace errors and make whitespace more consistent
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 27filter_generated.t
1 use strict;
2 use warnings;
3 use DBIx::Class::Schema::Loader;
4 use DBIx::Class::Schema::Loader::Utils 'slurp_file';
5 use File::Path;
6 use Test::More tests => 19;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use make_dbictest_db;
10 use dbixcsl_test_dir qw/$tdir/;
11
12 my $dump_path = "$tdir/dump";
13
14 my %original_class_data;
15
16 my ($schema_file_count, $result_file_count);
17
18 {
19     package DBICTest::Schema::1;
20     use Test::More;
21     use base 'DBIx::Class::Schema::Loader';
22     __PACKAGE__->loader_options(
23         dump_directory => $dump_path,
24         quiet => 1,
25         filter_generated_code => sub {
26             my ($type, $class, $text) = @_;
27
28             like $type, qr/^(?:schema|result)\z/,
29                 'got correct file type';
30
31             if ($type eq 'schema') {
32                 $schema_file_count++;
33                 is $class, 'DBICTest::Schema::1',
34                     'correct class for schema type file passed to filter';
35             }
36             elsif ($type eq 'result') {
37                 $result_file_count++;
38                 like $class, qr/^DBICTest::Schema::1::Result::(?:Foo|Bar)\z/,
39                     'correct class for result type file passed to filter';
40             }
41             else {
42                 die 'invalid file type passed to filter';
43             }
44
45             $original_class_data{$class} = $text;
46             if ($class =~ /::1$/) {
47                 $text = "No Gotcha!";
48             }
49             else {
50                 $text .= q{my $foo = "Kilroy was here";};
51             }
52             return $text;
53         },
54     );
55 }
56
57 {
58     package DBICTest::Schema::2;
59     use base 'DBIx::Class::Schema::Loader';
60     __PACKAGE__->loader_options(
61         dump_directory => $dump_path,
62         quiet => 1,
63         filter_generated_code => "$^X t/bin/simple_filter",
64     );
65 }
66
67 DBICTest::Schema::1->connect($make_dbictest_db::dsn);
68
69 # schema is generated in 2 passes
70
71 is $schema_file_count, 2,
72     'correct number of schema files passed to filter';
73
74 is $result_file_count, 4,
75     'correct number of result files passed to filter';
76
77 my $foo = slurp_file "$dump_path/DBICTest/Schema/1/Result/Foo.pm";
78 ok ! -e "$dump_path/DBICTest/Schema/1.pm",
79     "No package means no file written";
80 ok $original_class_data{"DBICTest::Schema::1"},
81     "Even though we processed the missing class";
82 like $foo, qr/# Created by .* THE FIRST PART/s,
83     "We get the whole autogenerated text";
84 like $foo, qr/Kilroy was here/, "Can insert text";
85
86 DBICTest::Schema::2->connect($make_dbictest_db::dsn);
87
88 $foo = slurp_file "$dump_path/DBICTest/Schema/2/Result/Foo.pm";
89
90 like $foo, qr/Kilroy was here/,
91     "Can insert text via command filter";
92
93 END { rmtree($dump_path, 1, 1); }