| 1 | #!/usr/bin/perl -w |
|---|
| 2 | |
|---|
| 3 | use Test::More tests => 21; |
|---|
| 4 | use Test::Exception; |
|---|
| 5 | use Sub::Override; |
|---|
| 6 | use File::Spec; |
|---|
| 7 | use File::Path; |
|---|
| 8 | |
|---|
| 9 | use Lancelot::DB; |
|---|
| 10 | BEGIN { use_ok(Lancelot::Template); } |
|---|
| 11 | |
|---|
| 12 | # This must not conflict with a real list name. |
|---|
| 13 | |
|---|
| 14 | my ($listname_local, $listname_domain) = ('pl-testlist', 'example.com'); |
|---|
| 15 | my $listname = "$listname_local\@$listname_domain"; |
|---|
| 16 | rmtree("$ENV{HOME}/.pl/$listname"); |
|---|
| 17 | my $db = new Lancelot::DB $listname, { create => 1 }; |
|---|
| 18 | $db->delete_configs("lancelot.templatedirs"); |
|---|
| 19 | |
|---|
| 20 | # Tests for replacement() |
|---|
| 21 | |
|---|
| 22 | # simple replacement from key=>value hash |
|---|
| 23 | is (Lancelot::Template::replacement($db, { "foo" => "BAR" }, "foo"), "BAR", |
|---|
| 24 | "replace test (simple)"); |
|---|
| 25 | # replacement from default value |
|---|
| 26 | is (Lancelot::Template::replacement($db, {}, "foo"), undef, |
|---|
| 27 | "replace test (empty default)"); |
|---|
| 28 | is (Lancelot::Template::replacement($db, {}, "foo|QUUX"), "QUUX", |
|---|
| 29 | "replace test (specified default)"); |
|---|
| 30 | is (Lancelot::Template::replacement($db, { "foo" => "BAR" }, "foo|QUUX"), |
|---|
| 31 | "BAR", "replace test (simple & specified default)"); |
|---|
| 32 | # replacement from configuration |
|---|
| 33 | $db->set_config("foo", "BAZ"); |
|---|
| 34 | is (Lancelot::Template::replacement($db, {}, "foo"), "BAZ", |
|---|
| 35 | "replace test (from configuration)"); |
|---|
| 36 | is (Lancelot::Template::replacement($db, { "foo" => "BAR" }, "foo"), "BAR", |
|---|
| 37 | "replace test (simple & from configuration)"); |
|---|
| 38 | # time format |
|---|
| 39 | is (Lancelot::Template::replacement($db, { "t" => 1234567890 }, |
|---|
| 40 | "t%dd.mm.yyyy, hh.mm.ss"), |
|---|
| 41 | "14.02.2009, 00.31.30", "replace test (time format)"); |
|---|
| 42 | |
|---|
| 43 | # Tests for register() |
|---|
| 44 | |
|---|
| 45 | ok( Lancelot::Template->register("test", "en", "foobar"), |
|---|
| 46 | "register (non-existing template)" ); |
|---|
| 47 | ok( Lancelot::Template->register("test", "en", "foobar"), |
|---|
| 48 | "register (existing template)" ); |
|---|
| 49 | |
|---|
| 50 | # Tests for find() and process() |
|---|
| 51 | |
|---|
| 52 | my $tdir = File::Spec->join($db->get_listdir, "templates", "en"); |
|---|
| 53 | mkpath $tdir; |
|---|
| 54 | open OUT, "> ".File::Spec->join($tdir, "pl-test1"); |
|---|
| 55 | print OUT "Test\n"; |
|---|
| 56 | close OUT; |
|---|
| 57 | |
|---|
| 58 | is (Lancelot::Template::find($db, "pl-test1", "en"), "Test\n", |
|---|
| 59 | "find (listdir)"); |
|---|
| 60 | is (Lancelot::Template->process("pl-test1", "en", $db, {}), "Test\n", |
|---|
| 61 | "process (simple)"); |
|---|
| 62 | rmtree(File::Spec->join($db->get_listdir, "templates")); |
|---|
| 63 | |
|---|
| 64 | $tdir = File::Spec->join($ENV{HOME}, '.pl', 'templates', 'en'); |
|---|
| 65 | mkpath $tdir unless -d $tdir; |
|---|
| 66 | open OUT, "> ".File::Spec->join($tdir, "pl-test2"); |
|---|
| 67 | print OUT "Test\n"; |
|---|
| 68 | close OUT; |
|---|
| 69 | |
|---|
| 70 | is (Lancelot::Template::find($db, "pl-test2", "en"), "Test\n", |
|---|
| 71 | "find (home directory)"); |
|---|
| 72 | my $saved_home = $ENV{'HOME'}; |
|---|
| 73 | delete $ENV{'HOME'}; |
|---|
| 74 | is (Lancelot::Template::find($db, "pl-test2", "en"), "Test\n", |
|---|
| 75 | "find (home dir, with default from /etc/passwd)"); |
|---|
| 76 | my $override = Sub::Override->new( |
|---|
| 77 | 'Lancelot::Template::get_homedir' => sub { return undef; } |
|---|
| 78 | ); |
|---|
| 79 | is (Lancelot::Template::find($db, "pl-test2", "en"), undef, |
|---|
| 80 | "find (home dir, with overridden getpwuid)"); |
|---|
| 81 | $override->restore(); |
|---|
| 82 | $ENV{'HOME'} = $saved_home; |
|---|
| 83 | |
|---|
| 84 | unlink File::Spec->join($ENV{HOME}, '.pl', 'templates', 'en', 'pl-test2'); |
|---|
| 85 | |
|---|
| 86 | $tdir = File::Spec->join("/tmp", "pl-test3", "en"); |
|---|
| 87 | mkpath $tdir unless -d $tdir; |
|---|
| 88 | open OUT, "> ".File::Spec->join($tdir, "pl-test3"); |
|---|
| 89 | print OUT "Test\n"; |
|---|
| 90 | close OUT; |
|---|
| 91 | |
|---|
| 92 | ok (!defined Lancelot::Template::find($db, "pl-test3", "en"), |
|---|
| 93 | "find (templatedirs not set)"); |
|---|
| 94 | $db->set_config("lancelot.templatedirs", "/tmp/pl-test3"); |
|---|
| 95 | is (Lancelot::Template::find($db, "pl-test3", "en"), "Test\n", |
|---|
| 96 | "find (templatedirs set)"); |
|---|
| 97 | rmtree(File::Spec->join("/tmp", "pl-test3")); |
|---|
| 98 | |
|---|
| 99 | $db->delete_configs("lancelot.templatedirs"); |
|---|
| 100 | ok (!defined Lancelot::Template->process("pl-test4", "en", $db, {}), |
|---|
| 101 | "process (undefined template)"); |
|---|
| 102 | Lancelot::Template->register("pl-test4", "en", "Hello world\n"); |
|---|
| 103 | is (Lancelot::Template->process("pl-test4", "en", $db, {}), "Hello world\n", |
|---|
| 104 | "process (fallback to registered template)"); |
|---|
| 105 | is (Lancelot::Template->process("pl-test4", "de", $db, {}), "Hello world\n", |
|---|
| 106 | "process (fallback to registered template - in English)"); |
|---|
| 107 | |
|---|
| 108 | Lancelot::Template->register("pl-test5", "en", "a<<b>>c\n"); |
|---|
| 109 | is (Lancelot::Template->process("pl-test5", "en", $db, { b => 'B' }), "aBc\n", |
|---|
| 110 | "process (with replacement)"); |
|---|