Skip to content

Commit

Permalink
[io grant] Expand &open tests
Browse files Browse the repository at this point in the history
Add tests for argument combinations:
- :r
- :a
- :ra
- :append
- :create
- :append, :create
- :truncate, :create
- :exclusive
- Tests that :bin, :enc, :chomp, :nl-in, and :nl-out are respected
  • Loading branch information
zoffixznet committed Mar 26, 2017
1 parent f8cd2fc commit 091931a
Showing 1 changed file with 174 additions and 2 deletions.
176 changes: 174 additions & 2 deletions S32-io/open.t
@@ -1,7 +1,7 @@
use v6;
use Test;

plan 27;
plan 62;

my \PATH = 't-S32-io-open.tmp';
my \PATH-RX = rx/'t-S32-io-open.tmp'/;
Expand Down Expand Up @@ -41,9 +41,11 @@ LEAVE unlink PATH;

$fh = open PATH, :mode<wo>;
ok defined($fh), 'can open existing file in mode wo';

throws-like '$fh.get', Exception, 'cannot read from file in mode wo';

throws-like { open PATH, :mode<meows> }, Exception, :message(/meows/),
'using an invalid mode throws';

$fh.close;
}

Expand Down Expand Up @@ -168,4 +170,174 @@ LEAVE unlink PATH;
$fh.close;
}

# test :r mode
{ unlink PATH;
my $fh;

isa-ok open(PATH, :r), Failure,
'opening non-existent file in :r mode fails';

# fill file with data
$fh = open PATH, :w; $fh.print('onions are tasty'); $fh.close;

$fh = open PATH, :r;
ok defined($fh), 'can open existing file in :r mode';
is $fh.lines.join, 'onions are tasty', 'can read from file in :r mode';
throws-like '$fh.print("42")', Exception, 'cannot write to file in :r mode';

$fh.close;
}

# test :a mode
{ unlink PATH;
my $fh;

$fh = open PATH, :a;
ok defined($fh), 'can open non-existent file in :a mode';
$fh.print('onions are tasty');
$fh.close;

$fh = open PATH, :a;
ok defined($fh), 'can open existing file in :a mode';
$fh.print('cats say meow');

throws-like { $fh.get }, Exception, 'trying to read in :a mode throws';
$fh.close;

$fh = open PATH, :r;
is $fh.lines.join, 'onions are tastycats say meow',
':a mode appends to existing data';

$fh.close;
}

# test :append mode
{ unlink PATH;
my $mode = ':append';
my $fh;

isa-ok open(PATH, :append), Failure,
"opening non-existent file in $mode mode fails";

# create and fill file with data
$fh = open PATH, :w; $fh.print('onions are tasty'); $fh.close;

$fh = open PATH, :append;
is $fh.lines.join, 'onions are tasty', "$mode can read from existing files";
throws-like { $fh.say: 'foo' }, Exception,
"trying to write in $mode mode throws";
$fh.close;
}

# test :append, :create mode
{ unlink PATH;
my $mode = ':append, :create';
my $fh;

$fh = open PATH, :append, :create;
ok defined($fh), "can open non-existent file in $mode mode";
ok PATH.IO.e, "$mode mode creates non-existent files";
$fh.close;

# create and fill file with data
$fh = open PATH, :w; $fh.print('onions are tasty'); $fh.close;

$fh = open PATH, :append, :create;
is $fh.lines.join, 'onions are tasty', "$mode can read from existing files";
throws-like { $fh.say: 'foo' }, Exception,
"trying to write in $mode mode throws";
$fh.close;
}

# test :ra mode
{ unlink PATH;
my $fh;

$fh = open PATH, :ra;
ok defined($fh), 'can open non-existent file in :ra mode';
$fh.print('onions are tasty');
$fh.close;

$fh = open PATH, :ra;
ok defined($fh), 'can open existing file in :ra mode';
$fh.print('cats say meow');

$fh.seek(0, SeekFromBeginning);
is $fh.lines.join, 'onions are tastycats say meow',
'can read in :append mode and it appends to existing data';

$fh.close;
}

# test :create mode
{ unlink PATH;
my $fh;

$fh = open PATH, :create;
ok defined($fh), 'can open non-existent file in :create mode';
throws-like { $fh.say: 'foo' }, Exception,
'trying to write in :create mode throws';

is $fh.lines.join, '', 'file starts out empty when using :create';
$fh.close;

# fill file with data
$fh = open PATH, :w; $fh.print('onions are tasty'); $fh.close;

$fh = open PATH, :create;
is $fh.lines.join, 'onions are tasty',
':create mode does not truncate existing files';

$fh.close;
}

# test :truncate :create mode
{ unlink PATH;
my $mode = ':truncate, :create';
my $fh;

$fh = open PATH, :truncate, :create;
ok defined($fh), "can open non-existent file in $mode mode";
ok PATH.IO.e, "$mode mode creates non-existent files";
$fh.close;

# create and fill file with data
$fh = open PATH, :w; $fh.print('onions are tasty'); $fh.close;

$fh = open PATH, :truncate, :create;
is $fh.lines.join, '', "$mode mode truncates existing files";

throws-like { $fh.say: 'foo' }, Exception,
"trying to write in $mode mode throws";

$fh.close;
}

# test :exclusive mode
{ unlink PATH;
isa-ok open(PATH, :exclusive), Failure,
"opening non-existent file in :exclusive mode fails";
}

# test attribute setting
{ unlink PATH;

with open PATH, :w, :bin, :enc<e>, :!chomp, :nl-in[<a b>], :nl-out<meow> {
LEAVE .close;
is-deeply .chomp, False, 'can set $!chomp to False with open';
is-deeply .nl-in, [<a b>], 'can set $!nl-in to Array with open';
is-deeply .nl-out, 'meow', 'can set $!nl-out with open';
is-deeply .encoding, 'bin', ':bin overrides :enc';
}

with IO::Handle.new(:path(PATH.IO), :!chomp)
.open: :w, :enc<iso-8859-1>, :chomp, :nl-in('a')
{
LEAVE .close;
is-deeply .chomp, True, 'can set $!chomp to True with open';
is-deeply .nl-in, 'a', 'can set $!nl-in to Str with open';
is-deeply .encoding, 'iso-8859-1', 'can set $!encoding with ';
}
}

# vim: ft=perl6

0 comments on commit 091931a

Please sign in to comment.