From 258ef17401f558941646685fb47ab24a236271b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C5=8Dan?= Date: Sat, 21 Feb 2026 08:45:43 -0700 Subject: [PATCH] fix: Assert plugin always throws on defined values (GH #300) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Template::Monad::Assert::AUTOLOAD was passing $stash instead of $this to dotop(), causing it to look up the item in the root stash rather than in the actual hash/list being asserted on. For example, [% foo.assert.bar %] would look for 'bar' in the stash root (where it doesn't exist) instead of in foo, always throwing "undefined value for bar" even when foo.bar is defined. Added 6 tests covering defined-value pass-through for hashes, nested hashes, arrays, and direct stash variable access. Fixes #300 Co-Authored-By: Kōan --- lib/Template/Plugin/Assert.pm | 2 +- t/assert.t | 52 +++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/Template/Plugin/Assert.pm b/lib/Template/Plugin/Assert.pm index df34dfa3c..1e1c3a199 100644 --- a/lib/Template/Plugin/Assert.pm +++ b/lib/Template/Plugin/Assert.pm @@ -84,7 +84,7 @@ sub AUTOLOAD { $item =~ s/.*:://; return if $item eq 'DESTROY'; - my $value = $stash->dotop($stash, $item, \@args); + my $value = $stash->dotop($this, $item, \@args); if (! defined $value) { die $EXCEPTION->new( assert => "undefined value for $item" ); diff --git a/t/assert.t b/t/assert.t index 0aa7c48cd..ddd957d69 100644 --- a/t/assert.t +++ b/t/assert.t @@ -40,12 +40,15 @@ sub nil { package main; -my $vars = { +my $vars = { object => Template::Test::Object->new, hash => { foo => 10, bar => undef }, list => [ undef ], subref => sub { return undef }, nothing => undef, + nested => { name => 'hello', deep => { value => 42 } }, + items => [ 10, 20, 30 ], + name => 'world', }; test_expect(\*DATA, undef, $vars); @@ -105,11 +108,56 @@ assert error - undefined value for first -- expect -- assert error - undefined value for nothing --- test -- +-- test -- [% USE assert; TRY; assert.subref; CATCH; error; END; %] -- expect -- assert error - undefined value for subref +#------------------------------------------------------------------------ +# GH #300: assert should pass through defined values without error +#------------------------------------------------------------------------ + +-- test -- +[% USE assert; + hash.assert.foo; +%] +-- expect -- +10 + +-- test -- +[% USE assert; + nested.assert.name; +%] +-- expect -- +hello + +-- test -- +[% USE assert; + nested.assert.deep.value; +%] +-- expect -- +42 + +-- test -- +[% USE assert; + items.assert.size; +%] +-- expect -- +3 + +-- test -- +[% USE assert; + items.assert.first; +%] +-- expect -- +10 + +-- test -- +[% USE assert; + assert.name; +%] +-- expect -- +world