Advertisement
Guest User

MineUnit

a guest
Oct 28th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.92 KB | None | 0 0
  1. -- [[ Config ]] --
  2. local DefaultTextColor = colors.white
  3. local DefaultBackgroundColor = colors.black
  4. local SuccessTextColor = colors.green
  5. local SuccessBackgroundColor = colors.black
  6. local FailureTextColor = colors.red
  7. local FailureBackgroundColor = colors.black
  8. local ErrorTextColor = colors.red
  9. local ErrorBackgroundColor = colors.pink
  10.  
  11. local debug = false
  12. local monitor = 0
  13. if debug then
  14.     monitor = peripheral.wrap("left")
  15.     monitor.clear()
  16.     monitor.setCursorPos(1, 1)
  17. end
  18.  
  19. -- [[ Simple Presenter ]] --
  20. local Presenter = {}
  21. Presenter.__index = Presenter
  22.  
  23. function Presenter.clear(self)
  24.     self.terminal.clear()
  25.     self.terminal.setCursorBlink(false)
  26.     self.terminal.setCursorPos(1,1)
  27. end
  28.  
  29. function Presenter.new(self, options)
  30.     local self = setmetatable({}, Presenter)
  31.  
  32.     options = options or {
  33.         terminal = term,
  34.  
  35.         fullMessages = true,
  36.  
  37.         failedTextColor = FailureTextColor,
  38.         failedBackgroundColor = FailureBackgroundColor,
  39.  
  40.         successTextColor = SuccessTextColor,
  41.         successBackgroundColor = SuccessBackgroundColor,
  42.  
  43.         errorTextColor = ErrorTextColor,
  44.         errorBackgroundColor = ErrorBackgroundColor
  45.     }
  46.  
  47.     self.terminal = options.terminal or term
  48.  
  49.     if options.fullMessages == nil then
  50.         self.fullMessages = true
  51.     else
  52.         self.fullMessages = options.fullMessages
  53.     end
  54.  
  55.     self.failedTextColor = options.failedTextColor or FailureTextColor
  56.     self.failedBackgroundColor = options.failedBackgroundColor or FailureBackgroundColor
  57.  
  58.     self.successTextColor = options.successTextColor or SuccessTextColor
  59.     self.successBackgroundColor = options.successBackgroundColor or SuccessBackgroundColor
  60.  
  61.     self.errorTextColor = options.errorTextColor or ErrorTextColor
  62.     self.errorBackgroundColor = options.errorBackgroundColor or ErrorBackgroundColor
  63.  
  64.     self:clear()
  65.     return self
  66. end
  67.  
  68. function Presenter.resetColor(self)
  69.     self.terminal.setTextColor(colors.white)
  70.     self.terminal.setBackgroundColor(colors.black)
  71. end
  72.  
  73. function Presenter.setColorError(self)
  74.     self.terminal.setTextColor(self.errorTextColor)
  75.     self.terminal.setBackgroundColor(self.errorBackgroundColor)
  76. end
  77.  
  78. function Presenter.setColorSuccess(self)
  79.     self.terminal.setTextColor(self.successTextColor)
  80.     self.terminal.setBackgroundColor(self.successBackgroundColor)
  81. end
  82.  
  83. function Presenter.setColorFailed(self)
  84.     self.terminal.setTextColor(self.failedTextColor)
  85.     self.terminal.setBackgroundColor(self.failedBackgroundColor)
  86. end
  87.  
  88. function Presenter.writeFullMessage(self, msg)
  89.     self.terminal.write(msg)
  90.     local x, y = self.terminal.getCursorPos()
  91.     y = y + 1;
  92.     x = 1
  93.     self.terminal.setCursorPos(x, y)
  94.  
  95.     local winX, winY = self.terminal.getSize()
  96.     if y > winY then
  97.         self.terminal.scroll(1)
  98.         y = y - 1
  99.         self.terminal.setCursorPos(x, y)        
  100.     end
  101. end
  102.  
  103. function Presenter.writeSimple(self, result)
  104.     if result then
  105.         self.terminal.write(".")
  106.     else
  107.         self.terminal.write("X")
  108.     end
  109.  
  110.     local x, y = self.terminal.getCursorPos()
  111.     local winX, winY = self.terminal.getSize()
  112.  
  113.     if y > winY then
  114.         self.terminal.scroll(1)
  115.         y = y - 1
  116.         self.terminal.setCursorPos(x, y)        
  117.     end
  118. end
  119.  
  120. function Presenter.write(self, result, msg)
  121.  
  122.     msg = msg..tostring(result)
  123.  
  124.     if result then
  125.         self:setColorSuccess()
  126.     else
  127.         self:setColorFailed()
  128.     end
  129.  
  130.     if self.fullMessages then
  131.         self:writeFullMessage(msg)
  132.     else
  133.         self:writeSimple(result)
  134.     end
  135.  
  136. end
  137.  
  138. function Presenter.displayError(self, error)
  139.     self:setColorError()
  140.     self.terminal.write(error)
  141.     self:resetColor()
  142. end
  143.  
  144. function Presenter.displayResult(self, result, msg)
  145.     self:write(result, msg)
  146. end
  147.  
  148. function Presenter.displayResults(self, total, passed, failed)
  149.     local curX, curY = self.terminal.getCursorPos()
  150.     local x, y = self.terminal.getSize()
  151.  
  152.     if curY > y then
  153.  
  154.         -- Scroll diff + 1
  155.         local diff = curY - y
  156.  
  157.         self.terminal.scroll(diff)
  158.     end
  159.     self.terminal.setCursorPos(1, y)
  160.     self:resetColor()
  161.     self.terminal.write("Total tests: "..total)
  162.  
  163.     x, y = self.terminal.getCursorPos()
  164.     self.terminal.setCursorPos(x + 3, y)
  165.     self:setColorSuccess()
  166.     self.terminal.write("Passed: "..passed)
  167.  
  168.     x, y = self.terminal.getCursorPos()
  169.     self.terminal.setCursorPos(x + 3, y)
  170.     self:setColorFailed()
  171.     self.terminal.write("Failed: "..failed)
  172.  
  173.     self.terminal.setCursorPos(1, y+1)
  174. end
  175.  
  176. function newPresenter(options)
  177.     return Presenter:new(options)
  178. end
  179.  
  180. -- [[ MineUnit ]] --
  181. local mineUnit = {
  182.     assertions = {},
  183.     _totalFailed = 0,
  184.     _totalPassed = 0,
  185.     _totalTests = 0
  186. }
  187. mineUnit.__index = mineUnit
  188.  
  189. function mineUnit.new(options)
  190.     local self = setmetatable({}, mineUnit)
  191.  
  192.     options = options or {
  193.         presenter = Presenter:new(),
  194.         slowRuns = false,
  195.     }
  196.  
  197.     self.presenter = options.presenter or Presenter:new()
  198.     self.slowRuns = options.slowRuns or false
  199.  
  200.     return self
  201. end
  202.  
  203. function mineUnit.reset(self)
  204.     self._totalFailed = 0
  205.     self._totalPassed = 0
  206.     self._totalTests = 0
  207. end
  208.  
  209. function mineUnit.pushAssertion(self, assertion, v1, v2, msg)
  210.     local asrt = {
  211.         test = assertion,
  212.         v1 = v1,
  213.         v2 = v2,
  214.         msg = msg or "Failed assertion for "..tostring(v1).." and "..tostring(v2)
  215.     }
  216.     table.insert(self.assertions, asrt)
  217. end
  218.  
  219. function mineUnit.run(self, presenter)
  220.     presenter = presenter or self.presenter
  221.  
  222.     self:reset()
  223.  
  224.     presenter:writeFullMessage("--- [[ Starting MineUnit test ]] ---")
  225.  
  226.     for i,assertion in pairs(self.assertions) do
  227.         self._totalTests = self._totalTests + 1
  228.         local success, resultOrError = pcall(assertion.test, assertion.v1, assertion.v2)
  229.  
  230.         if not success then
  231.             presenter:displayError(resultOrError)
  232.             self._totalFailed = self._totalFailed + 1
  233.         else
  234.             if resultOrError then
  235.                 self._totalPassed = self._totalPassed + 1
  236.             else
  237.                 self._totalFailed = self._totalFailed + 1
  238.             end
  239.             presenter:displayResult(resultOrError, assertion.msg)
  240.         end
  241.  
  242.         if self.slowRuns then
  243.             sleep(0.5)
  244.         end
  245.     end
  246.  
  247.     presenter:displayResults(self._totalTests, self._totalPassed, self._totalFailed)
  248. end
  249.  
  250. function new(options)
  251.     return mineUnit.new(options)
  252. end
  253.  
  254. -- [[ Assertions ]] --
  255. function mineUnit.is(self, v1, v2, msg)
  256.     local function test(v1, v2)
  257.         return v1 == v2
  258.     end
  259.     self:pushAssertion(test, v1, v2, msg or "Assert that "..tostring(v1).." is "..tostring(v2).." is ");
  260. end
  261.  
  262. function mineUnit.isTrue(self, v, printMessage)
  263.     self:is(v, true, printMessage or "Assert that "..tostring(v).." is true is ")
  264. end
  265.  
  266. function mineUnit.isFalse(self, v, printMessage)
  267.     self:is(v, false, printMessage or "Assert that "..tostring(v).." is false is ")
  268. end
  269.  
  270. function mineUnit.isSmaller(self, v1, v2, printMessage)
  271.     local function test(v1, v2)
  272.         return v1 < v2
  273.     end
  274.     self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is smaller then "..tostring(v2).." is ")
  275. end
  276.  
  277. function mineUnit.isGreater(self, v1, v2, printMessage)
  278.     local function test(v1, v2)
  279.         return v1 > v2
  280.     end
  281.     self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is greater then "..tostring(v2).." is ")
  282. end
  283.  
  284. function mineUnit.isSmallerOrEqual(self, v1, v2, printMessage)
  285.     local function test(v1, v2)
  286.         return v1 <= v2
  287.     end
  288.     self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is smaller or equal to "..tostring(v2).." is ")
  289. end
  290.  
  291. function mineUnit.isGreaterOrEqual(self, v1, v2)
  292.     local function test(v1, v2)
  293.         return v1 >= v2
  294.     end
  295.     self:pushAssertion(test, v1, v2, "Assert that "..tostring(v1).." is greater or equal to "..tostring(v2).." is ")
  296. end
  297.  
  298. function mineUnit.isEqual(self, v1, v2)
  299.     self:is(v1, v2, "Assert that "..tostring(v1).." is equal to "..tostring(v2).." is ")
  300. end
  301.  
  302. function mineUnit.isType(self, v, expectionType)
  303.     self:is(type(v), expectionType, "Assert that obj("..tostring(v)..") is of type "..tostring(expectionType).." is ")
  304. end
  305.  
  306. function mineUnit.shouldReturn(self, func, v, ...)
  307.  
  308.     local rtn = v
  309.     if #arg == 0 then
  310.         rtn = func()
  311.     elseif #arg == 1 then
  312.         rtn = func(arg[1])
  313.     elseif #arg == 2 then
  314.         rtn = func(arg[1],arg[2])
  315.     elseif #arg == 3 then
  316.         rtn = func(arg[1],arg[2],arg[3])
  317.     elseif #arg == 4 then
  318.         rtn = func(arg[1],arg[2],arg[3],arg[4])
  319.     else
  320.         rtn = func(arg)
  321.     end
  322.  
  323.     self:is(rtn, v, "Assert that expected return value "..tostring(v).." matches "..tostring(rtn).." is ")
  324. end
  325.  
  326. -- [[ Wrapper ]] --
  327.  
  328. function run(options, init)
  329.     local unit = mineUnit.new(options)
  330.     init(unit)
  331.     unit:run()
  332. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement