test_assertions.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ---
  2. -- test_assertions.lua
  3. --
  4. -- Assertion functions for unit tests.
  5. --
  6. -- Author Jason Perkins
  7. -- Copyright (c) 2008-2016 Jason Perkins and the Premake project.
  8. ---
  9. local p = premake
  10. local m = p.modules.self_test
  11. local _ = {}
  12. function m.capture(expected)
  13. local actual = p.captured() .. p.eol()
  14. -- create line-by-line iterators for both values
  15. local ait = actual:gmatch("(.-)" .. p.eol())
  16. local eit = expected:gmatch("(.-)\n")
  17. -- compare each value line by line
  18. local linenum = 1
  19. local atxt = ait()
  20. local etxt = eit()
  21. while etxt do
  22. if (etxt ~= atxt) then
  23. m.fail("(%d) expected:\n%s\n...but was:\n%s\nfulltext:\n%s", linenum, etxt, atxt, actual)
  24. end
  25. linenum = linenum + 1
  26. atxt = ait()
  27. etxt = eit()
  28. end
  29. end
  30. function m.closedfile(expected)
  31. if expected and not m.value_closedfile then
  32. m.fail("expected file to be closed")
  33. elseif not expected and m.value_closedfile then
  34. m.fail("expected file to remain open")
  35. end
  36. end
  37. function m.contains(expected, actual)
  38. if type(expected) == "table" then
  39. for i, v in ipairs(expected) do
  40. m.contains(v, actual)
  41. end
  42. elseif not table.contains(actual, expected) then
  43. m.fail("expected value %s not found", expected)
  44. end
  45. end
  46. function m.excludes(expected, actual)
  47. if type(expected) == "table" then
  48. for i, v in ipairs(expected) do
  49. m.excludes(v, actual)
  50. end
  51. elseif table.contains(actual, expected) then
  52. m.fail("excluded value %s found", expected)
  53. end
  54. end
  55. function m.fail(format, ...)
  56. -- if format is a number then it is the stack depth
  57. local depth = 3
  58. local arg = {...}
  59. if type(format) == "number" then
  60. depth = depth + format
  61. format = table.remove(arg, 1)
  62. end
  63. -- convert nils into something more usefuls
  64. for i = 1, #arg do
  65. if (arg[i] == nil) then
  66. arg[i] = "(nil)"
  67. elseif (type(arg[i]) == "table") then
  68. arg[i] = "{" .. table.concat(arg[i], ", ") .. "}"
  69. end
  70. end
  71. local msg = string.format(format, table.unpack(arg))
  72. error(debug.traceback(msg, depth), depth)
  73. end
  74. function m.filecontains(expected, fn)
  75. local f = io.open(fn)
  76. local actual = f:read("*a")
  77. f:close()
  78. if (expected ~= actual) then
  79. m.fail("expected %s but was %s", expected, actual)
  80. end
  81. end
  82. function m.hasoutput()
  83. local actual = p.captured()
  84. if actual == "" then
  85. m.fail("expected output, received none");
  86. end
  87. end
  88. function m.isemptycapture()
  89. local actual = p.captured()
  90. if actual ~= "" then
  91. m.fail("expected empty capture, but was %s", actual);
  92. end
  93. end
  94. function m.isequal(expected, actual, depth)
  95. depth = depth or 0
  96. if type(expected) == "table" then
  97. if expected and not actual then
  98. m.fail(depth, "expected table, got nil")
  99. end
  100. if #expected < #actual then
  101. m.fail(depth, "expected %d items, got %d", #expected, #actual)
  102. end
  103. for k,v in pairs(expected) do
  104. m.isequal(expected[k], actual[k], depth + 1)
  105. end
  106. else
  107. if (expected ~= actual) then
  108. m.fail(depth, "expected %s but was %s", expected, actual or "nil")
  109. end
  110. end
  111. return true
  112. end
  113. function m.isfalse(value)
  114. if (value) then
  115. m.fail("expected false but was true")
  116. end
  117. end
  118. function m.isnil(value)
  119. if (value ~= nil) then
  120. m.fail("expected nil but was " .. tostring(value))
  121. end
  122. end
  123. function m.isnotnil(value)
  124. if (value == nil) then
  125. m.fail("expected not nil")
  126. end
  127. end
  128. function m.issame(expected, action)
  129. if expected ~= action then
  130. m.fail("expected same value")
  131. end
  132. end
  133. function m.istrue(value)
  134. if (not value) then
  135. m.fail("expected true but was false")
  136. end
  137. end
  138. function m.missing(value, actual)
  139. if table.contains(actual, value) then
  140. m.fail("unexpected value %s found", value)
  141. end
  142. end
  143. function m.openedfile(fname)
  144. if fname ~= m.value_openedfilename then
  145. local msg = "expected to open file '" .. fname .. "'"
  146. if m.value_openedfilename then
  147. msg = msg .. ", got '" .. m.value_openedfilename .. "'"
  148. end
  149. m.fail(msg)
  150. end
  151. end
  152. function m.success(fn, ...)
  153. local ok, err = pcall(fn, ...)
  154. if not ok then
  155. m.fail("call failed: " .. err)
  156. end
  157. end
  158. function m.stderr(expected)
  159. if not expected and m.stderr_capture then
  160. m.fail("Unexpected: " .. m.stderr_capture)
  161. elseif expected then
  162. if not m.stderr_capture or not m.stderr_capture:find(expected) then
  163. m.fail(string.format("expected '%s'; got %s", expected, m.stderr_capture or "(nil)"))
  164. end
  165. end
  166. end
  167. function m.notstderr(expected)
  168. if not expected and not m.stderr_capture then
  169. m.fail("Expected output on stderr; none received")
  170. elseif expected then
  171. if m.stderr_capture and m.stderr_capture:find(expected) then
  172. m.fail(string.format("stderr contains '%s'; was %s", expected, m.stderr_capture))
  173. end
  174. end
  175. end