Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Test = {
- new = function(self, title)
- local new = {}
- setmetatable(new, {__index = self})
- self._maxCharsInLine = 50
- self._title = title
- self._printTestTitle(self)
- return new
- end,
- _printTestTitle = function(self)
- self._centerText(self, "-", self._title)
- end,
- _centerText = function(self, centeringChar, text)
- local textSize = string.len(text)
- local centeringStringSize = math.floor((self._maxCharsInLine - textSize)/2)
- for i = 0, centeringStringSize do
- io.write(centeringChar)
- end
- io.write(text)
- for i = 0, centeringStringSize do
- io.write(centeringChar)
- end
- io.write("\n")
- end,
- describe = function(self, description, tests)
- print(description)
- for _, test in pairs(tests) do
- io.write(" ")
- test()
- end
- end,
- it = function(self, testText, testCode)
- return function ()
- local result = function (value)
- return TestResult:new(testText, value)
- end
- testCode(result)
- end
- end
- }
- TestResult = {
- new = function(self, testText, value)
- local new = {}
- setmetatable(new, {__index = self})
- self.startValue = value
- self.testText = testText
- return new
- end,
- _printResult = function(self, value)
- if value == true then
- io.write("PASSED - ")
- else
- io.write("ERROR - ")
- end
- io.write("It " .. self.testText .. "\n")
- end,
- isEqual = function(self, value)
- self._printResult(self, self.startValue == value)
- end,
- isNotEqual = function(self, value)
- self._printResult(self, self.startValue ~= value)
- end,
- isLess = function(self, value)
- self._printResult(self, self.startValue < value)
- end,
- isLessOrEqual = function(self, value)
- self._printResult(self, self.startValue <= value)
- end,
- isLarge = function(self, value)
- self._printResult(self, self.startValue > value)
- end,
- isLargeOrEqual = function(self, value)
- self._printResult(self, self.startValue >= value)
- end,
- isTrue = function(self)
- self._printResult(self, self.startValue == true)
- end,
- isFalse = function(self)
- self._printResult(self, self.startValue == false)
- end,
- isNil = function(self)
- self._printResult(self, self.startValue == nil)
- end,
- isNotNil = function(self)
- self._printResult(self, self.startValue ~= nil)
- end,
- throwError = function(self, errorName)
- local status, retval = pcall(self.startValue);
- if status == false then
- retval = self._splitString(self, retval, ": ")[2]
- end
- self._printResult(self, errorName == retval and status == false)
- end,
- _splitString = function(self, s, delimiter)
- local result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- }
- return Test
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement