testfx.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. --
  2. -- tests/testfx.lua
  3. -- Automated test framework for Premake.
  4. -- Copyright (c) 2008-2009 Jason Perkins and the Premake project
  5. --
  6. --
  7. -- Define a namespace for the testing functions
  8. --
  9. test = { }
  10. --
  11. -- Assertion functions
  12. --
  13. function test.string_contains(buffer, expected)
  14. if not string.find(buffer,expected) then
  15. test.fail("\n==Fail==: Expected to find :\n%s\nyet it was not found in buffer:\n%s\n", expected,buffer)
  16. end
  17. end
  18. function test.string_does_not_contain(buffer, expected)
  19. if string.find(buffer,expected) then
  20. test.fail("\n==Fail==: Did not expected to find :\n%s\nyet it was found in buffer:\n%s\n", expected,buffer)
  21. end
  22. end
  23. function test.capture(expected)
  24. local actual = io.endcapture()
  25. local ait = actual:gfind("(.-)" .. io.eol)
  26. local eit = expected:gfind("(.-)\n")
  27. local linenum = 1
  28. local atxt = ait()
  29. local etxt = eit()
  30. while etxt do
  31. if (etxt ~= atxt) then
  32. test.fail("(%d) expected:\n%s\n...but was:\n%s", linenum, etxt, atxt)
  33. end
  34. linenum = linenum + 1
  35. atxt = ait()
  36. etxt = eit()
  37. end
  38. end
  39. function test.closedfile(expected)
  40. if expected and not test.value_closedfile then
  41. test.fail("expected file to be closed")
  42. elseif not expected and test.value_closedfile then
  43. test.fail("expected file to remain open")
  44. end
  45. end
  46. function test.contains(value, expected)
  47. if not table.contains(value, expected) then
  48. test.fail("expected value %s not found", expected)
  49. end
  50. end
  51. function test.fail(format, ...)
  52. -- convert nils into something more usefuls
  53. for i = 1, arg.n do
  54. if (arg[i] == nil) then
  55. arg[i] = "(nil)"
  56. elseif (type(arg[i]) == "table") then
  57. arg[i] = "{" .. table.concat(arg[i], ", ") .. "}"
  58. end
  59. end
  60. error(string.format(format, unpack(arg)), 3)
  61. end
  62. function test.filecontains(expected, fn)
  63. local f = io.open(fn)
  64. local actual = f:read("*a")
  65. f:close()
  66. if (expected ~= actual) then
  67. test.fail("expected %s but was %s", expected, actual)
  68. end
  69. end
  70. function test.isemptycapture()
  71. local actual = io.endcapture()
  72. if actual ~= "" then
  73. test.fail("expected empty capture, but was %s", actual);
  74. end
  75. end
  76. function test.isequal(expected, actual)
  77. if (type(expected) == "table") then
  78. for k,v in pairs(expected) do
  79. if not (test.isequal(expected[k], actual[k])) then
  80. test.fail("expected %s but was %s", expected, actual)
  81. end
  82. end
  83. else
  84. if (expected ~= actual) then
  85. test.fail("expected %s but was %s", expected, actual)
  86. end
  87. end
  88. return true
  89. end
  90. function test.isfalse(value)
  91. if (value) then
  92. test.fail("expected false but was true")
  93. end
  94. end
  95. function test.isnil(value)
  96. if (value ~= nil) then
  97. test.fail("expected nil but was " .. tostring(value))
  98. end
  99. end
  100. function test.isnotnil(value)
  101. if (value == nil) then
  102. test.fail("expected not nil")
  103. end
  104. end
  105. function test.istrue(value)
  106. if (not value) then
  107. test.fail("expected true but was false")
  108. end
  109. end
  110. function test.openedfile(fname)
  111. if fname ~= test.value_openedfilename then
  112. local msg = "expected to open file '" .. fname .. "'"
  113. if test.value_openedfilename then
  114. msg = msg .. ", got '" .. test.value_openedfilename .. "'"
  115. end
  116. test.fail(msg)
  117. end
  118. end
  119. function test.success(fn, ...)
  120. local ok, err = pcall(fn, unpack(arg))
  121. if not ok then
  122. test.fail("call failed: " .. err)
  123. end
  124. end
  125. --
  126. -- Test stubs
  127. --
  128. local function stub_io_open(fname, mode)
  129. test.value_openedfilename = fname
  130. test.value_openedfilemode = mode
  131. return {
  132. close = function()
  133. test.value_closedfile = true
  134. end
  135. }
  136. end
  137. local function stub_io_output(f)
  138. end
  139. local function stub_print(s)
  140. end
  141. --
  142. -- Define a collection for the test suites
  143. --
  144. T = { }
  145. --
  146. -- Test execution function
  147. --
  148. local _OS_host = _OS
  149. local function test_setup(suite, fn)
  150. -- clear out some important globals
  151. _ACTION = "test"
  152. _ARGS = { }
  153. _OPTIONS = { }
  154. _OS = _OS_host
  155. premake.solution.list = { }
  156. io.indent = nil
  157. io.eol = "\n"
  158. -- reset captured I/O values
  159. test.value_openedfilename = nil
  160. test.value_openedfilemode = nil
  161. test.value_closedfile = false
  162. if suite.setup then
  163. return pcall(suite.setup)
  164. else
  165. return true
  166. end
  167. end
  168. local function test_run(suite, fn)
  169. io.capture()
  170. return pcall(fn)
  171. end
  172. local function test_teardown(suite, fn)
  173. if suite.teardown then
  174. return pcall(suite.teardown)
  175. else
  176. return true
  177. end
  178. end
  179. function test.runall(suitename, testname)
  180. test.print = print
  181. print = stub_print
  182. io.open = stub_io_open
  183. io.output = stub_io_output
  184. local numpassed = 0
  185. local numfailed = 0
  186. local start_time = os.clock()
  187. function runtest(suitename, suitetests, testname, testfunc)
  188. if suitetests.setup ~= testfunc and suitetests.teardown ~= testfunc then
  189. local ok, err = test_setup(suitetests, testfunc)
  190. if ok then
  191. ok, err = test_run(suitetests, testfunc)
  192. end
  193. local tok, terr = test_teardown(suitetests, testfunc)
  194. ok = ok and tok
  195. err = err or terr
  196. if (not ok) then
  197. test.print(string.format("%s.%s: %s", suitename, testname, err))
  198. numfailed = numfailed + 1
  199. else
  200. numpassed = numpassed + 1
  201. end
  202. end
  203. end
  204. function runsuite(suitename, suitetests, testname)
  205. if testname then
  206. runtest(suitename, suitetests, testname, suitetests[testname])
  207. else
  208. for testname, testfunc in pairs(suitetests) do
  209. runtest(suitename, suitetests, testname, testfunc)
  210. end
  211. end
  212. end
  213. if suitename then
  214. runsuite(suitename, T[suitename], testname)
  215. else
  216. for suitename, suitetests in pairs(T) do
  217. runsuite(suitename, suitetests, testname)
  218. end
  219. end
  220. io.write('running time : ', os.clock() - start_time,'\n')
  221. print = test.print
  222. return numpassed, numfailed
  223. end