From feca9d5c111870bf4635434185457033f278a51a Mon Sep 17 00:00:00 2001 From: Ryan Shepherd Date: Tue, 25 Sep 2018 20:28:51 -0400 Subject: [PATCH] Basic automated testing framework --- PerlRPG/Console.pm | 1 + PerlRPG/Script.pm | 2 +- Tests/Script.pl | 30 ++++++++++++++++++++++++++++++ runtests.pl | 25 +++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 Tests/Script.pl create mode 100755 runtests.pl diff --git a/PerlRPG/Console.pm b/PerlRPG/Console.pm index 1f4a118..2cb79f2 100644 --- a/PerlRPG/Console.pm +++ b/PerlRPG/Console.pm @@ -16,6 +16,7 @@ use constant { ERROR => 5, CRIT => 6, FATAL => 7, + NOLOG => 99, }; my @debugnames=qw/DEVALL DEBUG INFO STATUS WARN ERROR CRIT FATAL/; $StackTraceLevel = ERROR; diff --git a/PerlRPG/Script.pm b/PerlRPG/Script.pm index 9ea4dd0..19c0ce4 100644 --- a/PerlRPG/Script.pm +++ b/PerlRPG/Script.pm @@ -13,7 +13,7 @@ use PerlRPG::Drawing; use vars qw/@ISA @EXPORT @EXPORT_OK/; @ISA = qw/Exporter/; -@EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar/; +@EXPORT = qw/CompileScripts RunScript RunScriptTick GetGameVar SetGameVar DefGameVar EvalString/; @EXPORT_OK = @EXPORT; my %labels; diff --git a/Tests/Script.pl b/Tests/Script.pl new file mode 100755 index 0000000..1a46c33 --- /dev/null +++ b/Tests/Script.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl -w +use strict; +use warnings; +use PerlRPG::Console; +use PerlRPG::Script; + + +use Test::More; + +$PerlRPG::Console::LogLevel = ERROR; +ok( !GetGameVar("TestVar"), "Undefined GetGameVar should fail"); +ok( !SetGameVar("TestVar"), "Undefined SetGameVar should fail"); +$PerlRPG::Console::LogLevel = WARN; +DefGameVar('TestVar', 1); +ok( GetGameVar("TestVar") == 1, "Defined GetGameVar should be 1"); +ok( SetGameVar("TestVar", 2) == 2, "Defined SetGameVar(2) should be 2"); +DefGameVar('TestVar', 1); +ok( SetGameVar("TestVar", 2) == 2, "Redefined GameVar=1 should still be 2"); + +PerlRPG::Script::RunScriptLine("test", 1, "TestVar = TestVar + 1"); +ok( GetGameVar("TestVar")==3, "RunScriptLine sets TestVar to 3"); + +ok( PerlRPG::Script::IsString("\"foo\""), "'\"foo\"' is a string"); +ok( PerlRPG::Script::IsString("foo")==0, "'foo' is not a string"); +ok( PerlRPG::Script::IsString(5)==0, "'5' is not a string"); +ok( EvalString("\"[TestVar]\"") eq '3', "\"[TestVar]\" is 3"); +ok( EvalString("3 + 3") == 6, "\"3 + 3\" == 6"); +ok( EvalString("lc('FoO') . uc('FoO')") eq 'fooFOO', "String function test"); + +done_testing(); diff --git a/runtests.pl b/runtests.pl new file mode 100755 index 0000000..76ce4d9 --- /dev/null +++ b/runtests.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use strict; + +$|=1; + +my @tests=(); +my($longest) = sort { $b <=> $a } map { length($_) } (@tests); +my $failed=0; + +foreach my $test () { + printf("Running %-*s... ", $longest, $test); + my $r = system("$test > /dev/null 2>\&1"); + if($r) { + print "Failed!\n"; + $failed++; + } else { + print "OK!\n"; + } +} + +if($failed) { + print "\nFailed $failed tests\n"; +} else { + print "\nAll tests passed!\n"; +}