This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Helfull's profile picture

Unittesting framework MineUnit

Started by Helfull, 28 October 2016 - 08:30 PM
Helfull #1
Posted 28 October 2016 - 10:30 PM
Hey guys,

i have started to work on my UnitTesting framework for CC, since i couldn't find a framework here on the Forum.

Demo output




How to use:
Spoiler

os.loadAPI("MineUnit")

MineUnit.run({
	slowRuns = true,
	presenter = MineUnit.newPresenter({
		terminal = peripheral.wrap("left")
	})
},
function(unit)

	unit:is(1, 1) -- Checks if 1 == 1
	unit:is(1, 2) -- Checks if 1 == 2

	unit:isEqual(1, 1) -- Alias for unit:is
	unit:isEqual(1, 2) -- Alias for unit:is

	unit:isTrue(true) -- checks if true == true
	unit:isTrue(false) -- checks if false == true

	unit:isFalse(false) -- checks if false == false
	unit:isFalse(true) -- checks if true == false

	unit:isSmaller(0, 1) -- checks if 0 < 1
	unit:isSmaller(1, 0) -- checks if 1 < 0

	unit:isGreater(1, 0) -- checks if 1 > 0
	unit:isGreater(0, 1) -- checks if 0 > 1

	unit:isSmallerOrEqual(1, 1) -- checks if 1 <= 1
	unit:isSmallerOrEqual(5, 1) -- checks if 5 <= 1

	unit:isGreaterOrEqual(2, 2) -- checks if 2 >= 2
	unit:isGreaterOrEqual(1, 5) -- checks if 1 >= 5

	local obj = {}
	unit:isType(obj, "table") -- Checks if type(obj) == "table"
	unit:isType(obj, "function") -- Checks if type(obj) == "function"

	local function test(v1, v2, v3)
		return (v1 == v2) and (v1 == v3)
	end
	unit:shouldReturn(test, true) -- Checks the return value of test against true
	unit:shouldReturn(test, false) -- Checks the return value of test against false

end)

Current Supported Assertions
Spoiler

	unit:is
	unit:isEqual
	unit:isTrue
	unit:isFalse
	unit:isSmaller
	unit:isGreater
	unit:isSmallerOrEqual
	unit:isGreaterOrEqual
	unit:isType
	unit:shouldReturn

MineUnit Options:
Spoiler

{
    presenter = Presenter:new(), -- the presenter to use
    slowRuns = false, -- should it run the tests slow
}

MineUnit Presenter Options:
Spoiler

{
	terminal = term, -- your terminal/monitor you want to use for the output

	fullMessages = true, -- should it show the full assertion text or only dots for success and "X" for failures

	failedTextColor = FailureTextColor, -- what text color should failures have
	failedBackgroundColor = FailureBackgroundColor, -- what background color should failures have

	successTextColor = SuccessTextColor, -- what text color should successfull assertions have
	successBackgroundColor = SuccessBackgroundColor, -- what background color should successfull assertions have

	errorTextColor = ErrorTextColor, -- what text color should errors have
	errorBackgroundColor = ErrorBackgroundColor -- what background color should errors have
}

ToDo:
  • Add more assertion methods
Any ideas for this are welcome

Download:
P0B9gDYh

Install:

pastebin get P0B9gDYh MineUnit

Demo code install:

pastebin get W8dAsfRY MineUnit
Edited on 28 October 2016 - 11:22 PM
Bomb Bloke #2
Posted 29 October 2016 - 12:33 AM
Psst - you forgot to post your code.
Helfull #3
Posted 29 October 2016 - 01:10 AM
The moment i posted it, i had still to mich todo for a release ^^

but the passt 2 hours it got a all it needs

P0B9gDYh

also there are already changes to the demo code


os.loadAPI("MineUnit")

MineUnit.run({
    slowRuns = true, -- if you have many tests and want to see them all
    presenter = MineUnit.newPresenter({
        terminal = peripheral.wrap("left") -- choose your output term
    })
},
function(unit)

    unit:is(1, 1) -- Checks if 1 == 1
    unit:is(1, 2) -- Checks if 1 == 2

    unit:isEqual(1, 1) -- Alias for unit:is
    unit:isEqual(1, 2) -- Alias for unit:is

    unit:isTrue(true) -- checks if true == true
    unit:isTrue(false) -- checks if false == true

    unit:isFalse(false) -- checks if false == false
    unit:isFalse(true) -- checks if true == false

    unit:isSmaller(0, 1) -- checks if 0 < 1
    unit:isSmaller(1, 0) -- checks if 1 < 0

    unit:isGreater(1, 0) -- checks if 1 > 0
    unit:isGreater(0, 1) -- checks if 0 > 1

    unit:isSmallerOrEqual(1, 1) -- checks if 1 <= 1
    unit:isSmallerOrEqual(5, 1) -- checks if 5 <= 1

    unit:isGreaterOrEqual(2, 2) -- checks if 2 >= 2
    unit:isGreaterOrEqual(1, 5) -- checks if 1 >= 5

    local obj = {}
    unit:isType(obj, "table") -- Checks if type(obj) == "table"
    unit:isType(obj, "function") -- Checks if type(obj) == "function"

    local function test(v1, v2, v3)
        return (v1 == v2) and (v1 == v3)
    end
    unit:shouldReturn(test, true) -- Checks the return value of test against true
    unit:shouldReturn(test, false) -- Checks the return value of test against false

end)

support for monitor output