Skip to content

Commit

Permalink
Add more tests for methods with contracts
Browse files Browse the repository at this point in the history
Issue: #1866
  • Loading branch information
awelless committed Sep 23, 2023
1 parent 30b9b78 commit 41f00c0
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,41 @@ internal class KotlinAssertTimeoutAssertionsTests {
assertMessageStartsWith(error, "Tempus Fugit ==> execution exceeded timeout of 10 ms by")
}

@Test
fun `assertTimeout with value initialization in lambda`() {
val value: Int

assertTimeout(ofMillis(500)) { value = 10 }

assertEquals(10, value)
}

@Test
fun `assertTimeout with message and value initialization in lambda`() {
val value: Int

assertTimeout(ofMillis(500), "message") { value = 10 }

assertEquals(10, value)
}

@Test
fun `assertTimeout with message supplier and value initialization in lambda`() {
val value: Int
val valueInMessageSupplier: Int

assertTimeout(
timeout = ofMillis(500),
message = {
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.
"message"
},
executable = { value = 10 }
)

assertEquals(10, value)
}

// -- executable - preemptively ---

@Test
Expand Down Expand Up @@ -266,6 +301,20 @@ internal class KotlinAssertTimeoutAssertionsTests {
assertMessageEquals(error, "Tempus Fugit ==> execution timed out after 10 ms")
}

@Test
fun `assertTimeoutPreemptively with message supplier and value initialization in lambda`() {
val valueInMessageSupplier: Int

assertTimeoutPreemptively(
timeout = ofMillis(500),
message = {
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.
"message"
},
executable = {}
)
}

/**
* Take a nap for 100 milliseconds.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ class KotlinAssertionsTests {
assertThrows<AssertionError>({ "should fail" }) { fail(null as Throwable?) }
}

@Test
fun `assertThrows with message supplier`() {
val valueInMessageSupplier: Int

assertThrows<AssertionError>({
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.

"should fail"
}) { fail("message") }
}

@Test
fun `expected context exception testing`() = runBlocking<Unit> {
assertThrows<AssertionError>("Should fail async") {
Expand Down Expand Up @@ -184,6 +195,38 @@ class KotlinAssertionsTests {
)
)

@Test
fun `assertDoesNotThrow with value initialization in lambda`() {
val value: Int

assertDoesNotThrow { value = 10 }

assertEquals(10, value)
}

@Test
fun `assertDoesNotThrow with message and value initialization in lambda`() {
val value: Int

assertDoesNotThrow("message") { value = 10 }

assertEquals(10, value)
}

@Test
fun `assertDoesNotThrow with message supplier and value initialization in lambda`() {
val value: Int
val valueInMessageSupplier: Int

assertDoesNotThrow({
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.

"message"
}) { value = 10 }

assertEquals(10, value)
}

@Test
fun `assertAll with stream of functions that throw AssertionErrors`() {
val multipleFailuresError = assertThrows<MultipleFailuresError>("Should have thrown multiple errors") {
Expand Down Expand Up @@ -219,6 +262,29 @@ class KotlinAssertionsTests {
assertFalse(nullableString.isEmpty()) // A smart cast to a non-nullable object.
}

@Test
fun `assertNotNull with message and compiler smart cast`() {
val nullableString: String? = "string"

assertNotNull(nullableString, "nullableString is null")
assertFalse(nullableString.isEmpty()) // A smart cast to a non-nullable object.
}

@Test
fun `assertNotNull with message supplier and compiler smart cast`() {
val nullableString: String? = "string"

val valueInMessageSupplier: Int

assertNotNull(nullableString) {
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.

"nullableString is null"
}

assertFalse(nullableString.isEmpty()) // A smart cast to a non-nullable object.
}

@Test
fun `assertNull with compiler smart cast`() {
val nullableString: String? = null
Expand All @@ -228,20 +294,45 @@ class KotlinAssertionsTests {
// nullableString?.isEmpty()
}

@Test
fun `assertNull with message and compiler smart cast`() {
val nullableString: String? = null

assertNull(nullableString, "nullableString is not null")
// Even safe call is not allowed because compiler knows that nullableString is always null.
// nullableString?.isEmpty()
}

@Test
fun `assertNull with message supplier and compiler smart cast`() {
val nullableString: String? = null

val valueInMessageSupplier: Int

assertNull(nullableString) {
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.

"nullableString is not null"
}

// Even safe call is not allowed because compiler knows that nullableString is always null.
// nullableString?.isEmpty()
}

@Test
fun `assertInstanceOf with compiler smart cast`() {
val string: Any = "string"
val maybeString: Any = "string"

assertInstanceOf<String>(string)
assertFalse(string.isEmpty()) // A smart cast to a String object.
assertInstanceOf<String>(maybeString)
assertFalse(maybeString.isEmpty()) // A smart cast to a String object.
}

@Test
fun `assertInstanceOf with compiler nullable smart cast`() {
val string: Any? = "string"
val maybeString: Any? = "string"

assertInstanceOf<String>(string)
assertFalse(string.isEmpty()) // A smart cast to a non-nullable String object.
assertInstanceOf<String>(maybeString)
assertFalse(maybeString.isEmpty()) // A smart cast to a non-nullable String object.
}

@Test
Expand All @@ -254,14 +345,26 @@ class KotlinAssertionsTests {
}

@Test
fun `assertDoesNotThrow with value initialization in lambda`() {
val value: Int
fun `assertInstanceOf with message and compiler smart cast`() {
val maybeString: Any = "string"

assertInstanceOf<String>(maybeString, "maybeString is not an instance of String")
assertFalse(maybeString.isEmpty()) // A smart cast to a String object.
}

@Test
fun `assertInstanceOf with message supplier and compiler smart cast`() {
val maybeString: Any = "string"

val valueInMessageSupplier: Int

assertDoesNotThrow {
value = 10
assertInstanceOf<String>(maybeString) {
valueInMessageSupplier = 20 // Val can be assigned in the message supplier lambda.

"maybeString is not an instance of String"
}

assertEquals(10, value)
assertFalse(maybeString.isEmpty()) // A smart cast to a String object.
}

companion object {
Expand Down

0 comments on commit 41f00c0

Please sign in to comment.