tests for _load_module
Graham Knop [Tue, 4 Mar 2014 02:46:23 +0000 (21:46 -0500)]
t/lib/BrokenModule.pm [new file with mode: 0644]
t/lib/FalseModule.pm [new file with mode: 0644]
t/lib/TrackLoad.pm [new file with mode: 0644]
t/load-module.t [new file with mode: 0644]

diff --git a/t/lib/BrokenModule.pm b/t/lib/BrokenModule.pm
new file mode 100644 (file)
index 0000000..6271159
--- /dev/null
@@ -0,0 +1,6 @@
+package BrokenModule;
+use strict;
+use warnings;
+
+my $f = blorp;
+1;
diff --git a/t/lib/FalseModule.pm b/t/lib/FalseModule.pm
new file mode 100644 (file)
index 0000000..9e7ae7a
--- /dev/null
@@ -0,0 +1,3 @@
+package FalseModule;
+
+0;
diff --git a/t/lib/TrackLoad.pm b/t/lib/TrackLoad.pm
new file mode 100644 (file)
index 0000000..d3e58c5
--- /dev/null
@@ -0,0 +1,4 @@
+package TrackLoad;
+our $LOADED;
+$LOADED++;
+1;
diff --git a/t/load-module.t b/t/load-module.t
new file mode 100644 (file)
index 0000000..84f1338
--- /dev/null
@@ -0,0 +1,32 @@
+use strict;
+use warnings FATAL => 'all';
+use Test::More;
+use Test::Fatal;
+
+use Role::Tiny ();
+
+use lib 't/lib';
+
+{
+  package TrackLoad;
+  our $LOADED = 0;
+}
+
+Role::Tiny::_load_module('TrackLoad');
+is $TrackLoad::LOADED, 0, 'modules not loaded if symbol table entries exist';
+
+eval { Role::Tiny::_load_module('BrokenModule') };
+like "$@", qr/Compilation failed/,
+  'broken modules throw errors';
+eval { require BrokenModule };
+like "$@", qr/Compilation failed/,
+  ' ... and still fail if required again';
+
+eval { Role::Tiny::_load_module('FalseModule') };
+like "$@", qr/did not return a true value/,
+  'modules returning false throw errors';
+eval { require FalseModule };
+like "$@", qr/did not return a true value/,
+  ' ... and still fail if required again';
+
+done_testing;