From: Vincent Pit Date: Sun, 18 Jul 2010 08:37:13 +0000 (+0200) Subject: Check path elements with ->does X-Git-Tag: v0.01~58 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=263a93f34deb0f2e87e89d6186361c7232b61c04;p=perl%2Fmodules%2FLaTeX-TikZ.git Check path elements with ->does --- diff --git a/lib/LaTeX/TikZ/Set/Path.pm b/lib/LaTeX/TikZ/Set/Path.pm index 319298b..b8d9768 100644 --- a/lib/LaTeX/TikZ/Set/Path.pm +++ b/lib/LaTeX/TikZ/Set/Path.pm @@ -15,30 +15,34 @@ Version 0.01 our $VERSION = '0.01'; -use LaTeX::TikZ::Tools; - use Any::Moose; +use Any::Moose 'Util::TypeConstraints' + => [ qw/subtype as where find_type_constraint/ ]; with qw( LaTeX::TikZ::Set::Op LaTeX::TikZ::Set::Mutable ); +subtype 'LaTeX::TikZ::Set::Path::Elements' + => as 'Object' + => where { $_->does('LaTeX::TikZ::Set::Op') }; + has '_ops' => ( is => 'ro', - isa => 'Maybe[ArrayRef[LaTeX::TikZ::Set::Op]]', + isa => 'Maybe[ArrayRef[LaTeX::TikZ::Set::Path::Elements]]', init_arg => 'ops', default => sub { [ ] }, ); sub ops { @{$_[0]->_ops} } -my $ltso_tc = LaTeX::TikZ::Tools::type_constraint('LaTeX::TikZ::Set::Op'); +my $ltspe_tc = find_type_constraint('LaTeX::TikZ::Set::Path::Elements'); sub add { my $set = shift; - $ltso_tc->check($_) for @_; + $ltspe_tc->check($_) for @_; push @{$_[0]->_ops}, @_; diff --git a/t/10-set.t b/t/10-set.t index 3908642..029fccd 100644 --- a/t/10-set.t +++ b/t/10-set.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 5 + 2 * 4; +use Test::More tests => 9 + 2 * 6; use LaTeX::TikZ; use LaTeX::TikZ::Formatter; @@ -74,3 +74,38 @@ check $seq3, 'two different raw sets and a sequence', <<'RES'; \draw bar ; \draw foo ; RES + +my $baz = eval { + Tikz->raw('baz'); +}; +is $@, '', 'creating yet another raw set doesn\'t croak'; + +eval { + $foo->add($baz); +}; +like $@, qr/Can't locate object method "add"/, + 'adding something to a raw set croaks'; + +eval { + $seq2->add($baz, $baz); +}; +is $@, '', 'adding something to a sequence set doesn\'t croak'; + +check $seq3, 'two different raw sets and an extended sequence', <<'RES'; +\draw bar ; +\draw foo ; +\draw bar ; +\draw baz ; +\draw baz ; +\draw foo ; +RES + +my $path = eval { + Tikz->path($foo, $bar, $baz); +}; +is $@, '', 'creating a path set doesn\'t croak'; + +check [ $path, $path ], 'two identical path sets', <<'RES'; +\draw foo bar baz ; +\draw foo bar baz ; +RES