|
| 1 | +package org.pointyware.kass.assertions |
| 2 | + |
| 3 | +import kotlin.test.Test |
| 4 | +import kotlin.test.assertEquals |
| 5 | +import kotlin.test.assertFailsWith |
| 6 | +import kotlin.test.assertFalse |
| 7 | +import kotlin.test.assertTrue |
| 8 | + |
| 9 | +/** |
| 10 | + * |
| 11 | + */ |
| 12 | +class StatementUnitTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + fun and_should_pass_only_if_both_statements_pass() { |
| 16 | + // Given two statements combined with and |
| 17 | + // And both statements pass |
| 18 | + val statement1 = object: Statement<Int> { |
| 19 | + override fun evaluate(subject: Int) = true |
| 20 | + override val failureMessage = "Statement 1 failed" |
| 21 | + } |
| 22 | + val statement2 = object: Statement<Int> { |
| 23 | + override fun evaluate(subject: Int) = true |
| 24 | + override val failureMessage = "Statement 2 failed" |
| 25 | + } |
| 26 | + val combined = statement1.and(statement2) |
| 27 | + |
| 28 | + // When the combination is evaluated |
| 29 | + val result = combined.evaluate(0) |
| 30 | + |
| 31 | + // Then the evaluation should pass |
| 32 | + // And the result should be true |
| 33 | + assertTrue(result) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun and_should_not_pass_if_first_statement_fails() { |
| 38 | + // Given two statements combined with and |
| 39 | + // And the first statement fails |
| 40 | + val statement1 = object: Statement<Int> { |
| 41 | + override fun evaluate(subject: Int) = false |
| 42 | + override val failureMessage = "Statement 1 failed" |
| 43 | + } |
| 44 | + val statement2 = object: Statement<Int> { |
| 45 | + override fun evaluate(subject: Int) = true |
| 46 | + override val failureMessage = "Statement 2 failed" |
| 47 | + } |
| 48 | + val combined = statement1.and(statement2) |
| 49 | + |
| 50 | + // When the combination is evaluated |
| 51 | + val result = combined.evaluate(0) |
| 52 | + |
| 53 | + // Then the evaluation should fail |
| 54 | + // And the failure message should contain the failure message of both statements |
| 55 | + assertFalse(result) |
| 56 | + assertEquals("Statement 1 failed or Statement 2 failed", combined.failureMessage) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun and_should_not_pass_if_second_statement_fails() { |
| 61 | + // Given two statements combined with and |
| 62 | + // And the second statement fails |
| 63 | + val statement1 = object: Statement<Int> { |
| 64 | + override fun evaluate(subject: Int) = true |
| 65 | + override val failureMessage = "Statement 1 failed" |
| 66 | + } |
| 67 | + val statement2 = object: Statement<Int> { |
| 68 | + override fun evaluate(subject: Int) = false |
| 69 | + override val failureMessage = "Statement 2 failed" |
| 70 | + } |
| 71 | + val combined = statement1.and(statement2) |
| 72 | + |
| 73 | + // When the combination is evaluated |
| 74 | + val result = combined.evaluate(0) |
| 75 | + |
| 76 | + // Then the evaluation should fail |
| 77 | + // And the failure message should contain the failure message of both statements |
| 78 | + assertFalse(result) |
| 79 | + assertEquals("Statement 1 failed or Statement 2 failed", combined.failureMessage) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun assertThat_should_pass_when_statement_passes() { |
| 84 | + // Given a subject and a statement that passes |
| 85 | + val statementFake = object: Statement<Int> { |
| 86 | + override fun evaluate(subject: Int) = true |
| 87 | + override val failureMessage = "Statement failed" |
| 88 | + } |
| 89 | + |
| 90 | + // When the statement is evaluated with the subject |
| 91 | + assertThat(0, statementFake) |
| 92 | + |
| 93 | + // Then the evaluation should pass |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + fun assertThat_should_fail_when_statement_fails() { |
| 98 | + // Given a subject and a statement that fails |
| 99 | + val statementFake = object: Statement<Int> { |
| 100 | + override fun evaluate(subject: Int) = false |
| 101 | + override val failureMessage = "Statement failed" |
| 102 | + } |
| 103 | + |
| 104 | + // When the statement is evaluated with the subject |
| 105 | + val error = assertFailsWith(AssertionError::class) { |
| 106 | + assertThat(0, statementFake) |
| 107 | + } |
| 108 | + |
| 109 | + // Then the evaluation should fail |
| 110 | + // And the message of the exception should contain the failure message of the statement |
| 111 | + assertEquals("Statement failed", error.message) |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun assumeThat_should_pass_when_statement_passes() { |
| 116 | + // Given a subject and a statement that passes |
| 117 | + val statementFake = object: Statement<Int> { |
| 118 | + override fun evaluate(subject: Int) = true |
| 119 | + override val failureMessage = "Statement failed" |
| 120 | + } |
| 121 | + |
| 122 | + // When the statement is evaluated with the subject |
| 123 | + assumeThat(0, statementFake) |
| 124 | + |
| 125 | + // Then the evaluation should pass |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun assumeThat_should_throw_FailedAssumption_when_statement_fails() { |
| 130 | + // Given a subject and a statement that fails |
| 131 | + val statementFake = object: Statement<Int> { |
| 132 | + override fun evaluate(subject: Int) = false |
| 133 | + override val failureMessage = "Statement failed" |
| 134 | + } |
| 135 | + |
| 136 | + // When the statement is evaluated with the subject |
| 137 | + val error = assertFailsWith<FailedAssumption> { |
| 138 | + assumeThat(0, statementFake) |
| 139 | + } |
| 140 | + |
| 141 | + // Then the evaluation should throw a FailedAssumption |
| 142 | + // And the message of the exception should contain the failure message of the statement |
| 143 | + assertEquals("Assumption failed - Statement failed", error.message) |
| 144 | + } |
| 145 | +} |
0 commit comments