Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [[ Config ]] --
- local DefaultTextColor = colors.white
- local DefaultBackgroundColor = colors.black
- local SuccessTextColor = colors.green
- local SuccessBackgroundColor = colors.black
- local FailureTextColor = colors.red
- local FailureBackgroundColor = colors.black
- local ErrorTextColor = colors.red
- local ErrorBackgroundColor = colors.pink
- local debug = false
- local monitor = 0
- if debug then
- monitor = peripheral.wrap("left")
- monitor.clear()
- monitor.setCursorPos(1, 1)
- end
- -- [[ Simple Presenter ]] --
- local Presenter = {}
- Presenter.__index = Presenter
- function Presenter.clear(self)
- self.terminal.clear()
- self.terminal.setCursorBlink(false)
- self.terminal.setCursorPos(1,1)
- end
- function Presenter.new(self, options)
- local self = setmetatable({}, Presenter)
- options = options or {
- terminal = term,
- fullMessages = true,
- failedTextColor = FailureTextColor,
- failedBackgroundColor = FailureBackgroundColor,
- successTextColor = SuccessTextColor,
- successBackgroundColor = SuccessBackgroundColor,
- errorTextColor = ErrorTextColor,
- errorBackgroundColor = ErrorBackgroundColor
- }
- self.terminal = options.terminal or term
- if options.fullMessages == nil then
- self.fullMessages = true
- else
- self.fullMessages = options.fullMessages
- end
- self.failedTextColor = options.failedTextColor or FailureTextColor
- self.failedBackgroundColor = options.failedBackgroundColor or FailureBackgroundColor
- self.successTextColor = options.successTextColor or SuccessTextColor
- self.successBackgroundColor = options.successBackgroundColor or SuccessBackgroundColor
- self.errorTextColor = options.errorTextColor or ErrorTextColor
- self.errorBackgroundColor = options.errorBackgroundColor or ErrorBackgroundColor
- self:clear()
- return self
- end
- function Presenter.resetColor(self)
- self.terminal.setTextColor(colors.white)
- self.terminal.setBackgroundColor(colors.black)
- end
- function Presenter.setColorError(self)
- self.terminal.setTextColor(self.errorTextColor)
- self.terminal.setBackgroundColor(self.errorBackgroundColor)
- end
- function Presenter.setColorSuccess(self)
- self.terminal.setTextColor(self.successTextColor)
- self.terminal.setBackgroundColor(self.successBackgroundColor)
- end
- function Presenter.setColorFailed(self)
- self.terminal.setTextColor(self.failedTextColor)
- self.terminal.setBackgroundColor(self.failedBackgroundColor)
- end
- function Presenter.writeFullMessage(self, msg)
- self.terminal.write(msg)
- local x, y = self.terminal.getCursorPos()
- y = y + 1;
- x = 1
- self.terminal.setCursorPos(x, y)
- local winX, winY = self.terminal.getSize()
- if y > winY then
- self.terminal.scroll(1)
- y = y - 1
- self.terminal.setCursorPos(x, y)
- end
- end
- function Presenter.writeSimple(self, result)
- if result then
- self.terminal.write(".")
- else
- self.terminal.write("X")
- end
- local x, y = self.terminal.getCursorPos()
- local winX, winY = self.terminal.getSize()
- if y > winY then
- self.terminal.scroll(1)
- y = y - 1
- self.terminal.setCursorPos(x, y)
- end
- end
- function Presenter.write(self, result, msg)
- msg = msg..tostring(result)
- if result then
- self:setColorSuccess()
- else
- self:setColorFailed()
- end
- if self.fullMessages then
- self:writeFullMessage(msg)
- else
- self:writeSimple(result)
- end
- end
- function Presenter.displayError(self, error)
- self:setColorError()
- self.terminal.write(error)
- self:resetColor()
- end
- function Presenter.displayResult(self, result, msg)
- self:write(result, msg)
- end
- function Presenter.displayResults(self, total, passed, failed)
- local curX, curY = self.terminal.getCursorPos()
- local x, y = self.terminal.getSize()
- if curY > y then
- -- Scroll diff + 1
- local diff = curY - y
- self.terminal.scroll(diff)
- end
- self.terminal.setCursorPos(1, y)
- self:resetColor()
- self.terminal.write("Total tests: "..total)
- x, y = self.terminal.getCursorPos()
- self.terminal.setCursorPos(x + 3, y)
- self:setColorSuccess()
- self.terminal.write("Passed: "..passed)
- x, y = self.terminal.getCursorPos()
- self.terminal.setCursorPos(x + 3, y)
- self:setColorFailed()
- self.terminal.write("Failed: "..failed)
- self.terminal.setCursorPos(1, y+1)
- end
- function newPresenter(options)
- return Presenter:new(options)
- end
- -- [[ MineUnit ]] --
- local mineUnit = {
- assertions = {},
- _totalFailed = 0,
- _totalPassed = 0,
- _totalTests = 0
- }
- mineUnit.__index = mineUnit
- function mineUnit.new(options)
- local self = setmetatable({}, mineUnit)
- options = options or {
- presenter = Presenter:new(),
- slowRuns = false,
- }
- self.presenter = options.presenter or Presenter:new()
- self.slowRuns = options.slowRuns or false
- return self
- end
- function mineUnit.reset(self)
- self._totalFailed = 0
- self._totalPassed = 0
- self._totalTests = 0
- end
- function mineUnit.pushAssertion(self, assertion, v1, v2, msg)
- local asrt = {
- test = assertion,
- v1 = v1,
- v2 = v2,
- msg = msg or "Failed assertion for "..tostring(v1).." and "..tostring(v2)
- }
- table.insert(self.assertions, asrt)
- end
- function mineUnit.run(self, presenter)
- presenter = presenter or self.presenter
- self:reset()
- presenter:writeFullMessage("--- [[ Starting MineUnit test ]] ---")
- for i,assertion in pairs(self.assertions) do
- self._totalTests = self._totalTests + 1
- local success, resultOrError = pcall(assertion.test, assertion.v1, assertion.v2)
- if not success then
- presenter:displayError(resultOrError)
- self._totalFailed = self._totalFailed + 1
- else
- if resultOrError then
- self._totalPassed = self._totalPassed + 1
- else
- self._totalFailed = self._totalFailed + 1
- end
- presenter:displayResult(resultOrError, assertion.msg)
- end
- if self.slowRuns then
- sleep(0.5)
- end
- end
- presenter:displayResults(self._totalTests, self._totalPassed, self._totalFailed)
- end
- function new(options)
- return mineUnit.new(options)
- end
- -- [[ Assertions ]] --
- function mineUnit.is(self, v1, v2, msg)
- local function test(v1, v2)
- return v1 == v2
- end
- self:pushAssertion(test, v1, v2, msg or "Assert that "..tostring(v1).." is "..tostring(v2).." is ");
- end
- function mineUnit.isTrue(self, v, printMessage)
- self:is(v, true, printMessage or "Assert that "..tostring(v).." is true is ")
- end
- function mineUnit.isFalse(self, v, printMessage)
- self:is(v, false, printMessage or "Assert that "..tostring(v).." is false is ")
- end
- function mineUnit.isSmaller(self, v1, v2, printMessage)
- local function test(v1, v2)
- return v1 < v2
- end
- self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is smaller then "..tostring(v2).." is ")
- end
- function mineUnit.isGreater(self, v1, v2, printMessage)
- local function test(v1, v2)
- return v1 > v2
- end
- self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is greater then "..tostring(v2).." is ")
- end
- function mineUnit.isSmallerOrEqual(self, v1, v2, printMessage)
- local function test(v1, v2)
- return v1 <= v2
- end
- self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is smaller or equal to "..tostring(v2).." is ")
- end
- function mineUnit.isGreaterOrEqual(self, v1, v2)
- local function test(v1, v2)
- return v1 >= v2
- end
- self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is greater or equal to "..tostring(v2).." is ")
- end
- function mineUnit.isEqual(self, v1, v2)
- self:is(v1, v2, "Assert that "..tostring(v1).." is equal to "..tostring(v2).." is ")
- end
- function mineUnit.isType(self, v, expectionType)
- self:is(type(v), expectionType, "Assert that obj("..tostring(v)..") is of type "..tostring(expectionType).." is ")
- end
- function mineUnit.shouldReturn(self, func, v, ...)
- local rtn = v
- if #arg == 0 then
- rtn = func()
- elseif #arg == 1 then
- rtn = func(arg[1])
- elseif #arg == 2 then
- rtn = func(arg[1],arg[2])
- elseif #arg == 3 then
- rtn = func(arg[1],arg[2],arg[3])
- elseif #arg == 4 then
- rtn = func(arg[1],arg[2],arg[3],arg[4])
- else
- rtn = func(arg)
- end
- self:is(rtn, v, "Assert that expected return value "..tostring(v).." matches "..tostring(rtn).." is ")
- end
- -- [[ Wrapper ]] --
- function run(options, init)
- local unit = mineUnit.new(options)
- init(unit)
- unit:run()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement