More tests
- calc.t: more tests for invalid syntax - fileio.t was generated a while ago
This commit is contained in:
parent
dd47bfbdf7
commit
b5efbcdff9
2 changed files with 80 additions and 4 deletions
25
t/calc.t
25
t/calc.t
|
@ -6,18 +6,35 @@ use Test::Warnings ":all";
|
|||
|
||||
BEGIN { use_ok('RevBank::Global'); }
|
||||
|
||||
dies_ok sub { parse_amount("-1") };
|
||||
dies_ok sub { parse_amount("0-42") };
|
||||
dies_ok sub { parse_amount("999999") };
|
||||
|
||||
# Invalid syntax
|
||||
is parse_amount(undef), undef;
|
||||
is parse_amount(""), undef;
|
||||
is parse_amount("0.123"), undef;
|
||||
is parse_amount("42.000"), undef;
|
||||
is parse_amount("a"), undef;
|
||||
is parse_amount("(1+1)"), undef;
|
||||
is parse_amount("+"), undef;
|
||||
is parse_amount("-"), undef;
|
||||
is parse_amount("++"), undef;
|
||||
is parse_amount("--"), undef;
|
||||
is parse_amount("+-"), undef;
|
||||
is parse_amount("-+"), undef;
|
||||
is parse_amount("1+"), undef;
|
||||
is parse_amount("1-"), undef;
|
||||
is parse_amount("1 1"), undef;
|
||||
|
||||
# Out of range
|
||||
dies_ok sub { parse_amount("-1") };
|
||||
dies_ok sub { parse_amount("0-42") };
|
||||
dies_ok sub { parse_amount("999999") };
|
||||
|
||||
# Bare number
|
||||
is parse_amount("42")->cents, 4200;
|
||||
is parse_amount("42.0")->cents, 4200;
|
||||
is parse_amount("42.00")->cents, 4200;
|
||||
|
||||
|
||||
# Arithmetic
|
||||
is parse_amount("1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1")->cents, 4200;
|
||||
|
||||
is parse_amount("-42+42")->cents, 0;
|
||||
|
|
59
t/fileio.t
Normal file
59
t/fileio.t
Normal file
|
@ -0,0 +1,59 @@
|
|||
# These tests were written by ChatGPT. All four were actually correct the
|
||||
# first try.
|
||||
|
||||
use Test::More;
|
||||
use File::Temp;
|
||||
|
||||
use RevBank::FileIO;
|
||||
|
||||
# ChatGPT didn't realise that ::FileIO doesn't export its functions
|
||||
use RevBank::Global;
|
||||
|
||||
subtest "slurp" => sub {
|
||||
my $tmp = File::Temp->new();
|
||||
my $data = "foo\nbar\nbaz\n";
|
||||
print $tmp $data;
|
||||
close $tmp;
|
||||
my @lines = slurp($tmp->filename);
|
||||
is_deeply \@lines, ["foo\n", "bar\n", "baz\n"], "slurp works";
|
||||
};
|
||||
|
||||
subtest "spurt" => sub {
|
||||
my $tmp = File::Temp->new();
|
||||
spurt($tmp->filename, "foo\nbar\nbaz\n");
|
||||
open my $fh, "<", $tmp->filename;
|
||||
local $/;
|
||||
my $contents = <$fh>;
|
||||
close $fh;
|
||||
is $contents, "foo\nbar\nbaz\n", "spurt works";
|
||||
};
|
||||
|
||||
subtest "append" => sub {
|
||||
my $tmp = File::Temp->new();
|
||||
spurt($tmp->filename, "foo\n");
|
||||
append($tmp->filename, "bar\n", "baz\n");
|
||||
open my $fh, "<", $tmp->filename;
|
||||
local $/;
|
||||
my $contents = <$fh>;
|
||||
close $fh;
|
||||
is $contents, "foo\nbar\nbaz\n", "append works";
|
||||
};
|
||||
|
||||
subtest "rewrite" => sub {
|
||||
my $tmp = File::Temp->new();
|
||||
spurt($tmp->filename, "foo\nbar\nbaz\n");
|
||||
rewrite($tmp->filename, sub {
|
||||
my ($line) = @_;
|
||||
if ($line =~ /^bar/) {
|
||||
return "quux\n";
|
||||
}
|
||||
return $line;
|
||||
});
|
||||
open my $fh, "<", $tmp->filename;
|
||||
local $/;
|
||||
my $contents = <$fh>;
|
||||
close $fh;
|
||||
is $contents, "foo\nquux\nbaz\n", "rewrite works";
|
||||
};
|
||||
|
||||
done_testing();
|
Loading…
Add table
Reference in a new issue