diff options
Diffstat (limited to 't')
-rw-r--r-- | t/00use.t | 10 | ||||
-rw-r--r-- | t/01point.t | 31 | ||||
-rw-r--r-- | t/02scope.t | 24 | ||||
-rw-r--r-- | t/03readonly.t | 20 | ||||
-rw-r--r-- | t/04named.t | 44 | ||||
-rw-r--r-- | t/99pod.t | 11 |
6 files changed, 140 insertions, 0 deletions
diff --git a/t/00use.t b/t/00use.t new file mode 100644 index 0000000..4812811 --- /dev/null +++ b/t/00use.t @@ -0,0 +1,10 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +use_ok( "Struct::Dumb" ); + +done_testing; diff --git a/t/01point.t b/t/01point.t new file mode 100644 index 0000000..2e95949 --- /dev/null +++ b/t/01point.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; +use Test::Fatal; + +use Struct::Dumb; + +struct Point => [qw( x y )]; + +my $point = Point(10, 20); +ok( ref $point, '$point is a ref' ); + +can_ok( $point, "x" ); + +is( $point->x, 10, '$point->x is 10' ); + +$point->y = 30; +is( $point->y, 30, '$point->y is 30 after mutation' ); + +like( exception { $point->z }, + qr/^main::Point does not have a 'z' field at \S+ line \d+\.?\n/, + '$point->z throws exception' ); + +like( exception { Point(30) }, + qr/^usage: main::Point\(\$x, \$y\) at \S+ line \d+\.?\n/, + 'Point(30) throws usage exception' ); + +done_testing; diff --git a/t/02scope.t b/t/02scope.t new file mode 100644 index 0000000..f2acb1e --- /dev/null +++ b/t/02scope.t @@ -0,0 +1,24 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +package Foo; +use Struct::Dumb; +struct Point => [qw( x y )]; + +package Bar; +use Struct::Dumb; +struct Point => [qw( x y z )]; + +package main; + +my $point2 = Foo::Point(10, 20); +my $point3 = Bar::Point(10, 20, 30); + +ok( !$point2->can( "z" ), '$point2 cannot ->z' ); +can_ok( $point3, "z" ); + +done_testing; diff --git a/t/03readonly.t b/t/03readonly.t new file mode 100644 index 0000000..57e938a --- /dev/null +++ b/t/03readonly.t @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; +use Test::Fatal; + +use Struct::Dumb qw( readonly_struct ); + +readonly_struct Point => [qw( x y )]; + +my $point = Point(10, 20); + +is( $point->x, 10, '$point->x is 10' ); + +ok( exception { $point->y = 30 }, + '$point->y throws exception on readonly_struct' ); + +done_testing; diff --git a/t/04named.t b/t/04named.t new file mode 100644 index 0000000..6735099 --- /dev/null +++ b/t/04named.t @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; +use Test::Fatal; + +use Struct::Dumb; + +struct Colour => [qw( red green blue )], named_constructor => 1; + +{ + my $colour = Colour( red => 1, green => 0, blue => 0 ); + + can_ok( $colour, "red" ); + is( $colour->red, 1, '$colour->red is 1' ); +} + +{ + my $colour = Colour( green => 1, blue => 0.5, red => 0 ); + + is( $colour->blue, 0.5, '$colour->blue is 0.5' ); +} + +{ + package named::default; + use Struct::Dumb qw( -named_constructors ); + + struct Point3D => [qw( x y z )]; + + my $point = Point3D( x => 1, z => 3, y => 2 ); + ::is( $point->z, 3, '$point->z from default named constructor' ); +} + +like( exception { Colour( red => 0, green => 0 ) }, + qr/^usage: main::Colour requires 'blue' at \S+ line \d+\.?\n/, + 'Colour() without blue throws usage exception' ); + +like( exception { Colour( red => 0, green => 0, blue => 0, yellow => 1 ) }, + qr/^usage: main::Colour does not recognise 'yellow' at \S+ line \d+\.?\n/, + 'Colour() with yellow throws usage exception' ); + +done_testing; diff --git a/t/99pod.t b/t/99pod.t new file mode 100644 index 0000000..eb319fb --- /dev/null +++ b/t/99pod.t @@ -0,0 +1,11 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +eval "use Test::Pod 1.00"; +plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; + +all_pod_files_ok(); |