Commits

Toby Inkster committed 5c90f02

Improve test suite; coverage is now 100%.

Comments (0)

Files changed (4)

lib/Throwable/Factory.pm

 		my $class = shift;
 		return +{} unless @_;
 		unshift @_, 'message' if @_ % 2 and not ref $_[0];
-		$class->SUPER::BUILDARGS(@_) if @_;
+		$class->SUPER::BUILDARGS(@_);
 	}
 	
 	sub TO_STRING
 	ok($e->line);
 };
 
-done_testing();
+try {
+	MyException->throw({
+		message => "Test exception 2",
+		foo     => "Hello world",
+	});
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+	is($e->message, 'Test exception 2', 'Test that hashref constructor works.');
+	is($e->foo, 'Hello world');
+};
+
+try {
+	MyException->throw(["Test exception 3", "Hello world"]);
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+	is($e->message, 'Test exception 3', 'Test that arrayref constructor works.');
+	is($e->foo, 'Hello world');
+};
+
+try {
+	MyException->throw(
+		message => "Test exception 4",
+		foo     => "Hello world",
+	);
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+	is($e->message, 'Test exception 4', 'Test that Moose-style constructor works.');
+	is($e->foo, 'Hello world');
+};
+
+try {
+	MyException->throw;
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+	is($e->message, undef, 'Test that emptyconstructor works.');
+	is($e->foo, undef);
+};
+
+done_testing;
 		fields      => [ 'baz' ],
 		description => 'Extended version of Except1',
 	},
+	Except3 => {
+		isa         => 'Except2',
+		fields      => 'bam',
+		description => 'Extended version of Except2',
+	},
 ;
 
 # Throws for unsupported Exception::Class-style options.
 	is($e->description, 'Extended version of Except1');
 };
 
+try {
+	Except3->throw('Test 3');
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+
+	is($e->error, 'Test 3');
+	is_deeply([$e->FIELDS], [qw/ message foo bar baz bam /]);
+};
+
 done_testing;
+=head1 PURPOSE
+
+Test that Throwable::Factory::Base can be used.
+
+=head1 AUTHOR
+
+Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
+
+=head1 COPYRIGHT AND LICENCE
+
+This software is copyright (c) 2012 by Toby Inkster.
+
+This is free software; you can redistribute it and/or modify it under
+the same terms as the Perl 5 programming language system itself.
+
+=cut
+
+use strict;
+use warnings;
+use Test::More;
+
+use Try::Tiny;
+use Scalar::Util 'blessed';
+use Throwable::Factory;
+
+try {
+	Throwable::Factory::Base->throw('Test');
+}
+catch {
+	my $e = shift;
+	BAIL_OUT("not a blessed exception: $e") unless blessed $e;
+	
+	isa_ok $e, Throwable::Factory::Base;
+	is_deeply([$e->FIELDS], ['message']);
+	is($e->description, 'Generic exception');
+};
+
+done_testing;