packages feed

zeolite-lang-0.10.0.0: lib/util/tests.0rt

/* -----------------------------------------------------------------------------
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
  require stdout "message"
  exclude stderr "message"
}

unittest test {
  \ LazyStream<Formatted>.new().append("message").writeTo(SimpleOutput.stdout())
}


testcase "stderr writer" {
  success
  require stderr "message"
  exclude stdout "message"
}

unittest test {
  \ LazyStream<Formatted>.new().append("message").writeTo(SimpleOutput.stderr())
}


testcase "error writer" {
  crash
  require "message"
}

unittest test {
  \ LazyStream<Formatted>.new().append("message").writeTo(SimpleOutput.error())
}


testcase "iterate ReadIterator" {
  success
}

unittest forward {
  String s <- "abcde"
  Int count <- 0
  scoped {
    ReadIterator<Char> iter <- ReadIterator:fromReadPosition<Char>(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)
  }
}

unittest reverse {
  String s <- "abcde"
  Int count <- s.readSize()-1
  scoped {
    ReadIterator<Char> iter <- ReadIterator:fromReadPositionAt<Char>(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)
  }
}


testcase "Argv checks" {
  success
  args "arg1" "arg2" "arg3" "arg4"
}

unittest global {
  Int count <- Argv.global().readSize()
  if (count != 5) {
    fail(count)
  }
  \ Testing.checkEquals<?>(Argv.global().readPosition(0),"testcase")
  \ Testing.checkEquals<?>(Argv.global().readPosition(1),"arg1")
  \ Testing.checkEquals<?>(Argv.global().readPosition(2),"arg2")
  \ Testing.checkEquals<?>(Argv.global().readPosition(3),"arg3")
  \ Testing.checkEquals<?>(Argv.global().readPosition(4),"arg4")
}

unittest subSequence {
  ReadPosition<String> argv <- Argv.global().subSequence(2,2)
  Int count <- argv.readSize()
  if (count != 2) {
    fail(count)
  }
  \ Testing.checkEquals<?>(argv.readPosition(0),"arg2")
  \ Testing.checkEquals<?>(argv.readPosition(1),"arg3")
}


testcase "Argv readPosition out of bounds" {
  crash
  require "index 5"
}

unittest test {
  \ Argv.global().readPosition(5)
}


testcase "Argv subSequence out of bounds" {
  crash
  require "index 5"
}

unittest test {
  \ Argv.global().subSequence(5,1)
}


testcase "TextReader checks" {
  success
}

concrete Helper {
  @type checkNext (TextReader,String) -> ()
}

define Helper {
  checkNext (reader,line) {
    if (reader.pastEnd()) {
      fail("past end")
    }
    String value <- reader.readNextLine()
    if (value != line) {
      fail(value)
    }
  }
}

concrete FakeFile {
  refines BlockReader<String>

  @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
  }
}

unittest correctSplit {
    TextReader reader <- TextReader.fromBlockReader(FakeFile.create())
    \ Helper.checkNext(reader,"this is a partial line")
    \ Helper.checkNext(reader,"with some more data")
    \ Helper.checkNext(reader,"and")
    \ Helper.checkNext(reader,"more lines")
    \ Helper.checkNext(reader,"unfinished")
    if (!reader.pastEnd()) {
      fail("not past end")
    }
    if (reader.readNextLine() != "") {
      fail("reused data")
    }
}

unittest readAll {
  String allContents <- TextReader.readAll(FakeFile.create())
  \ Testing.checkEquals<?>(allContents,"this is a partial line\nwith some more data\nand\nmore lines\nunfinished")
}


testcase "ErrorOr checks" {
  success
}

unittest withValue {
  ErrorOr<Int> value <- ErrorOr:value<Int>(10)
  if (value.isError()) {
    fail("Failed")
  }
  \ Testing.checkEquals<?>(value.getValue(),10)
}

unittest withError {
  ErrorOr<String> value <- ErrorOr:error("error message")
  if (!value.isError()) {
    fail("Failed")
  }
  \ Testing.checkEquals<?>(value.getError().formatted(),"error message")
}

unittest convertError {
  scoped {
    ErrorOr<String> error <- ErrorOr:error("error message")
  } in ErrorOr<Int> value <- error.convertError()
  \ Testing.checkEquals<?>(value.getError().formatted(),"error message")
}


testcase "ErrorOr getError() crashes with value" {
  crash
  require "empty"
}

unittest test {
  ErrorOr<Int> value <- ErrorOr:value<Int>(10)
  \ value.getError()
}


testcase "ErrorOr getValue() crashes with error" {
  crash
  require "error message"
}

unittest test {
  ErrorOr<String> value <- ErrorOr:error("error message")
  \ value.getValue()
}