Expectations/Assertions library for Kotlin with some fluent syntax
Expect.kt is a set of assertions that allow you to assert the outcome of testing state using a more fluent and specific set of assertions than you get from the kotlin.test
. Each type has a set of extension functions that allow you to check certain specifics that relate to that type.
Rather than fulfilling a specific need this project was started as a means to understand the Kotlin language and explore its features (and limitations). As it stands most of the assertions are not making good use of the "Kotlin way" and are shamelessly ripped off FluentAssertions for .NET. The tweaking and improving come next plus there are actually some really nice bits of testing code with Kotlin.
Expect.kt allows you to use two different approaches to defining an assertion.
Expect syntax offers typical expect(something).toBeInACertainState style of grammar. The following test highlight this appraoch.
expect("james").toBe("james")
It is also possible to use a more fluent syntax to make the same assertion
"james".should.be("james")
Expect.kt allows you to chain assertions to keep things really neat and tidy.
expect("james").toStartWith("j").and.toEndWith("s")
Or, in fluent syntax
"james".should.startWith("j").and.endWith("s")
There are a few assertions that can be used against any type,
Assert that the state is the value passed in,
expect("james").toBe("james")
"james".should.be("james")
Assert that the state is NOT the value passed in,
expect("james").toNotBe("henry")
"james".should.notBe("henry")
Assert that the state is null,
expect(null).toBeNull()
null.should.beNull()
Assert that the state is NOT null
expect("james").toNotBeNull()
"james".should.notBeNull()
Assert that the value is true
expect(true).toBeTrue()
true.should.beTrue()
Assert that the state is NOT true
expect(false).toNotBeTrue()
false.should.notBeTrue()
Assert that the value is false
expect(false).toBeFalse()
false.should.beFalse()
Assert that the state is NOT false
expect(true).toNotBeFalse()
true.should.notBeFalse()
Assert that a string is empty ("").
expect("").toBeEmpty()
"".should.beEmpty()
Assert that a string is not empty ("").
expect(" ").toNotBeEmpty()
" ".should.beNotEmpty()
Assert that a string is of a certain length.
expect("james").toHaveLength(5)
"james".should.haveLength(5)
Assert that a string is blank, in that it is empty or contains only whitespace.
expect(" ").toBeBlank()
" ".should.beBlank()
Assert that a string is not blank, in that it isn't empty or contains more than whitespace.
expect("james").toNotBeBlank()
"james".should.notBeBlank()
Assert that a string is equal to another string regardless of case.
expect("JAMES").toBeEquivalentTo("james")
"JAMES".should.beEquivalentTo("james")
Assert that a string ends with another string
expect("James").toEndWith("es")
"James".should.endWith("es")
Assert that a string ends with another string regardless of case.
expect("JAMES").toEndWithEquivalent("es")
"JAMES".should.endWithEquivalent("es")
Assert that a string contains another string.
expect("JAMES").toContain("AM")
"JAMES".should.contain("AM)
Assert that a string contains another string regardless of case.
Assert that a string doesn't contain another string.
Assert that a string doesn't contain another string regardless of case.
Assert that a string starts with another string.
Assert that a string starts with another string regardless of case.
Assert that a string matches the given regex.
Assert that a number is greater than or equal to another
Assert that a number is greater than to another
Assert that a number is less than or equal to another
Assert that a number is less than to another
Assert that a number is within the lower and upper bounds passed in
Assert that a number is approximately another, the level of error is defined by the tolerance value (+/- tolerance)
Assert that the given date is after the passed in date.
Assert that the given date is before the passed in date.