diff --git a/README.md b/README.md index 3f2a4dd..d1837f7 100644 --- a/README.md +++ b/README.md @@ -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 )` | diff --git a/lua/gluatest/expectations/negative.lua b/lua/gluatest/expectations/negative.lua index 681f316..48cf452 100644 --- a/lua/gluatest/expectations/negative.lua +++ b/lua/gluatest/expectations/negative.lua @@ -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 diff --git a/lua/gluatest/expectations/positive.lua b/lua/gluatest/expectations/positive.lua index f9b1c21..b78b78e 100644 --- a/lua/gluatest/expectations/positive.lua +++ b/lua/gluatest/expectations/positive.lua @@ -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 )