Add argument type checking tests for io library

This commit is contained in:
James Roseborough
2008-12-05 01:47:50 +00:00
parent b54ef775d2
commit 5e734e1650
4 changed files with 174 additions and 49 deletions

View File

@@ -0,0 +1,68 @@
package.path = "?.lua;src/test/errors/?.lua"
require 'args'
-- arg type tests for io library functions
local f
-- io.close ([file])
banner('io.close')
f = io.open("abc.txt","w")
checkallpass('io.close',{{f}})
checkallerrors('io.close',{notanil},'bad argument #1')
-- io.input ([file])
checkallpass('io.input',{{nil,f,"abc.txt"}})
checkallerrors('io.input',{nonstring},'bad argument #1')
-- io.lines ([filename])
io.input("abc.txt")
checkallpass('io.lines',{{nil,"abc.txt"}})
checkallerrors('io.lines',{{f}},'bad argument #1')
checkallerrors('io.lines',{nonstring},'bad argument #1')
-- io.open (filename [, mode])
checkallpass('io.open',{{"abc.txt"},{nil,"r","w","a","r+","w+","a+"}})
checkallerrors('io.open',{notastring},'bad argument #1')
checkallerrors('io.open',{{"abc.txt"},{nonstring}},'bad argument #2')
-- io.output ([file])
checkallpass('io.output',{{nil,f,"abc.txt"}})
checkallerrors('io.output',{nonstring},'bad argument #1')
-- io.popen (prog [, mode])
checkallpass('io.popen',{{"hostname"},{nil,"r","w","a","r+","w+","a+"}})
checkallerrors('io.popen',{notastring},'bad argument #1')
checkallerrors('io.popen',{{"hostname"},{nonstring}},'bad argument #2')
-- io.read (···)
local areadfmt = {2,"*n","*a","*l","3"}
checkallpass('io.read',{})
checkallpass('io.read',{areadfmt})
checkallpass('io.read',{areadfmt,areadfmt})
checkallerrors('io.read',{{aboolean,afunction,atable}},'bad argument #1')
-- io.read (···)
checkallpass('io.write',{})
checkallpass('io.write',{somestring})
checkallpass('io.write',{somestring,somestring})
checkallerrors('io.write',{nonstring},'bad argument #1')
checkallerrors('io.write',{somestring,nonstring},'bad argument #2')
-- file:write ()
file = io.open("seektest.txt","w")
checkallpass('file.write',{{file},somestring})
checkallpass('file.write',{{file},somestring,somestring})
checkallerrors('file.write',{},'bad argument #1')
checkallerrors('file.write',{{file},nonstring},'bad argument #1')
checkallerrors('file.write',{{file},somestring,nonstring},'bad argument #2')
pcall( file.close, file )
-- file:seek ([whence] [, offset])
file = io.open("seektest.txt","r")
checkallpass('file.seek',{{file}})
checkallpass('file.seek',{{file},{"set","cur","end"}})
checkallpass('file.seek',{{file},{"set","cur","end"},{2,"3"}})
checkallerrors('file.seek',{},'bad argument #1')
checkallerrors('file.seek',{{file},nonstring},'bad argument #1')
checkallerrors('file.seek',{{file},{"set","cur","end"},nonnumber},'bad argument #2')

View File

@@ -24,3 +24,40 @@ for i,v in ipairs(t) do
print( string.format("%q",tostring(v)), type(v))
end
local h = io.open("abc.txt", "a")
print( 'h', io.type(h) )
print( 'write', h:write('\nmore text\neven more text\n') )
print( 'close', h:close() )
local j = io.open( "abc.txt", "r" )
print( 'j', io.type(j) )
print( 'seek', j:seek("set", 3) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("set", 2) )
print( 'read', j:read(4), j:read(3) )
print( 'seek', j:seek("cur", -8 ) )
print( 'read', j:read(4), j:read(3) )
print( 'seek(cur,0)', j:seek("cur",0) )
print( 'seek(cur,20)', j:seek("cur",20) )
print( 'seek(end,-5)', j:seek("end", -5) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
print( 'read(4)', string.format("%q", tostring(j:read(4))) )
for l in io.lines("abc.txt") do
print( string.format('%q',l) )
end
io.input("abc.txt")
for l in io.lines() do
print( string.format('%q',l) )
end
io.input(io.open("abc.txt","r"))
for l in io.lines() do
print( string.format('%q',l) )
end
io.input("abc.txt")
io.input(io.input())
for l in io.lines() do
print( string.format('%q',l) )
end