├── example.lua ├── minctest-0.1.1-1.rockspec ├── LICENSE ├── README.md └── minctest.lua /example.lua: -------------------------------------------------------------------------------- 1 | 2 | require "minctest" 3 | 4 | lrun("test1", function() 5 | lok('a' == 'a'); --assert true 6 | end) 7 | 8 | lrun("test2", function() 9 | lequal(5, 6); --compare integers 10 | lfequal(5.5, 5.6); --compare floats 11 | end) 12 | 13 | return lresults(); --show results 14 | -------------------------------------------------------------------------------- /minctest-0.1.1-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "minctest" 2 | version = "0.1.1-1" 3 | source = { 4 | url = "git+https://github.com/codeplea/minctest-lua.git", 5 | tag = "v0.1.1" 6 | } 7 | description = { 8 | summary = "very minimal unit-testing \"framework\" for Lua", 9 | detailed = [[ 10 | This is a Lua port of [Minctest](https://codeplea.com/minctest), a very 11 | minimal unit-testing "framework" originally written in ANSI C. It's handy when 12 | you want some real simple unit tests for a small project.]], 13 | homepage = "https://github.com/codeplea/minctest-lua", 14 | license = "zlib" 15 | } 16 | dependencies = { 17 | "lua >= 5.1, < 5.4" 18 | } 19 | build = { 20 | type = "builtin", 21 | modules = { 22 | minctest = "minctest.lua" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | zlib License 2 | 3 | Copyright (C) 2014, 2015, 2016 Lewis Van Winkle 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgement in the product documentation would be 16 | appreciated but is not required. 17 | 2. Altered source versions must be plainly marked as such, and must not be 18 | misrepresented as being the original software. 19 | 3. This notice may not be removed or altered from any source distribution. 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minctest 2 | 3 | This is a Lua port of [Minctest](https://codeplea.com/minctest), a very 4 | minimal unit-testing "framework" originally written in ANSI C. It's handy when 5 | you want some real simple unit tests for a small project. 6 | 7 | Basically, it implements assertion and equal functions. It'll track and time 8 | how many tests pass and fail. Failed tests will also display which line the 9 | failing test code was on. 10 | 11 | ## Features 12 | 13 | - Contained in a single file. 14 | - Reports file and line number for failed assertions. 15 | - Reports run time for each test. 16 | - Tests continue even after an assertion fails. 17 | - Has assertion for checking float equality. 18 | - Released under the zlib license - free for nearly any use. 19 | 20 | ## Installation 21 | 22 | You can either copy `minctest.lua` into your project, or if you use luarocks 23 | run `luarocks install minctest`. 24 | 25 | ## Example 26 | 27 | ```lua 28 | require "minctest" 29 | 30 | lrun("test1", function() 31 | lok('a' == 'a'); --assert true 32 | end) 33 | 34 | lrun("test2", function() 35 | lequal(5, 5); --compare integers 36 | lfequal(5.5, 5.5); --compare floats 37 | end) 38 | 39 | return lresults(); --show results 40 | ``` 41 | 42 | That produces the following output: 43 | 44 | test1 pass: 1 fail: 0 0ms 45 | test2 pass: 2 fail: 0 1ms 46 | ALL TESTS PASSED (3/3) 47 | 48 | 49 | 50 | ## Hints 51 | All functions start with the letter 'l'. 52 | 53 | 54 | ## Users 55 | 56 | If you're using Minctest in your project, let me know. I could add a link back. 57 | -------------------------------------------------------------------------------- /minctest.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- MINCTEST - Minimal Lua Test Library - 0.1.1 3 | -- This is based on minctest.h (https://codeplea.com/minctest) 4 | -- 5 | -- Copyright (c) 2014, 2015, 2016 Lewis Van Winkle 6 | -- 7 | -- http://CodePlea.com 8 | -- 9 | -- This software is provided 'as-is', without any express or implied 10 | -- warranty. In no event will the authors be held liable for any damages 11 | -- arising from the use of this software. 12 | -- 13 | -- Permission is granted to anyone to use this software for any purpose, 14 | -- including commercial applications, and to alter it and redistribute it 15 | -- freely, subject to the following restrictions: 16 | -- 17 | -- 1. The origin of this software must not be misrepresented; you must not 18 | -- claim that you wrote the original software. If you use this software 19 | -- in a product, an acknowledgement in the product documentation would be 20 | -- appreciated but is not required. 21 | -- 2. Altered source versions must be plainly marked as such, and must not be 22 | -- misrepresented as being the original software. 23 | -- 3. This notice may not be removed or altered from any source distribution. 24 | 25 | 26 | -- MINCTEST - Minimal testing library for C 27 | -- 28 | -- 29 | -- Example: 30 | -- 31 | -- 32 | -- require "minctest" 33 | -- 34 | -- lrun("test1", function() 35 | -- lok('a' == 'a'); --assert true 36 | -- end) 37 | -- 38 | -- lrun("test2", function() 39 | -- lequal(5, 6); --compare integers 40 | -- lfequal(5.5, 5.6); --compare floats 41 | -- end) 42 | -- 43 | -- return lresults(); --show results 44 | -- 45 | -- 46 | -- Hints: 47 | -- All functions/variables start with the letter 'l'. 48 | -- 49 | -- 50 | 51 | 52 | local LTEST_FLOAT_TOLERANCE = 0.001 53 | 54 | 55 | local ltests = 0 56 | local lfails = 0 57 | 58 | 59 | lresults = function() 60 | if (lfails == 0) then 61 | print("ALL TESTS PASSED (" .. ltests .. "/" .. ltests .. ")") 62 | else 63 | print("SOME TESTS FAILED (" .. ltests-lfails .. "/" .. ltests .. ")") 64 | end 65 | return lfails ~= 0 66 | end 67 | 68 | 69 | lrun = function(name, testfunc) 70 | local ts = ltests 71 | local fs = lfails 72 | local clock = os.clock() 73 | io.write(string.format("\t%-16s", name)) 74 | testfunc() 75 | io.write(string.format("pass:%2d fail:%2d %4dms\n", 76 | (ltests-ts)-(lfails-fs), lfails-fs, 77 | math.floor((os.clock() - clock) * 1000))); 78 | end 79 | 80 | lok = function(test) 81 | ltests = ltests + 1 82 | if not test then 83 | lfails = lfails + 1 84 | io.write(string.format("%s:%d error \n", 85 | debug.getinfo(2, 'S').short_src, 86 | debug.getinfo(2, 'l').currentline)) 87 | end 88 | end 89 | 90 | lequal = function(a, b) 91 | ltests = ltests + 1 92 | if a ~= b then 93 | lfails = lfails + 1 94 | io.write(string.format("%s:%d (%d != %d)\n", 95 | debug.getinfo(2, 'S').short_src, 96 | debug.getinfo(2, 'l').currentline, 97 | a, b)) 98 | end 99 | end 100 | 101 | lfequal = function(a, b) 102 | ltests = ltests + 1 103 | if math.abs(a - b) > LTEST_FLOAT_TOLERANCE then 104 | lfails = lfails + 1 105 | io.write(string.format("%s:%d (%f != %f)\n", 106 | debug.getinfo(2, 'S').short_src, 107 | debug.getinfo(2, 'l').currentline, 108 | a, b)) 109 | end 110 | end 111 | 112 | --------------------------------------------------------------------------------