/* ----------------------------------------------------------------------------- Copyright 2020 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] testcase "stdout writer" { success Test$run() require stdout "message" exclude stderr "message" } define Test { run () { \ LazyStream$new().append("message").writeTo(SimpleOutput$stdout()) } } concrete Test { @type run () -> () } testcase "stderr writer" { success Test$run() require stderr "message" exclude stdout "message" } define Test { run () { \ LazyStream$new().append("message").writeTo(SimpleOutput$stderr()) } } concrete Test { @type run () -> () } testcase "error writer" { crash Test$run() require "message" } define Test { run () { \ LazyStream$new().append("message").writeTo(SimpleOutput$error()) } } concrete Test { @type run () -> () } testcase "iterate String forward" { success Test$run() } define Test { run () { String s <- "abcde" Int count <- 0 scoped { ReadIterator iter <- ReadIterator$$fromReadPosition(s) } in while (!iter.pastForwardEnd()) { if (iter.readCurrent() != s.readPosition(count)) { fail(iter.readCurrent()) } } update { count <- count+1 iter <- iter.forward() } if (count != s.readSize()) { fail(count) } } } concrete Test { @type run () -> () } testcase "iterate String reverse" { success Test$run() } define Test { run () { String s <- "abcde" Int count <- s.readSize()-1 scoped { ReadIterator iter <- ReadIterator$$fromReadPositionAt(s,count) } in while (!iter.pastReverseEnd()) { if (iter.readCurrent() != s.readPosition(count)) { fail(iter.readCurrent()) } } update { count <- count-1 iter <- iter.reverse() } if (count != -1) { fail(count) } } } concrete Test { @type run () -> () } testcase "Argv is set and available" { success Test$run() require stdout "/testcase$" } define Test { run () { Int count <- Argv$global().readSize() if (count != 1) { fail(count) } String name <- Argv$global().readPosition(0) \ LazyStream$new() .append(name) .append("\n") .writeTo(SimpleOutput$stdout()) } } concrete Test { @type run () -> () } testcase "TextReader splits lines correctly" { success Test$run() } concrete FakeFile { refines BlockReader @type create () -> (FakeFile) } define FakeFile { @value Int counter create () { return FakeFile{ 0 } } readBlock (_) { cleanup { counter <- counter+1 } in if (counter == 0) { return "this is a " } elif (counter == 1) { return "partial line\nwith " } elif (counter == 2) { return "some more data\nand\nmore lines" } elif (counter == 3) { return "\nunfinished" } else { fail("too many reads") } } pastEnd () { return counter > 3 } } define Test { run () { TextReader reader <- TextReader$fromBlockReader(FakeFile$create()) \ checkNext(reader,"this is a partial line") \ checkNext(reader,"with some more data") \ checkNext(reader,"and") \ checkNext(reader,"more lines") \ checkNext(reader,"unfinished") if (!reader.pastEnd()) { fail("not past end") } if (reader.readNextLine() != "") { fail("reused data") } } @type checkNext (TextReader,String) -> () checkNext (reader,line) { if (reader.pastEnd()) { fail("past end") } String value <- reader.readNextLine() if (value != line) { fail(value) } } } concrete Test { @type run () -> () } testcase "TextReader readAll" { success Test$run() } concrete FakeFile { refines BlockReader @type create () -> (FakeFile) } define FakeFile { @value Int counter create () { return FakeFile{ 0 } } readBlock (_) { cleanup { counter <- counter+1 } in if (counter == 0) { return "this is a " } elif (counter == 1) { return "partial line\nwith " } elif (counter == 2) { return "some more data\nand\nmore lines" } elif (counter == 3) { return "\nunfinished" } else { fail("too many reads") } } pastEnd () { return counter > 3 } } define Test { run () { String allContents <- TextReader$readAll(FakeFile$create()) \ Testing$check(allContents,"this is a partial line\nwith some more data\nand\nmore lines\nunfinished") } } concrete Test { @type run () -> () } testcase "ErrorOr value" { success Test$run() } define Test { run () { ErrorOr value <- ErrorOr$$value(10) if (value.isError()) { fail("Failed") } \ Testing$check(value.getValue(),10) } } concrete Test { @type run () -> () } testcase "ErrorOr getError() crashes with value" { crash Test$run() require "empty" } define Test { run () { ErrorOr value <- ErrorOr$$value(10) \ value.getError() } } concrete Test { @type run () -> () } testcase "ErrorOr error" { success Test$run() } define Test { run () { ErrorOr value <- ErrorOr$$error("error message") if (!value.isError()) { fail("Failed") } \ Testing$check(value.getError().formatted(),"error message") } } concrete Test { @type run () -> () } testcase "ErrorOr getValue() crashes with error" { crash Test$run() require "error message" } define Test { run () { ErrorOr value <- ErrorOr$$error("error message") \ value.getValue() } } concrete Test { @type run () -> () } testcase "ErrorOr convert error" { success Test$run() } define Test { run () { scoped { ErrorOr error <- ErrorOr$$error("error message") } in ErrorOr value <- error.convertError() \ Testing$check(value.getError().formatted(),"error message") } } concrete Test { @type run () -> () }