use v6;
use StringIO;
use Test;

{
    my StringIO $io .= new("f");
    nok $io.eof;
    ok  $io.getc eq "f";
    nok $io.eof;
    nok defined $io.getc;
    ok  $io.eof;
    nok defined $io.getc;
}

{
    my StringIO $io .= new("foo\nbar");
    nok $io.eof;
    ok  $io.get eq "foo";
    nok $io.eof;
    ok  $io.get eq "bar";
    ok  $io.eof;
    nok defined $io.get;
}

{
    my StringIO $io .= new("foo\nbar\nbaz");
    nok $io.eof;
    ok  $io.lines.join("-") eq "foo-bar-baz";
    ok  $io.eof;
    nok $io.lines.Int != 0;
}

{
    my StringIO $io .= new("foo\nbar\nbaz");
    nok $io.eof;
    ok  $io.read(4).decode eq "foo\n";
    nok $io.eof;
    ok  $io.read(2).decode eq "ba";
    nok $io.eof;
    ok  $io.read(3).decode eq "r\nb";
    nok $io.eof;
    ok  $io.read(10).decode eq "az";
    ok  $io.eof;
    nok $io.read(1).bytes != 0;
}

{
    my StringIO $io .= new;

    nok $io.eof;
    ok  $io.write("foobar".encode);
    nok $io.eof;
    ok  $io.data eq "foobar";
    nok $io.eof;
    ok  $io.get eq "foobar";
    ok  $io.eof;

# Writing to an eof'd StringIO uneofs it.
    ok  $io.write("foobar".encode);
    nok $io.eof;
    ok  $io.slurp eq "foobar";
    ok  $io.eof;
# Fails in old rakudo
#    ok  $io.write(''.encode.decode);
    ok  $io.print('');
    nok $io.eof;
}

{
    my StringIO $io .= new;

    nok $io.eof;
    ok  $io.print("foo", "bar", "baz");
    nok $io.eof;
    ok  $io.slurp eq "foobarbaz";
    ok  $io.eof;

    ok  $io.print("foo", "bar", "baz");
    nok $io.eof;
    ok  $io.slurp eq "foobarbaz";
    ok  $io.eof;

    ok  $io.print('', '', '');
    nok $io.eof;
    ok  $io.slurp eq '';
    ok  $io.eof;

    nok defined $io.slurp;
}

{
    my StringIO $io .= new;
    my Str @stuff = <foo bar baz>;

    ok $io.print(@stuff);
    ok $io.data eq "foobarbaz";
}

{
    my StringIO $io .= new;

    ok $io.say("foo", "bar", "baz");
    ok $io.data eq "foobarbaz\n";
}

{
    my StringIO $io .= new;

    nok $io ~~ :d;
    nok $io ~~ :e;
    nok $io ~~ :f;
    nok $io ~~ :l;
    ok  $io ~~ :r;
    nok $io ~~ :s;
    nok $io ~~ :t;
    ok  $io ~~ :w;
    nok $io ~~ :x;
    ok  $io ~~ :z;

    $io.print("foo");

    ok  $io ~~ :s;
    nok $io ~~ :z;
}
