Skip to content

Commit

Permalink
Implement CharSequence Inspectors (#2886)
Browse files Browse the repository at this point in the history
  • Loading branch information
pientaa committed Mar 20, 2022
1 parent 698b5a9 commit 3d0c726
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,38 @@ import io.kotest.matchers.comparables.beGreaterThan
import io.kotest.matchers.comparables.beLessThan
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

class InspectorsTest : WordSpec() {

private val list = listOf(1, 2, 3, 4, 5)
private val array = arrayOf(1, 2, 3, 4, 5)
private val list = listOf(1, 2, 3, 4, 5)
private val array = arrayOf(1, 2, 3, 4, 5)
private val charSequence = "charSequence"

init {
init {

"forNone" should {
"pass if no elements pass fn test for a list" {
list.forNone {
it shouldBe 10
}
}
"pass if no elements pass fn test for an array" {
array.forNone {
it shouldBe 10
}
}
"fail if one elements passes fn test" {
shouldThrow<AssertionError> {
list.forNone {
it shouldBe 4
}
}.message shouldBe """1 elements passed but expected 0
"forNone" should {
"pass if no elements pass fn test for a list" {
list.forNone {
it shouldBe 10
}
}
"pass if no elements pass fn test for a char sequence" {
charSequence.forNone {
it shouldBe 'x'
}
}
"pass if no elements pass fn test for an array" {
array.forNone {
it shouldBe 10
}
}
"fail if one elements passes fn test" {
shouldThrow<AssertionError> {
list.forNone {
it shouldBe 4
}
}.message shouldBe """1 elements passed but expected 0
The following elements passed:
4
Expand All @@ -45,13 +52,13 @@ The following elements failed:
2 => expected:<4> but was:<2>
3 => expected:<4> but was:<3>
5 => expected:<4> but was:<5>"""
}
"fail if all elements pass fn test" {
shouldThrow<AssertionError> {
list.forNone {
it should beGreaterThan(0)
}
}.message shouldBe """5 elements passed but expected 0
}
"fail if all elements pass fn test" {
shouldThrow<AssertionError> {
list.forNone {
it should beGreaterThan(0)
}
}.message shouldBe """5 elements passed but expected 0
The following elements passed:
1
Expand All @@ -62,26 +69,31 @@ The following elements passed:
The following elements failed:
--none--"""
}
}
}

"forSome" should {
"pass if one elements pass test" {
list.forSome {
it shouldBe 3
}
}
"pass if size-1 elements pass test" {
list.forSome {
it should beGreaterThan(1)
}
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forSome {
it should beLessThan(0)
}
}.message shouldBe """No elements passed but expected at least one
"forSome" should {
"pass if one elements pass test" {
list.forSome {
it shouldBe 3
}
}
"pass if size-1 elements pass test" {
list.forSome {
it should beGreaterThan(1)
}
}
"pass if two elements pass test for a char sequence" {
charSequence.forSome {
it shouldBe 'c'
}
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forSome {
it should beLessThan(0)
}
}.message shouldBe """No elements passed but expected at least one
The following elements passed:
--none--
Expand All @@ -92,13 +104,13 @@ The following elements failed:
3 => 3 should be < 0
4 => 4 should be < 0
5 => 5 should be < 0"""
}
"fail if all elements pass test" {
shouldThrow<AssertionError> {
list.forSome {
it should beGreaterThan(0)
}
}.message shouldBe """All elements passed but expected < 5
}
"fail if all elements pass test" {
shouldThrow<AssertionError> {
list.forSome {
it should beGreaterThan(0)
}
}.message shouldBe """All elements passed but expected < 5
The following elements passed:
1
Expand All @@ -109,21 +121,44 @@ The following elements passed:
The following elements failed:
--none--"""
}
}
}

"forOne" should {
"pass if one elements pass test" {
list.forOne {
it shouldBe 3
}
}
"fail if > 1 elements pass test" {
shouldThrow<AssertionError> {
list.forOne { t ->
t should beGreaterThan(2)
}
}.message shouldBe """3 elements passed but expected 1
"forOne" should {
"pass if one elements pass test" {
list.forOne {
it shouldBe 3
}
}
"fail if all elements pass test for a char sequence" {
shouldThrow<AssertionError> {
charSequence.forOne { t ->
t shouldNotBe 'X'
}
}.message shouldBe """12 elements passed but expected 1
The following elements passed:
c
h
a
r
S
e
q
u
e
n
... and 2 more passed elements
The following elements failed:
--none--"""
}
"fail if > 1 elements pass test" {
shouldThrow<AssertionError> {
list.forOne { t ->
t should beGreaterThan(2)
}
}.message shouldBe """3 elements passed but expected 1
The following elements passed:
3
Expand All @@ -133,13 +168,13 @@ The following elements passed:
The following elements failed:
1 => 1 should be > 2
2 => 2 should be > 2"""
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forOne { t ->
t shouldBe 22
}
}.message shouldBe """0 elements passed but expected 1
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forOne { t ->
t shouldBe 22
}
}.message shouldBe """0 elements passed but expected 1
The following elements passed:
--none--
Expand All @@ -150,26 +185,31 @@ The following elements failed:
3 => expected:<22> but was:<3>
4 => expected:<22> but was:<4>
5 => expected:<22> but was:<5>"""
}
}
}

"forAny" should {
"pass if one elements pass test" {
list.forAny { t ->
t shouldBe 3
}
}
"pass if at least elements pass test" {
list.forAny { t ->
t should beGreaterThan(2)
}
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forAny { t ->
t shouldBe 6
}
}.message shouldBe """0 elements passed but expected at least 1
"forAny" should {
"pass if one elements pass test" {
list.forAny { t ->
t shouldBe 3
}
}
"pass if at least elements pass test" {
list.forAny { t ->
t should beGreaterThan(2)
}
}
"pass if all elements pass test for a char sequence" {
charSequence.forAny {
it shouldNotBe 'X'
}
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
array.forAny { t ->
t shouldBe 6
}
}.message shouldBe """0 elements passed but expected at least 1
The following elements passed:
--none--
Expand All @@ -180,21 +220,26 @@ The following elements failed:
3 => expected:<6> but was:<3>
4 => expected:<6> but was:<4>
5 => expected:<6> but was:<5>"""
}
}
}

"forExactly" should {
"pass if exactly k elements pass" {
list.forExactly(2) { t ->
t should beLessThan(3)
}
}
"fail if more elements pass test" {
shouldThrow<AssertionError> {
list.forExactly(2) { t ->
t should beGreaterThan(2)
}
}.message shouldBe """3 elements passed but expected 2
"forExactly" should {
"pass if exactly k elements pass" {
list.forExactly(2) { t ->
t should beLessThan(3)
}
}
"pass if exactly k elements pass for a char sequence" {
charSequence.forExactly(1) {
it shouldBe 'h'
}
}
"fail if more elements pass test" {
shouldThrow<AssertionError> {
list.forExactly(2) { t ->
t should beGreaterThan(2)
}
}.message shouldBe """3 elements passed but expected 2
The following elements passed:
3
Expand All @@ -204,13 +249,13 @@ The following elements passed:
The following elements failed:
1 => 1 should be > 2
2 => 2 should be > 2"""
}
"fail if less elements pass test" {
shouldThrow<AssertionError> {
array.forExactly(2) { t ->
t should beLessThan(2)
}
}.message shouldBe """1 elements passed but expected 2
}
"fail if less elements pass test" {
shouldThrow<AssertionError> {
array.forExactly(2) { t ->
t should beLessThan(2)
}
}.message shouldBe """1 elements passed but expected 2
The following elements passed:
1
Expand All @@ -220,13 +265,13 @@ The following elements failed:
3 => 3 should be < 2
4 => 4 should be < 2
5 => 5 should be < 2"""
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
list.forExactly(2) { t ->
t shouldBe 33
}
}.message shouldBe """0 elements passed but expected 2
}
"fail if no elements pass test" {
shouldThrow<AssertionError> {
list.forExactly(2) { t ->
t shouldBe 33
}
}.message shouldBe """0 elements passed but expected 2
The following elements passed:
--none--
Expand All @@ -237,7 +282,7 @@ The following elements failed:
3 => expected:<33> but was:<3>
4 => expected:<33> but was:<4>
5 => expected:<33> but was:<5>"""
}
}
}
}
}
}

0 comments on commit 3d0c726

Please sign in to comment.