Files
Nim/tests/stdlib/tstreams.nim
flywind cfb38c2383 move tests to testament (#16101)
* move tests to testament

* minor

* fix random

* disable test random

(cherry picked from commit cbc793b30b)
2020-11-25 12:42:51 +01:00

79 lines
1.5 KiB
Nim

discard """
input: "Arne"
output: '''
Hello! What is your name?
Nice name: Arne
fs is: nil
threw exception
_heh_
'''
nimout: '''
I
AM
GROOT
'''
disabled: "windows"
"""
import streams
block tstreams:
var outp = newFileStream(stdout)
var inp = newFileStream(stdin)
writeLine(outp, "Hello! What is your name?")
var line = readLine(inp)
writeLine(outp, "Nice name: " & line)
block tstreams2:
var
fs = newFileStream("amissingfile.txt")
line = ""
echo "fs is: ",repr(fs)
if not isNil(fs):
while fs.readLine(line):
echo line
fs.close()
block tstreams3:
try:
var fs = openFileStream("shouldneverexist.txt")
except IoError:
echo "threw exception"
static:
var s = newStringStream("I\nAM\nGROOT")
for line in s.lines:
echo line
s.close
# bug #12410
var a = newStringStream "hehohihahuhyh"
a.readDataStrImpl = nil
var buffer = "_ooo_"
doAssert a.readDataStr(buffer, 1..3) == 3
echo buffer
block:
var ss = newStringStream("The quick brown fox jumped over the lazy dog.\nThe lazy dog ran")
assert(ss.getPosition == 0)
assert(ss.peekStr(5) == "The q")
assert(ss.getPosition == 0) # haven't moved
assert(ss.readStr(5) == "The q")
assert(ss.getPosition == 5) # did move
assert(ss.peekLine() == "uick brown fox jumped over the lazy dog.")
assert(ss.getPosition == 5) # haven't moved
var str = newString(100)
assert(ss.peekLine(str))
assert(str == "uick brown fox jumped over the lazy dog.")
assert(ss.getPosition == 5) # haven't moved