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
Changes from 1 commit
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
Next Next commit
Introduce greater and less than type conditions
ployt0 authored Mar 12, 2023

Verified

This commit was signed with the committer’s verified signature.
utkarshgupta137 Utkarsh Gupta
commit 2e56b7ae997e228d99643ef61d669d11e85b49b2
111 changes: 111 additions & 0 deletions src/Expectation.lua
Original file line number Diff line number Diff line change
@@ -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