1
2--[[--------------------------------------------------------------------------
3
4    This file is part of lunit 0.5.
5
6    For Details about lunit look at: http://www.mroth.net/lunit/
7
8    Author: Michael Roth <mroth@nessie.de>
9
10    Copyright (c) 2006-2008 Michael Roth <mroth@nessie.de>
11
12    Permission is hereby granted, free of charge, to any person
13    obtaining a copy of this software and associated documentation
14    files (the "Software"), to deal in the Software without restriction,
15    including without limitation the rights to use, copy, modify, merge,
16    publish, distribute, sublicense, and/or sell copies of the Software,
17    and to permit persons to whom the Software is furnished to do so,
18    subject to the following conditions:
19
20    The above copyright notice and this permission notice shall be
21    included in all copies or substantial portions of the Software.
22
23    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
31--]]--------------------------------------------------------------------------
32
33
34
35--[[
36
37      begin()
38        run(testcasename, testname)
39          err(fullname, message, traceback)
40          fail(fullname, where, message, usermessage)
41          pass(testcasename, testname)
42      done()
43
44      Fullname:
45        testcase.testname
46        testcase.testname:setupname
47        testcase.testname:teardownname
48
49--]]
50
51
52lunit = require "lunit"
53
54local lunit_console
55
56if _VERSION >= 'Lua 5.2' then
57
58    lunit_console = setmetatable({},{__index = _ENV})
59    _ENV = lunit_console
60
61else
62
63    module( "lunit-console", package.seeall )
64    lunit_console = _M
65
66end
67
68
69
70local function printformat(format, ...)
71  io.write( string.format(format, ...) )
72end
73
74
75local columns_printed = 0
76
77local function writestatus(char)
78  if columns_printed == 0 then
79    io.write("    ")
80  end
81  if columns_printed == 60 then
82    io.write("\n    ")
83    columns_printed = 0
84  end
85  io.write(char)
86  io.flush()
87  columns_printed = columns_printed + 1
88end
89
90
91local msgs = {}
92
93
94function begin()
95  local total_tc = 0
96  local total_tests = 0
97
98  msgs = {} -- e
99
100  for tcname in lunit.testcases() do
101    total_tc = total_tc + 1
102    for testname, test in lunit.tests(tcname) do
103      total_tests = total_tests + 1
104    end
105  end
106
107  printformat("Loaded testsuite with %d tests in %d testcases.\n\n", total_tests, total_tc)
108end
109
110
111function run(testcasename, testname)
112  -- NOP
113end
114
115
116function err(fullname, message, traceback)
117  writestatus("E")
118  msgs[#msgs+1] = "Error! ("..fullname.."):\n"..message.."\n\t"..table.concat(traceback, "\n\t") .. "\n"
119end
120
121
122function fail(fullname, where, message, usermessage)
123  writestatus("F")
124  local text =  "Failure ("..fullname.."):\n"..
125                where..": "..message.."\n"
126
127  if usermessage then
128    text = text .. where..": "..usermessage.."\n"
129  end
130
131  msgs[#msgs+1] = text
132end
133
134
135function pass(testcasename, testname)
136  writestatus(".")
137end
138
139
140
141function done()
142  printformat("\n\n%d Assertions checked.\n", lunit.stats.assertions )
143  print()
144
145  for i, msg in ipairs(msgs) do
146    printformat( "%3d) %s\n", i, msg )
147  end
148
149  printformat("Testsuite finished (%d passed, %d failed, %d errors).\n",
150      lunit.stats.passed, lunit.stats.failed, lunit.stats.errors )
151end
152
153
154return lunit_console
155
156
157