<?php
namespace nur\b\io;

use nur\t\TestCase;

class StringWriterTest extends TestCase {
  function testWrite1() {
    $sw = new StringWriter();
    self::assertSame("", $sw->getString());

    $sw->write();
    self::assertSame("", $sw->getString());

    $sw->write("line");
    self::assertSame("line", $sw->getString());

    $sw->write("line1");
    $sw->write("line2");
    self::assertSame("line1line2", $sw->getString());

    $sw->wnl("line");
    self::assertSame("line\n", $sw->getString());

    $sw->wnl("line1");
    $sw->wnl("line2");
    self::assertSame("line1\nline2\n", $sw->getString());
  }

  function testWrite2() {
    $sw = new StringWriter();
    self::assertSame("", $sw->getString());

    $sw->write();
    self::assertSame("", $sw->getString());

    $sw->write("line");
    self::assertSame("line", $sw->getString());

    $sw->write("line1", "line2");
    self::assertSame("line1line2", $sw->getString());

    $sw->write("line1", null, "line2", false, "line3");
    self::assertSame("line1line2line3", $sw->getString());

    $sw->wnl("line");
    self::assertSame("line\n", $sw->getString());

    $sw->wnl("line1", "line2");
    self::assertSame("line1line2\n", $sw->getString());

    $sw->wnl("line1", null, "line2", false, "line3");
    self::assertSame("line1line2line3\n", $sw->getString());
  }

  function testPrint1() {
    $sw = new StringWriter();
    self::assertSame("", $sw->getString());

    $sw->print();
    self::assertSame("", $sw->getString());

    $sw->print("line");
    self::assertSame("line", $sw->getString());

    $sw->print("line1");
    $sw->print("line2");
    self::assertSame("line1line2", $sw->getString());

    $sw->pnl("line");
    self::assertSame("line\n", $sw->getString());

    $sw->pnl("line1");
    $sw->pnl("line2");
    self::assertSame("line1\nline2\n", $sw->getString());
  }

  function testPrint2() {
    $sw = new StringWriter();
    self::assertSame("", $sw->getString());

    $sw->print();
    self::assertSame("", $sw->getString());

    $sw->print("line");
    self::assertSame("line", $sw->getString());

    $sw->print("line1", "line2");
    self::assertSame("line1 line2", $sw->getString());

    $sw->print("line1", null, "line2", false, "line3");
    self::assertSame("line1 line2 line3", $sw->getString());

    $sw->pnl("line");
    self::assertSame("line\n", $sw->getString());

    $sw->pnl("line1", "line2");
    self::assertSame("line1 line2\n", $sw->getString());

    $sw->pnl("line1", null, "line2", false, "line3");
    self::assertSame("line1 line2 line3\n", $sw->getString());
  }

  function testWriteLines() {
    $sw = new StringWriter();

    $sw->writeLines(null);
    self::assertSame("", $sw->getString());

    $sw->writeLines([]);
    self::assertSame("", $sw->getString());

    $sw->writeLines(["line"]);
    self::assertSame("line\n", $sw->getString());

    $sw->writeLines(["line1", "line2"]);
    self::assertSame("line1\nline2\n", $sw->getString());
  }

  function testIndent() {
    $sw = new StringWriter();
    $sw->pnl("not indented");
    $sw->indent();
    $sw->pnl("level 1a");
    $sw->pnl("level 1b");
    $sw->indent();
    $sw->pnl("level 2a");
    $sw->pnl("level 2b");
    $sw->dedent();
    $sw->pnl("level 1c");
    $sw->dedent();
    $sw->pnl("not indented");
    self::assertSame("not indented\n  level 1a\n  level 1b\n    level 2a\n    level 2b\n  level 1c\nnot indented\n", $sw->getString());

    $sw = new StringWriter();
    $sw->pnl("a1\na2");
    $sw->indent();
    $sw->pnl("b1\nb2");
    $sw->indent();
    $sw->pnl("c1\nc2");
    $sw->dedent();
    $sw->pnl("b3\nb4");
    $sw->dedent();
    $sw->pnl("a3\na4");
    self::assertSame("a1\na2\n  b1\n  b2\n    c1\n    c2\n  b3\n  b4\na3\na4\n", $sw->getString());

    $sw = new StringWriter();
    $sw->print("a1\na2\n");
    $sw->indent();
    $sw->print("b1\nb2\n");
    $sw->indent();
    $sw->print("c1\nc2\n");
    $sw->dedent();
    $sw->print("b3\nb4\n");
    $sw->dedent();
    $sw->print("a3\na4\n");
    self::assertSame("a1\na2\n  b1\n  b2\n    c1\n    c2\n  b3\n  b4\na3\na4\n", $sw->getString());
  }
}