diff --git a/CHANGELOG.md b/CHANGELOG.md index 43fd403..2a1b890 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased Changes +## 0.4.3 (2023-03-12) +* Added `under` and `over` expectations, analogous to less than and greater than. The latter were ruled out by the last word needing to be callable. +* Added shorthand `lt`, `lte`, `gt`, and `gte` for those that know and want to keep things tidy. + ## 0.4.2 (2022-01-05) * Updated `rotriever.toml` to fix deprecation warnings * Added dark theme support to the documentation site diff --git a/src/Expectation.lua b/src/Expectation.lua index 96dc2c7..0a84702 100644 --- a/src/Expectation.lua +++ b/src/Expectation.lua @@ -89,6 +89,13 @@ function Expectation.new(value) self.equal = bindSelf(self, self.equal) self.throw = bindSelf(self, self.throw) self.near = bindSelf(self, self.near) + self.over = bindSelf(self, self.over) + self.gt = bindSelf(self, self.over) + self.gte = bindSelf(self, self.gte) + self.under = bindSelf(self, self.under) + self.lt = bindSelf(self, self.under) + self.lte = bindSelf(self, self.lte) + return self end @@ -263,6 +270,110 @@ function Expectation:near(otherValue, limit) return self end +--[[ + Assert that our expectation value is greater than another value. +]] +function Expectation:over(otherValue) + assert(type(self.value) == "number", "Expectation value must be a number to use 'over'") + assert(type(otherValue) == "number", "otherValue must be a number") + + local result = self.value > otherValue == self.successCondition + + local message = formatMessage(self.successCondition, + ("Expected value to be over %f but got %f instead"):format( + otherValue, + self.value + ), + ("Expected value to not be over %f but got %f instead"):format( + otherValue, + self.value + ) + ) + + assertLevel(result, message, 3) + self:_resetModifiers() + + return self +end + +--[[ + Assert that our expectation value is greater than or equal to another value. +]] +function Expectation:gte(otherValue) + assert(type(self.value) == "number", "Expectation value must be a number to use 'gte'") + assert(type(otherValue) == "number", "otherValue must be a number") + + local result = self.value >= otherValue == self.successCondition + + local message = formatMessage(self.successCondition, + ("Expected value to be greater than or equal to %f but got %f instead"):format( + otherValue, + self.value + ), + ("Expected value to not be greater than or equal to %f but got %f instead"):format( + otherValue, + self.value + ) + ) + + assertLevel(result, message, 3) + self:_resetModifiers() + + return self +end + +--[[ + Assert that our expectation value is less than another value. +]] +function Expectation:under(otherValue) + assert(type(self.value) == "number", "Expectation value must be a number to use 'under'") + assert(type(otherValue) == "number", "otherValue must be a number") + + local result = self.value < otherValue == self.successCondition + + local message = formatMessage(self.successCondition, + ("Expected value to be under %f but got %f instead"):format( + otherValue, + self.value + ), + ("Expected value to not be under %f but got %f instead"):format( + otherValue, + self.value + ) + ) + + assertLevel(result, message, 3) + self:_resetModifiers() + + return self +end + +--[[ + Assert that our expectation value is less than or equal to another value. +]] +function Expectation:lte(otherValue) + assert(type(self.value) == "number", "Expectation value must be a number to use 'lte'") + assert(type(otherValue) == "number", "otherValue must be a number") + + local result = self.value <= otherValue == self.successCondition + + local message = formatMessage(self.successCondition, + ("Expected value to be less than or equal to %f but got %f instead"):format( + otherValue, + self.value + ), + ("Expected value to not be less than or equal to %f but got %f instead"):format( + otherValue, + self.value + ) + ) + + assertLevel(result, message, 3) + self:_resetModifiers() + + return self +end + --[[ Assert that our functoid expectation value throws an error when called. An optional error message can be passed to assert that the error message diff --git a/tests/Expectation.lua b/tests/Expectation.lua index cbb8133..ff5cf97 100644 --- a/tests/Expectation.lua +++ b/tests/Expectation.lua @@ -266,4 +266,136 @@ return { assert(success, "should succeed") end, + ["a greater than b should succeed when a>b"] = function() + local expect = Expectation.new(2) + + local success = pcall(function() + expect:over(1) + end) + + assert(success, "should succeed") + end, + ["a greater than b should fail when equal"] = function() + local expect = Expectation.new(2) + + local success, message = pcall(function() + expect:over(2) + end) + + assert(not success, "should fail") + assert( + message:match('Expected value to be over %d+.%d+ but got %d+.%d+ instead'), + ("Error message does not match:\n%s\n"):format(message) + ) + end, + ["a greater than b should fail when ab"] = function() + local expect = Expectation.new(2) + + local success = pcall(function() + expect:gte(1) + end) + + assert(success, "should succeed") + end, + ["a greater than or equal to b should succeed when equal"] = function() + local expect = Expectation.new(2) + + local success = pcall(function() + expect:gte(2) + end) + + assert(success, "should succeed") + end, + ["a greater than or equal to b should fail when a