#!/usr/bin/env perl use strict; use warnings; use JSON::PP; use FindBin; use lib $FindBin::Bin; use Grains qw(grains_on_square total_grains); my $exercise = 'Grains'; my $test_version = 1; use Test::More tests => 12; my $exercise_version = $exercise->VERSION // 0; if ($exercise_version != $test_version) { warn "\nExercise version mismatch. Further tests may fail!" . "\n$exercise is v$exercise_version. " . "Test is v$test_version.\n"; BAIL_OUT if $ENV{EXERCISM}; } can_ok $exercise, 'import' or BAIL_OUT 'Cannot import subroutines from module'; my $C_DATA = do { local $/; decode_json(); }; my @exception_cases; foreach (@{$C_DATA->{cases}}) { if (exists $_->{cases}) { foreach my $case (@{$_->{cases}}) { if ($case->{property} eq 'square') { if ($case->{expected} == -1) { push @exception_cases, $case; } else { is grains_on_square($case->{input}{square}), $case->{expected}, 'square no. ' . $case->{description}; } } } } elsif ($_->{property} eq 'total') { is total_grains(), $_->{expected}, $_->{description}; } } SKIP: { eval { require Test::Fatal }; skip 'Test::Fatal not loaded', scalar @exception_cases if $@; eval q{ use Test::Fatal qw(dies_ok); dies_ok {grains_on_square($_->{input}{square})} $_->{description} foreach @exception_cases; }; } __DATA__ { "exercise": "grains", "version": "1.1.0", "comments": [ "The final tests of square test error conditions", "The expectation for these tests is -1, indicating an error", "In these cases you should expect an error as is idiomatic for your language" ], "cases": [ { "description": "returns the number of grains on the square", "cases": [ { "description": "1", "property": "square", "input": { "square": 1 }, "expected": 1 }, { "description": "2", "property": "square", "input": { "square": 2 }, "expected": 2 }, { "description": "3", "property": "square", "input": { "square": 3 }, "expected": 4 }, { "description": "4", "property": "square", "input": { "square": 4 }, "expected": 8 }, { "description": "16", "property": "square", "input": { "square": 16 }, "expected": 32768 }, { "description": "32", "property": "square", "input": { "square": 32 }, "expected": 2147483648 }, { "description": "64", "property": "square", "input": { "square": 64 }, "expected": 9223372036854775808 }, { "description": "square 0 raises an exception", "property": "square", "input": { "square": 0 }, "expected": -1 }, { "description": "negative square raises an exception", "property": "square", "input": { "square": -1 }, "expected": -1 }, { "description": "square greater than 64 raises an exception", "property": "square", "input": { "square": 65 }, "expected": -1 } ] }, { "description": "returns the total number of grains on the board", "property": "total", "input": {}, "expected": 18446744073709551615 } ] }