Skip to content

Commit

Permalink
Add aboutEqual expectations (#61)
Browse files Browse the repository at this point in the history
* Add aboutEqual expectations

* Deprecate .eq

* Update readme example
  • Loading branch information
brandonsturgeon authored Jul 1, 2024
1 parent b43f4bc commit 65fb98b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ There are a number of different expectations you can use.
| Expectation | Description | Example |
|----------------------|-----------------------------------------------------------|-----------------------------------------------------------------|
| **`equal`**/**`eq`** | Basic `==` equality check | `expect( a ).to.equal( b )` |
| **`aboutEqual`** | Basic `==` equality check, with a tolerance | `expect( 0.999 ).to.aboutEqual( 1 )` |
| **`beLessThan`** | Basic `<` comparison | `expect( 5 ).to.beLessThan( 6 )` |
| **`beGreaterThan`** | Basic `>` comparison | `expect( 10 ).to.beGreaterThan( 1 )` |
| **`beBetween`** | Expects the subject to be less than min, and greater than max | `expect( 5 ).to.beBetween( 3, 7 )` |
Expand Down
17 changes: 15 additions & 2 deletions lua/gluatest/expectations/negative.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ return function( subject, ... )

local i = expectations

function expectations.eq( comparison )
function expectations.equal( comparison )
if subject == comparison then
i.expected( "to not equal '%s'", comparison )
end
end
expectations.equal = expectations.eq

function expectations.eq( comparison )
GLuaTest.DeprecatedNotice( "toNot.eq( value )", "toNot.equal( value )" )
return expectations.equal( comparison )
end

function expectations.aboutEqual( comparison )
local tolerance = args[1] or 0.00001
local difference = math.abs( subject - comparison )

if difference <= tolerance then
i.expected( "to not be within '%s' of '%s' - found a difference of '%s'", tolerance, comparison, difference )
end
end

function expectations.beLessThan( comparison )
if subject < comparison then
Expand Down
17 changes: 15 additions & 2 deletions lua/gluatest/expectations/positive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,25 @@ return function( subject, ... )

local i = expectations

function expectations.eq( comparison )
function expectations.equal( comparison )
if subject ~= comparison then
i.expected( "to equal '%s'", comparison )
end
end
expectations.equal = expectations.eq

function expectations.eq( comparison )
GLuaTest.DeprecatedNotice( "to.eq( value )", "to.equal( value )" )
return expectations.equal( comparison )
end

function expectations.aboutEqual( comparison )
local tolerance = args[1] or 0.00001
local difference = math.abs( subject - comparison )

if difference > tolerance then
i.expected( "to be within '%s' of '%s' - found a difference of '%s'", tolerance, comparison, difference )
end
end

function expectations.beLessThan( comparison )

Expand Down

0 comments on commit 65fb98b

Please sign in to comment.