Bug 29857: Unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Mon, 31 Jan 2022 15:06:46 +0000 (12:06 -0300)
committerFridolin Somers <fridolin.somers@biblibre.com>
Wed, 2 Mar 2022 08:47:47 +0000 (22:47 -1000)
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
t/Koha/Exceptions.t

index 56107fc..fc3afb1 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 9;
+use Test::More tests => 10;
 use Test::MockObject;
 use Test::Exception;
 
@@ -336,3 +336,64 @@ subtest 'Koha::Exceptions::Plugin tests' => sub {
     # stringify the exception
     is( "$@", "Calling 'upgrade' died for plugin $plugin_class", 'Exception stringified correctly' );
 };
+
+subtest 'Koha::Exception tests' => sub {
+
+    plan tests => 8;
+
+    use Koha::Exception;
+
+    use Exception::Class (
+        'Koha::Exceptions::Weird' => {
+            isa         => 'Koha::Exception',
+            description => 'Weird exception!',
+            fields      => [ 'a', 'b' ]
+        }
+    );
+
+    my $exception_message = "This is a message";
+
+    throws_ok
+        { Koha::Exceptions::Weird->throw( $exception_message ) }
+        'Koha::Exception',
+        'Exception is thrown :-D';
+
+    is(
+        "$@",
+        "Exception 'Koha::Exceptions::Weird' thrown '$exception_message'\n",
+        'Exception not stringified if manually passed'
+    );
+
+    throws_ok
+        { Koha::Exceptions::Weird->throw( a => "A", b => "B" ) }
+        'Koha::Exception',
+        'Exception is thrown :-D';
+
+    is(
+        "$@",
+        "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!' with a => A, b => B\n",
+        'Exception stringified correctly'
+    );
+
+    throws_ok
+        { Koha::Exceptions::Weird->throw( a => "A" ) }
+        'Koha::Exception',
+        'Exception is thrown :-D';
+
+    is(
+        "$@",
+        "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!' with a => A\n",
+        'Exception stringified correctly, b skipped entirely'
+    );
+
+    throws_ok
+        { Koha::Exceptions::Weird->throw() }
+        'Koha::Exception',
+        'Exception is thrown :-D';
+
+    is(
+        "$@",
+        "Exception 'Koha::Exceptions::Weird' thrown 'Weird exception!'\n",
+        'Exception stringified correctly'
+    );
+};