root/t/01template.t

Revision 303:6f57b7328cb8, 4.0 KB (checked in by Anselm Lingnau <anselm@…>, 9 months ago)

The Great Renaming: Change all command names to 'pl-*' and directory to '.pl'.
The idea behind this is to emphasise that the software is called ?*Project*
Lancelot? as opposed to plain ?Lancelot?. There is apparently a KDE tool that
goes by the name of ?Lancelot?, and we want to make clear that this is separate
and different. Also, Project Lancelot has been around for a lot longer!

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