PageRenderTime 217ms CodeModel.GetById 167ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lunit-console.lua

http://github.com/u1tnk/kah.lua
Lua | 141 lines | 57 code | 38 blank | 46 comment | 8 complexity | 9185b5a2c728cdaa98f7b85bc89bcdd2 MD5 | raw file
  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
 52require "lunit"
 53
 54module( "lunit-console", package.seeall )
 55
 56
 57local function printformat(format, ...)
 58  io.write( string.format(format, ...) )
 59end
 60
 61
 62local columns_printed = 0
 63
 64local function writestatus(char)
 65  if columns_printed == 0 then
 66    io.write("    ")
 67  end
 68  if columns_printed == 60 then
 69    io.write("\n    ")
 70    columns_printed = 0
 71  end
 72  io.write(char)
 73  io.flush()
 74  columns_printed = columns_printed + 1
 75end
 76
 77
 78local msgs = {}
 79
 80
 81function begin()
 82  local total_tc = 0
 83  local total_tests = 0
 84
 85  for tcname in lunit.testcases() do
 86    total_tc = total_tc + 1
 87    for testname, test in lunit.tests(tcname) do
 88      total_tests = total_tests + 1
 89    end
 90  end
 91
 92  printformat("Loaded testsuite with %d tests in %d testcases.\n\n", total_tests, total_tc)
 93end
 94
 95
 96function run(testcasename, testname)
 97  -- NOP
 98end
 99
100
101function err(fullname, message, traceback)
102  writestatus("E")
103  msgs[#msgs+1] = "Error! ("..fullname.."):\n"..message.."\n\t"..table.concat(traceback, "\n\t") .. "\n"
104end
105
106
107function fail(fullname, where, message, usermessage)
108  writestatus("F")
109  local text =  "Failure ("..fullname.."):\n"..
110                where..": "..message.."\n"
111
112  if usermessage then
113    text = text .. where..": "..usermessage.."\n"
114  end
115
116  msgs[#msgs+1] = text
117end
118
119
120function pass(testcasename, testname)
121  writestatus(".")
122end
123
124
125
126function done()
127  printformat("\n\n%d Assertions checked.\n", lunit.stats.assertions )
128  print()
129
130  for i, msg in ipairs(msgs) do
131    printformat( "%3d) %s\n", i, msg )
132  end
133
134  printformat("Testsuite finished (%d passed, %d failed, %d errors).\n",
135      lunit.stats.passed, lunit.stats.failed, lunit.stats.errors )
136end
137
138
139
140
141