|
| 1 | +package com.memozr.assertk |
| 2 | + |
| 3 | +import com.memozr.assertk.ObjectStuff.notNull |
| 4 | +import com.nhaarman.mockito_kotlin.spy |
| 5 | +import com.nhaarman.mockito_kotlin.verify |
| 6 | +import org.assertj.core.api.AbstractObjectAssert |
| 7 | +import org.assertj.core.api.Assertions |
| 8 | +import org.junit.Test |
| 9 | + |
| 10 | +class `Object assert test` { |
| 11 | + val nullObject: Any? = null |
| 12 | + |
| 13 | + lateinit var mockAssertion: AbstractObjectAssert<*, Any> |
| 14 | + val _expect = object : AssertionHook { |
| 15 | + override fun <A: Any> that(subjectUnderTest: A?): ObjectAssert<A> { |
| 16 | + val spy: AbstractObjectAssert<*, A?>? = spy(Assertions.assertThat(subjectUnderTest)) |
| 17 | + mockAssertion = spy as AbstractObjectAssert<*, Any> |
| 18 | + return ObjectAssert(subjectUnderTest, mockAssertion) as ObjectAssert<A> |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `isEqualTo performs logical equality`() { |
| 24 | + _expect that Unit isEqualTo Unit |
| 25 | + |
| 26 | + val expected = Any() |
| 27 | + _expect thatThrownBy { |
| 28 | + _expect that Any() isEqualTo expected |
| 29 | + } |
| 30 | + |
| 31 | + verify(mockAssertion).isEqualTo(expected) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `isNotEqualTo performs logical inequality`() { |
| 36 | + _expect that Any() isNotEqualTo Any() |
| 37 | + |
| 38 | + _expect thatThrownBy { |
| 39 | + _expect that Unit isNotEqualTo Unit |
| 40 | + } |
| 41 | + |
| 42 | + verify(mockAssertion).isNotEqualTo(Unit) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `_is notNull checks whether object is null`() { |
| 47 | + _expect that Any() _is notNull |
| 48 | + |
| 49 | + _expect thatThrownBy { |
| 50 | + _expect that nullObject _is notNull |
| 51 | + } |
| 52 | + verify(mockAssertion).isNotNull() |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `_is null checks whether object is not null`() { |
| 57 | + _expect that nullObject _is null |
| 58 | + |
| 59 | + _expect thatThrownBy { |
| 60 | + _expect that Any() _is null |
| 61 | + } |
| 62 | + verify(mockAssertion).isNull() |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `isInstance of checks for object instance`() { |
| 67 | + val anObject = Any() |
| 68 | + |
| 69 | + _expect that anObject isInstance of<Any>() |
| 70 | + verify(mockAssertion).isInstanceOf(Any::class.java) |
| 71 | + |
| 72 | + _expect thatThrownBy { |
| 73 | + _expect that anObject isInstance of<Unit>() |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `describedAs describes the object when referring to it in failure messages`() { |
| 80 | + _expect thatThrownBy { |
| 81 | + _expect that Any() describedAs "A labeled object" isInstance of<Unit>() |
| 82 | + } hasMessageContaining "A labeled object" |
| 83 | + |
| 84 | + verify(mockAssertion).describedAs("A labeled object") |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun `block _expections are supported`() { |
| 89 | + _expect that Any() isSuchThat { |
| 90 | + it _is notNull |
| 91 | + it isInstance of<Any>() |
| 92 | + it isNotEqualTo Unit |
| 93 | + it isNotEqualTo Any() |
| 94 | + } |
| 95 | + |
| 96 | + _expect thatThrownBy { |
| 97 | + _expect that Any() isSuchThat { |
| 98 | + it _is null |
| 99 | + it isInstance of<Unit>() |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments