Advertisement
AMONUWNA

TestApi

Feb 14th, 2020
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. Test = {
  2.   new = function(self, title)
  3.     local new = {}
  4.     setmetatable(new, {__index = self})
  5.  
  6.     self._maxCharsInLine = 50
  7.     self._title = title
  8.    
  9.     self._printTestTitle(self)
  10.    
  11.     return new
  12.   end,
  13.  
  14.   _printTestTitle = function(self)
  15.     self._centerText(self, "-", self._title)
  16.   end,
  17.  
  18.   _centerText = function(self, centeringChar, text)
  19.     local textSize = string.len(text)
  20.     local centeringStringSize = math.floor((self._maxCharsInLine - textSize)/2)
  21.  
  22.     for i = 0, centeringStringSize do
  23.       io.write(centeringChar)
  24.     end
  25.  
  26.     io.write(text)
  27.  
  28.     for i = 0, centeringStringSize do
  29.       io.write(centeringChar)
  30.     end
  31.    
  32.     io.write("\n")
  33.   end,
  34.  
  35.   describe = function(self, description, tests)
  36.     print(description)
  37.  
  38.     for _, test in pairs(tests) do
  39.       io.write("  ")
  40.       test()
  41.     end
  42.   end,
  43.  
  44.   it = function(self, testText, testCode)
  45.     return function ()
  46.       local result = function (value)
  47.         return TestResult:new(testText, value)
  48.       end
  49.          
  50.       testCode(result)
  51.     end
  52.   end
  53. }
  54.  
  55. TestResult = {
  56.   new = function(self, testText, value)
  57.     local new = {}
  58.     setmetatable(new, {__index = self})
  59.     self.startValue = value
  60.     self.testText = testText
  61.     return new
  62.   end,
  63.  
  64.   _printResult = function(self, value)
  65.     if value == true then
  66.       io.write("PASSED - ")
  67.     else
  68.       io.write("ERROR  - ")
  69.     end
  70.    
  71.     io.write("It " .. self.testText .. "\n")
  72.   end,
  73.  
  74.   isEqual = function(self, value)
  75.     self._printResult(self, self.startValue == value)
  76.   end,
  77.  
  78.   isNotEqual = function(self, value)
  79.     self._printResult(self, self.startValue ~= value)
  80.   end,
  81.  
  82.   isLess = function(self, value)
  83.     self._printResult(self, self.startValue < value)
  84.   end,
  85.  
  86.   isLessOrEqual = function(self, value)
  87.     self._printResult(self, self.startValue <= value)
  88.   end,
  89.  
  90.   isLarge = function(self, value)
  91.     self._printResult(self, self.startValue > value)
  92.   end,
  93.  
  94.   isLargeOrEqual = function(self, value)
  95.     self._printResult(self, self.startValue >= value)
  96.   end,
  97.  
  98.   isTrue = function(self)
  99.     self._printResult(self, self.startValue == true)
  100.   end,
  101.  
  102.   isFalse = function(self)
  103.     self._printResult(self, self.startValue == false)
  104.   end,
  105.  
  106.   isNil = function(self)
  107.     self._printResult(self, self.startValue == nil)
  108.   end,
  109.  
  110.   isNotNil = function(self)
  111.     self._printResult(self, self.startValue ~= nil)
  112.   end,
  113.  
  114.   throwError = function(self, errorName)
  115.     local status, retval = pcall(self.startValue);
  116.  
  117.     if status == false then
  118.       retval = self._splitString(self, retval, ": ")[2]
  119.     end
  120.  
  121.     self._printResult(self, errorName == retval and status == false)
  122.   end,
  123.  
  124.   _splitString = function(self, s, delimiter)
  125.     local result = {};
  126.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  127.         table.insert(result, match);
  128.     end
  129.     return result;
  130.   end
  131. }
  132.  
  133. return Test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement