<?php
namespace nur\b\io;

use nur\t\TestCase;

class StringReaderTest extends TestCase {
  function testReadLine() {
    $sr = new StringReader();
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString(null);
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("");
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line");
    self::assertSame("line", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line\r\n");
    self::assertSame("line", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line\r");
    self::assertSame("line", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line\n");
    self::assertSame("line", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\nline2");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\nline2\n");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\r\nline2\r\n");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\rline2\n");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\nline2\r");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("line1\rline2\nline3\r\n");
    self::assertSame("line1", $sr->readLine());
    self::assertSame("line2", $sr->readLine());
    self::assertSame("line3", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("\n");
    self::assertSame("", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);

    $sr->setString("\n\n");
    self::assertSame("", $sr->readLine());
    self::assertSame("", $sr->readLine());
    self::assertException(EOFException::class, [$sr, "readLine"]);
  }

  function testReadLines() {
    $sr = new StringReader();
    self::assertSame([], $sr->readLines());

    $sr->setString(null);
    self::assertSame([], $sr->readLines());

    $sr->setString("");
    self::assertSame([], $sr->readLines());

    $sr->setString("line");
    self::assertSame(["line"], $sr->readLines());

    $sr->setString("line\r\n");
    self::assertSame(["line"], $sr->readLines());

    $sr->setString("line\r");
    self::assertSame(["line"], $sr->readLines());

    $sr->setString("line\n");
    self::assertSame(["line"], $sr->readLines());

    $sr->setString("line1\nline2");
    self::assertSame(["line1", "line2"], $sr->readLines());

    $sr->setString("line1\nline2\n");
    self::assertSame(["line1", "line2"], $sr->readLines());

    $sr->setString("line1\r\nline2\r\n");
    self::assertSame(["line1", "line2"], $sr->readLines());

    $sr->setString("line1\rline2\n");
    self::assertSame(["line1", "line2"], $sr->readLines());

    $sr->setString("line1\nline2\r");
    self::assertSame(["line1", "line2"], $sr->readLines());

    $sr->setString("line1\rline2\nline3\r\n");
    self::assertSame(["line1", "line2", "line3"], $sr->readLines());

    $sr->setString("\n");
    self::assertSame([""], $sr->readLines());

    $sr->setString("\n\n");
    self::assertSame(["", ""], $sr->readLines());
  }
}
