Skip to content
This repository has been archived by the owner on Sep 14, 2024. It is now read-only.

Address the lack of greater.than, and less.than expectations. #175

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions src/Expectation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
132 changes: 132 additions & 0 deletions tests/Expectation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 a<b"] = function()
local expect = Expectation.new(2)

local success, message = pcall(function()
expect:over(3)
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 or equal to b should succeed when a>b"] = 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<b"] = function()
local expect = Expectation.new(2)

local success, message = pcall(function()
expect:gte(3)
end)

assert(not success, "should fail")
assert(
message:match('Expected value to be greater than or equal to %d+.%d+ but got %d+.%d+ instead'),
("Error message does not match:\n%s\n"):format(message)
)
end,
["a less than b should succeed when a<b"] = function()
local expect = Expectation.new(1)

local success = pcall(function()
expect:under(2)
end)

assert(success, "should succeed")
end,
["a less than b should fail when equal"] = function()
local expect = Expectation.new(2)

local success, message = pcall(function()
expect:under(2)
end)

assert(not success, "should fail")
assert(
message:match('Expected value to be under %d+.%d+ but got %d+.%d+ instead'),
("Error message does not match:\n%s\n"):format(message)
)
end,
["a less than b should fail when a<b"] = function()
local expect = Expectation.new(3)

local success, message = pcall(function()
expect:under(2)
end)

assert(not success, "should fail")
assert(
message:match('Expected value to be under %d+.%d+ but got %d+.%d+ instead'),
("Error message does not match:\n%s\n"):format(message)
)
end,
["a less or equal to b should succeed when a<b"] = function()
local expect = Expectation.new(1)

local success = pcall(function()
expect:lte(2)
end)

assert(success, "should succeed")
end,
["a less or equal to b should succeed when equal"] = function()
local expect = Expectation.new(2)

local success = pcall(function()
expect:lte(2)
end)

assert(success, "should succeed")
end,
["a less or equal to b should fail when a<b"] = function()
local expect = Expectation.new(3)

local success, message = pcall(function()
expect:lte(2)
end)

assert(not success, "should fail")
assert(
message:match('Expected value to be less than or equal to %d+.%d+ but got %d+.%d+ instead'),
("Error message does not match:\n%s\n"):format(message)
)
end,
}