add notToBeGreaterThan and notToBeLessThan

moreover:
- make the distiction between toEqual and toBeEqualComparingTo clearer
- rewrite ComparableExpectationsSpec to new nameing and include test
  cases for toBeLessThanOrEqualTo and toBeGreaterThanOrEqualTo where
  the equals/compareTo contract is broken
This commit is contained in:
Robert Stoll
2021-04-24 22:53:11 +02:00
parent 97d0133680
commit 44cd5e05b3
14 changed files with 421 additions and 167 deletions

View File

@@ -10,135 +10,180 @@ import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
abstract class ComparableExpectationsSpec(
isLessThan: Fun1<Int, Int>,
isLessOrEquals: Fun1<Int, Int>,
isGreaterThan: Fun1<Int, Int>,
isGreaterOrEquals: Fun1<Int, Int>,
isEqualComparingTo: Fun1<Int, Int>,
isEqualComparingTo2: Fun1<DiffEqualsCompareTo, DiffEqualsCompareTo>,
toBeLessThan: Fun1<Int, Int>,
toBeLessThanOrEqualTo: Fun1<Int, Int>,
toBeEqualComparingTo: Fun1<Int, Int>,
toBeGreaterThanOrEqualTo: Fun1<Int, Int>,
toBeGreaterThan: Fun1<Int, Int>,
toBeLessThan2: Fun1<DiffEqualsCompareTo, DiffEqualsCompareTo>,
toBeEqualComparingTo2: Fun1<DiffEqualsCompareTo, DiffEqualsCompareTo>,
toBeGreaterThanOrEqualTo2: Fun1<DiffEqualsCompareTo, DiffEqualsCompareTo>,
toBeLessThanOrEqualToDescr: String = IS_LESS_THAN_OR_EQUAL.getDefault(),
toBeGreaterThanOrEqualToDescr: String = IS_GREATER_THAN_OR_EQUAL.getDefault(),
describePrefix: String = "[Atrium] "
) : Spek({
include(object : SubjectLessSpec<Int>(
describePrefix,
isLessThan.forSubjectLess(1),
isLessOrEquals.forSubjectLess(1),
isGreaterThan.forSubjectLess(1),
isGreaterOrEquals.forSubjectLess(1),
isEqualComparingTo.forSubjectLess(1)
toBeLessThan.forSubjectLess(1),
toBeLessThanOrEqualTo.forSubjectLess(1),
toBeEqualComparingTo.forSubjectLess(1),
toBeGreaterThanOrEqualTo.forSubjectLess(1),
toBeGreaterThan.forSubjectLess(1)
) {})
val isLessThanDescr = IS_LESS_THAN.getDefault()
val isLessOrEqualsDescr = IS_LESS_THAN_OR_EQUAL.getDefault()
val isGreaterThanDescr = IS_GREATER_THAN.getDefault()
val isGreaterOrEqualsDescr = IS_GREATER_THAN_OR_EQUAL.getDefault()
val isEqualComparingToDescr = IS_EQUAL.getDefault()
val toBeLessThanDescr = IS_LESS_THAN.getDefault()
val toBeGreaterThanDescr = IS_GREATER_THAN.getDefault()
val toBeEqualComparingToDescr = IS_EQUAL.getDefault()
val fluent = expect(10)
describe("$describePrefix context subject is 10") {
context("${isLessThan.name} ...") {
val isLessThanFun = isLessThan.lambda
val fluent = expect(10)
context("${toBeLessThan.name} ...") {
val toBeLessThanFun = toBeLessThan.lambda
it("... 11 does not throw") {
fluent.isLessThanFun(11)
fluent.toBeLessThanFun(11)
}
it("... 10 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_LESS_THAN and `: 10`") {
expect {
fluent.isLessThanFun(10)
}.toThrow<AssertionError> { messageContains("$isLessThanDescr: 10") }
fluent.toBeLessThanFun(10)
}.toThrow<AssertionError> { messageContains("$toBeLessThanDescr: 10") }
}
it("... 9 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_LESS_THAN and `: 10`") {
expect {
fluent.isLessThanFun(9)
}.toThrow<AssertionError> { messageContains("$isLessThanDescr: 9") }
fluent.toBeLessThanFun(9)
}.toThrow<AssertionError> { messageContains("$toBeLessThanDescr: 9") }
}
}
describe("${isLessOrEquals.name} ...") {
val isLessOrEqualsFun = isLessOrEquals.lambda
describe("${toBeLessThanOrEqualTo.name} ...") {
val toBeLessThanOrEqualToFun = toBeLessThanOrEqualTo.lambda
it("... 11 does not throw") {
fluent.isLessOrEqualsFun(11)
fluent.toBeLessThanOrEqualToFun(11)
}
it("... 10 does not throw") {
fluent.isLessOrEqualsFun(10)
fluent.toBeLessThanOrEqualToFun(10)
}
it("... 9 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_LESS_THAN_OR_EQUAL and `: 10`") {
expect {
fluent.isLessOrEqualsFun(9)
}.toThrow<AssertionError> { messageContains("$isLessOrEqualsDescr: 9") }
fluent.toBeLessThanOrEqualToFun(9)
}.toThrow<AssertionError> { messageContains("$toBeLessThanOrEqualToDescr: 9") }
}
}
describe("${isGreaterThan.name} ...") {
val isGreaterThanFun = isGreaterThan.lambda
it("... 11 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN and `: 11`") {
expect {
fluent.isGreaterThanFun(11)
}.toThrow<AssertionError> { messageContains("$isGreaterThanDescr: 11") }
}
it("... 10 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN and `: 10`") {
expect {
fluent.isGreaterThanFun(10)
}.toThrow<AssertionError> { messageContains("$isGreaterThanDescr: 10") }
}
it("... 9 does not throw") {
fluent.isGreaterThanFun(9)
}
}
describe("${isGreaterOrEquals.name} ...") {
val isGreaterOrEqualsFun = isGreaterOrEquals.lambda
it("... 11 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN_OR_EQUAL and `: 11`") {
expect {
fluent.isGreaterOrEqualsFun(11)
}.toThrow<AssertionError> { messageContains("$isGreaterOrEqualsDescr: 11") }
}
it("... 10 does not throw") {
fluent.isGreaterOrEqualsFun(10)
}
it("... 9 does not throw") {
fluent.isGreaterOrEqualsFun(9)
}
}
describe("${isEqualComparingTo.name} ...") {
val isEqualComparingToFun = isEqualComparingTo.lambda
describe("${toBeEqualComparingTo.name} ...") {
val toBeEqualComparingToFun = toBeEqualComparingTo.lambda
it("... 11 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_EQUAL and `: 11`") {
expect {
fluent.isEqualComparingToFun(11)
}.toThrow<AssertionError> { messageContains("$isEqualComparingToDescr: 11") }
fluent.toBeEqualComparingToFun(11)
}.toThrow<AssertionError> { messageContains("$toBeEqualComparingToDescr: 11") }
}
it("... 10 does not throw") {
fluent.isEqualComparingToFun(10)
fluent.toBeEqualComparingToFun(10)
}
it("... 9 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_EQUAL and `: 9`") {
expect {
fluent.isEqualComparingToFun(9)
}.toThrow<AssertionError> { messageContains("$isEqualComparingToDescr: 9") }
fluent.toBeEqualComparingToFun(9)
}.toThrow<AssertionError> { messageContains("$toBeEqualComparingToDescr: 9") }
}
}
describe("${toBeGreaterThanOrEqualTo.name} ...") {
val toBeGreaterThanOrEqualFun = toBeGreaterThanOrEqualTo.lambda
it("... 11 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN_OR_EQUAL and `: 11`") {
expect {
fluent.toBeGreaterThanOrEqualFun(11)
}.toThrow<AssertionError> { messageContains("$toBeGreaterThanOrEqualToDescr: 11") }
}
it("... 10 does not throw") {
fluent.toBeGreaterThanOrEqualFun(10)
}
it("... 9 does not throw") {
fluent.toBeGreaterThanOrEqualFun(9)
}
}
describe("${toBeGreaterThan.name} ...") {
val toBeGreaterThanFun = toBeGreaterThan.lambda
it("... 11 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN and `: 11`") {
expect {
fluent.toBeGreaterThanFun(11)
}.toThrow<AssertionError> { messageContains("$toBeGreaterThanDescr: 11") }
}
it("... 10 throws an AssertionError containing ${DescriptionComparableAssertion::class.simpleName}.$IS_GREATER_THAN and `: 10`") {
expect {
fluent.toBeGreaterThanFun(10)
}.toThrow<AssertionError> { messageContains("$toBeGreaterThanDescr: 10") }
}
it("... 9 does not throw") {
fluent.toBeGreaterThanFun(9)
}
}
}
describe("$describePrefix context subject is a class where equals is different from compareTo") {
describe("${isEqualComparingTo2.name} ...") {
val isEqualComparingToFun = isEqualComparingTo2.lambda
it("expected is same instance but compareTo returns false - throws AssertionError") {
context("${toBeLessThan2.name} ...") {
val toBeLessThanFun = toBeLessThan2.lambda
it("expected is same instance but compareTo does not return 0 but 1 - throws AssertionError") {
expect {
val subject = DiffEqualsCompareTo("welcome")
expect(subject).isEqualComparingToFun(subject)
}.toThrow<AssertionError> { messageContains("$isEqualComparingToDescr: DiffEqualsCompareTo(s=welcome)") }
expect(subject).toBeLessThanFun(subject)
}.toThrow<AssertionError> { messageContains("$toBeLessThanOrEqualToDescr: DiffEqualsCompareTo(s=welcome)") }
}
it("expected equals but compareTo returns false - throws AssertionError") {
it("expected equals but compareTo does not return 0 but 1- throws AssertionError") {
expect {
expect(DiffEqualsCompareTo("welcome")).isEqualComparingToFun(DiffEqualsCompareTo("welcome"))
}.toThrow<AssertionError> { messageContains("$isEqualComparingToDescr: DiffEqualsCompareTo(s=welcome)") }
expect(DiffEqualsCompareTo("welcome")).toBeLessThanFun(DiffEqualsCompareTo("welcome"))
}.toThrow<AssertionError> { messageContains("$toBeLessThanOrEqualToDescr: DiffEqualsCompareTo(s=welcome)") }
}
it("expected does not equal but compareTo = 0 - does not throw") {
expect(DiffEqualsCompareTo("welcome")).isEqualComparingToFun(DiffEqualsCompareTo("hello"))
it("expected does not equal but compareTo returns 0 - does not throw") {
expect(DiffEqualsCompareTo("welcome")).toBeLessThanFun(DiffEqualsCompareTo("hello"))
}
}
describe("${toBeEqualComparingTo2.name} ...") {
val toBeEqualComparingToFun = toBeEqualComparingTo2.lambda
it("expected is same instance but compareTo does not return 0 but 1 - throws AssertionError") {
expect {
val subject = DiffEqualsCompareTo("welcome")
expect(subject).toBeEqualComparingToFun(subject)
}.toThrow<AssertionError> { messageContains("$toBeEqualComparingToDescr: DiffEqualsCompareTo(s=welcome)") }
}
it("expected equals but compareTo does not return 0 but 1 - throws AssertionError") {
expect {
expect(DiffEqualsCompareTo("welcome")).toBeEqualComparingToFun(DiffEqualsCompareTo("welcome"))
}.toThrow<AssertionError> { messageContains("$toBeEqualComparingToDescr: DiffEqualsCompareTo(s=welcome)") }
}
it("expected does not equal but compareTo returns 0 - does not throw") {
expect(DiffEqualsCompareTo("welcome")).toBeEqualComparingToFun(DiffEqualsCompareTo("hello"))
}
}
describe("${toBeGreaterThanOrEqualTo2.name} ...") {
val toBeGreaterThanOrEqualFun = toBeGreaterThanOrEqualTo2.lambda
it("expected is same instance but compareTo does not return 0 but -1 - throws AssertionError") {
expect {
val subject = DiffEqualsCompareTo("allo")
expect(subject).toBeGreaterThanOrEqualFun(subject)
}.toThrow<AssertionError> { messageContains("$toBeGreaterThanOrEqualToDescr: DiffEqualsCompareTo(s=allo)") }
}
it("expected equals but compareTo does not return 0 but -1 - throws AssertionError") {
expect {
expect(DiffEqualsCompareTo("allo")).toBeGreaterThanOrEqualFun(DiffEqualsCompareTo("allo"))
}.toThrow<AssertionError> { messageContains("$toBeGreaterThanOrEqualToDescr: DiffEqualsCompareTo(s=allo)") }
}
it("expected does not equal but compareTo returns 0 - does not throw") {
expect(DiffEqualsCompareTo("welcome")).toBeGreaterThanOrEqualFun(DiffEqualsCompareTo("hello"))
}
}
}