1 -- test script for wslua Dir functions
3 local testlib
= require("testlib")
5 testlib
.init( { [OTHER
] = 0 } )
7 ------------- helper funcs ------------
9 -- the following are so we can use pcall (which needs a function to call)
10 local function callDirFuncBase(name
, t
)
11 t
.result
= Dir
[name
]()
15 local function callDirFunc(name
, val
, t
)
16 t
.result
= Dir
[name
](val
)
20 local function makeFile(filename
)
21 local f
= io
.open(filename
, "w")
23 error ("failed to make file"..filename
.." in directory\n"..
24 "make sure to delete 'temp' directory before running again")
26 f
:write("fooobarrloo")
31 --------------------------
33 -- for our called function results
36 testlib
.testing("Dir basics")
38 testlib
.test(OTHER
,"global", _G
.Dir
~= nil)
39 testlib
.test(OTHER
,"global", type(Dir
.make
) == 'function')
40 testlib
.test(OTHER
,"global", type(Dir
.remove) == 'function')
41 testlib
.test(OTHER
,"global", type(Dir
.remove_all
) == 'function')
42 testlib
.test(OTHER
,"global", type(Dir
.open
) == 'function')
43 testlib
.test(OTHER
,"global", type(Dir
.close
) == 'function')
44 testlib
.test(OTHER
,"global", type(Dir
.exists
) == 'function')
45 testlib
.test(OTHER
,"global", type(Dir
.personal_config_path
) == 'function')
46 testlib
.test(OTHER
,"global", type(Dir
.global_config_path
) == 'function')
47 testlib
.test(OTHER
,"global", type(Dir
.personal_plugins_path
) == 'function')
48 testlib
.test(OTHER
,"global", type(Dir
.global_plugins_path
) == 'function')
50 testlib
.testing("Dir paths/filenames")
52 testlib
.test(OTHER
,"Dir.__FILE__", __FILE__
~= nil)
53 testlib
.test(OTHER
,"Dir.__DIR__", __DIR__
~= nil)
54 testlib
.test(OTHER
,"Dir.exists", pcall(callDirFunc
, "exists", "temp", t
))
55 testlib
.test(OTHER
,"Dir.personal_config_path", pcall(callDirFuncBase
, "personal_config_path", t
))
56 testlib
.test(OTHER
,"Dir.global_config_path", pcall(callDirFuncBase
, "global_config_path", t
))
57 testlib
.test(OTHER
,"Dir.personal_plugins_path", pcall(callDirFuncBase
, "personal_plugins_path", t
))
58 testlib
.test(OTHER
,"Dir.global_plugins_path", pcall(callDirFuncBase
, "global_plugins_path", t
))
60 -- Users expect trailing slashes for DATA_DIR and USER_DIR (bug 14619).
61 local dirsep
= package
.config
:sub(1,1)
62 testlib
.test(OTHER
,"DATA_DIR", string.sub(DATA_DIR
, -1) == dirsep
)
63 testlib
.test(OTHER
,"USER_DIR", string.sub(USER_DIR
, -1) == dirsep
)
65 print("\nFor your information, I got the following info:\n")
66 print("__FILE__ = '" .. __FILE__
.. "'")
67 print("__DIR__ = '" .. __DIR__
.. "'")
68 print("personal_config_path = '" .. Dir
.personal_config_path() .. "'")
69 print("global_config_path = '" .. Dir
.global_config_path() .. "'")
70 print("personal_plugins_path = '" .. Dir
.personal_plugins_path() .. "'")
71 print("global_plugins_path = '" .. Dir
.global_plugins_path() .. "'")
74 testlib
.testing("Directory manipulation")
76 testlib
.test(OTHER
,"Dir.exists", pcall(callDirFunc
, "exists", "temp", t
))
78 if t
.result
== true or t
.result
== false then
79 error("this testsuite requires there be no 'temp' directory or file; please remove it")
82 testlib
.testing("Dir.make")
84 testlib
.test(OTHER
,"Dir.make", pcall(callDirFunc
, "make", "temp", t
) and t
.result
== true)
85 testlib
.test(OTHER
,"Dir.exists", pcall(callDirFunc
, "exists", "temp", t
) and t
.result
== true)
86 -- make the same dir, should give false
87 testlib
.test(OTHER
,"Dir.make", pcall(callDirFunc
, "make", "temp", t
) and t
.result
== false)
89 testlib
.testing("Dir.remove")
91 testlib
.test(OTHER
,"Dir.remove", pcall(callDirFunc
, "remove", "temp", t
) and t
.result
== true)
92 testlib
.test(OTHER
,"Dir.exists", pcall(callDirFunc
, "exists", "temp", t
) and t
.result
== nil)
93 testlib
.test(OTHER
,"Dir.remove", pcall(callDirFunc
, "remove", "temp", t
) and t
.result
== false)
96 makeFile("temp/file.txt")
98 -- will return nil because temp has a file
99 testlib
.test(OTHER
,"Dir.remove", pcall(callDirFunc
, "remove", "temp", t
) and t
.result
== nil)
101 testlib
.testing("Dir.remove_all")
103 testlib
.test(OTHER
,"Dir.remove_all", pcall(callDirFunc
, "remove_all", "temp", t
) and t
.result
== true)
104 testlib
.test(OTHER
,"Dir.remove_all", pcall(callDirFunc
, "remove_all", "temp", t
) and t
.result
== false)
107 makeFile("temp/file1.txt")
108 makeFile("temp/file2.txt")
109 makeFile("temp/file3.txt")
110 testlib
.test(OTHER
,"Dir.remove_all", pcall(callDirFunc
, "remove_all", "temp", t
) and t
.result
== true)
111 testlib
.test(OTHER
,"Dir.remove_all", pcall(callDirFunc
, "remove_all", "temp", t
) and t
.result
== false)
113 testlib
.testing("Dir.open")
116 makeFile("temp/file1.txt")
117 makeFile("temp/file2.txt")
118 makeFile("temp/file3.txt")
119 testlib
.test(OTHER
,"Dir.open", pcall(callDirFunc
, "open", "temp", t
))
120 testlib
.test(OTHER
,"Dir.open", type(t
.result
) == 'userdata')
121 testlib
.test(OTHER
,"Dir.open", typeof(t
.result
) == 'Dir')
123 io
.stdout
:write("calling Dir object...")
127 io
.stdout
:write("passed\n")
131 testlib
.test(OTHER
,"Dir.call", files
["file1.txt"])
132 testlib
.test(OTHER
,"Dir.call", files
["file2.txt"])
133 testlib
.test(OTHER
,"Dir.call", files
["file3.txt"])
134 testlib
.test(OTHER
,"Dir.call", dir() == nil)
135 testlib
.test(OTHER
,"Dir.call", dir() == nil)
137 testlib
.testing("Dir.close")
139 testlib
.test(OTHER
,"Dir.close", pcall(callDirFunc
, "close", dir
, t
))
140 testlib
.test(OTHER
,"Dir.close", pcall(callDirFunc
, "close", dir
, t
))
142 testlib
.testing("Negative testing 1")
143 -- now try breaking it
144 testlib
.test(OTHER
,"Dir.open", pcall(callDirFunc
, "open", "temp", t
))
150 Dir
.remove_all("temp")
155 testlib
.test(OTHER
,"Dir.call", files
["file1.txt"])
156 testlib
.test(OTHER
,"Dir.call", files
["file2.txt"])
157 testlib
.test(OTHER
,"Dir.call", files
["file3.txt"])
158 testlib
.test(OTHER
,"Dir.close", pcall(callDirFunc
, "close", dir
, t
))
160 testlib
.testing("Negative testing 2")
161 -- do it again, but this time don't do dir() until after removing the files
163 makeFile("temp/file1.txt")
164 makeFile("temp/file2.txt")
165 makeFile("temp/file3.txt")
167 testlib
.test(OTHER
,"Dir.open", pcall(callDirFunc
, "open", "temp", t
))
170 Dir
.remove_all("temp")
173 testlib
.test(OTHER
,"Dir.call", file
== nil)
174 testlib
.test(OTHER
,"Dir.close", pcall(callDirFunc
, "close", dir
, t
))
178 testlib
.testing("Negative testing 3")
181 testlib
.test(OTHER
,"Dir.make", not pcall(callDirFunc
, "make", {}, t
))
182 testlib
.test(OTHER
,"Dir.make", not pcall(callDirFunc
, "make", nil, t
))
183 testlib
.test(OTHER
,"Dir.remove", not pcall(callDirFunc
, "remove", {}, t
))
184 testlib
.test(OTHER
,"Dir.remove", not pcall(callDirFunc
, "remove", nil, t
))
185 testlib
.test(OTHER
,"Dir.remove_all", not pcall(callDirFunc
, "remove_all", {}, t
))
186 testlib
.test(OTHER
,"Dir.remove_all", not pcall(callDirFunc
, "remove_all", nil, t
))
187 testlib
.test(OTHER
,"Dir.open", not pcall(callDirFunc
, "open", {}, t
))
188 testlib
.test(OTHER
,"Dir.open", not pcall(callDirFunc
, "open", nil, t
))
189 testlib
.test(OTHER
,"Dir.close", not pcall(callDirFunc
, "close", "dir", t
))
190 testlib
.test(OTHER
,"Dir.close", not pcall(callDirFunc
, "close", nil, t
))
193 print("\n-----------------------------\n")