Commits

Toby Inkster committed 2127964

allow assertions to operate on $_; test assertions

Comments (0)

Files changed (2)

 		for my $a (@$ass)
 		{
 			require Types::TypeTiny;
-			if (Types::TypeTiny::CodeLike->check($a))
+			if (Types::TypeTiny::CodeLike()->check($a))
 			{
 				$assertion_i++;
 				$assertions[$assertion_i] = $a;
-				push @r, sprintf('$Types::XSD::assertions[%d]->(%s)', $assertion_i, $var);
+				push @r,
+					($var eq '$_')
+						? sprintf('$Types::XSD::assertions[%d]->(%s)', $assertion_i, $var)
+						: sprintf('do { local $_ = %s; $Types::XSD::assertions[%d]->(%s) }', $var, $assertion_i, $var);
 			}
-			elsif (Types::TypeTiny::StringLike->check($a))
+			elsif (Types::TypeTiny::StringLike()->check($a))
 			{
-				push @r, ($var eq '$_') ? "do { $a }" : "do { local $_ = $var; $a }";
+				push @r,
+					($var eq '$_')
+						? "do { $a }"
+						: "do { local \$_ = $var; $a }";
 			}
 			else
 			{
 sub facet
 {
 	my $self   = pop;
-	my @facets = @_;
+	my @facets = ("assertion", @_);
 	my $regexp = qr{^${\(join "|", map quotemeta, @facets)}$}ms;
 	my $name   = "$self";
 	
 should_fail('2009-02-12T03:54:00', DateTime[explicitTimezone => "required"]);
 should_pass('2009-02-12T03:54:00', DateTime[explicitTimezone => "prohibited"]);
 
+should_pass('2009-02-12T03:54:00', DateTime[assertion => sub { m/^2009/ }]);
+should_pass('2009-02-12T03:54:00', DateTime[assertion => 'm/^2009/']);
+should_fail('2010-02-12T03:54:00', DateTime[assertion => sub { m/^2009/ }]);
+should_fail('2010-02-12T03:54:00', DateTime[assertion => 'm/^2009/']);
+
+should_pass('2009-02-12T03:54:00', DateTime[assertion => [sub { m/^2009/ }, 'm/-02-/']]);
+
 done_testing;