From: Vincent Pit Date: Sat, 17 Jul 2010 23:07:23 +0000 (+0200) Subject: Test the API in t/01-api.t X-Git-Tag: v0.01~62 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLaTeX-TikZ.git;a=commitdiff_plain;h=22f1a59ec62500605cf907a4117faf25160ad92c Test the API in t/01-api.t --- diff --git a/MANIFEST b/MANIFEST index a85376d..812e676 100644 --- a/MANIFEST +++ b/MANIFEST @@ -31,4 +31,5 @@ lib/LaTeX/TikZ/Set/Raw.pm lib/LaTeX/TikZ/Set/Rectangle.pm lib/LaTeX/TikZ/Set/Sequence.pm t/00-load.t +t/01-api.t t/91-pod.t diff --git a/lib/LaTeX/TikZ/Mod/Raw.pm b/lib/LaTeX/TikZ/Mod/Raw.pm index e7ff0fd..c6e1a2c 100644 --- a/lib/LaTeX/TikZ/Mod/Raw.pm +++ b/lib/LaTeX/TikZ/Mod/Raw.pm @@ -37,7 +37,7 @@ sub declare { } sub apply { $_[0]->content } -use LaTeX::TikZ::API raw_mode => sub { +use LaTeX::TikZ::API raw_mod => sub { shift; __PACKAGE__->new(content => $_[0]); diff --git a/t/01-api.t b/t/01-api.t new file mode 100644 index 0000000..6bcada7 --- /dev/null +++ b/t/01-api.t @@ -0,0 +1,89 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => 5 + 13 + 12; + +use LaTeX::TikZ; + +ok(defined &Tikz, 'main::Tikz constant is defined'); +is(prototype('Tikz'), '', 'main::Tikz is actually a constant'); + +{ + package LaTeX::TikZ::TestAPI1; + + eval { + LaTeX::TikZ->import(as => ':)'); + }; + ::like($@, qr/^Invalid name/, 'invalid name'); +} + +{ + package LaTeX::TikZ::TestAPI2; + + use LaTeX::TikZ as => 'T'; + + ::ok(defined &T, 'LaTeX::TikZ::TestAPI2::T constant is defined'); + ::is(prototype('T'), '', 'LaTeX::TikZ::TestAPI2::T is actually a constant'); +} + +my @methods = qw/ + raw + path seq + point line rectangle circle + raw_mod + clip layer + width color fill +/; + +for (@methods) { + ok(Tikz->can($_), "Tikz evaluates to something that ->can($_)"); +} + +require LaTeX::TikZ::API; + +for my $name (undef, ':)') { + eval { + LaTeX::TikZ::API->import( + $name => sub { }, + ); + }; + like $@, qr/^Invalid interface name/, 'invalid interface name'; +} + +eval { + LaTeX::TikZ::API->import( + 'raw' => sub { }, + ); +}; +like $@, qr/^'raw' is already defined/, 'already defined'; + +for my $code (undef, [ ]) { + eval { + LaTeX::TikZ::API->import( + 'foo' => $code, + ); + }; + like $@, qr/^Invalid code reference/, 'invalid code'; +} + +eval { + LaTeX::TikZ::API->import( + 'foo' => sub { @_ }, + ); +}; +is $@, '', 'registering foo doesn\'t croak'; +ok(Tikz->can('foo'), 'Tikz evaluates to something that ->can(foo)'); +is_deeply [ Tikz->foo('hello') ], [ Tikz, 'hello' ], 'Tikz->foo works'; + +eval { + LaTeX::TikZ::API->import( + 'bar' => sub { @_ }, + 'baz' => undef, + ); +}; +like $@, qr/^Invalid code reference/, 'baz is invalid code'; +ok(!Tikz->can('baz'), 'baz was not defined'); +ok(Tikz->can('bar'), 'but bar was defined'); +is_deeply [ Tikz->bar('bonjour') ], [ Tikz, 'bonjour' ], 'Tikz->bar works';