Skip to content

Commit 231aef0

Browse files
authored
Merge pull request #31 from jGleitz/word-parts-from-notation
feat: Word#flatMapParts & Word#partsInNotation
2 parents 481b83d + c26edb1 commit 231aef0

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/main/kotlin/Word.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ class Word(val parts: Sequence<String>) {
2828
*/
2929
fun mapParts(transform: (String) -> String) = Word(parts.map(transform))
3030

31+
/**
32+
* Creates a new word, with all its parts transformed by the provided [transform] function, which may return more than one new part for
33+
* every existing part.
34+
*/
35+
fun flatMapParts(transform: (String) -> Sequence<String>) = Word(parts.flatMap(transform))
36+
37+
/**
38+
* Creates a new words, with all its parts parsed by the provided [notation]. Allows to parse words that use a combination of notations.
39+
*/
40+
fun partsFromNotation(notation: StringNotation) = Word(parts.flatMap { it.fromNotation(notation).parts })
41+
3142
/**
3243
* Appends a part to this word.
3344
*/
@@ -39,6 +50,10 @@ class Word(val parts: Sequence<String>) {
3950
operator fun plus(word: Word) = Word(parts + word.parts)
4051

4152
override fun toString() = "Word(${parts.joinToString { "\"$it\"" }})"
53+
54+
override fun equals(other: Any?) = this === other || (other is Word && partsList == other.partsList)
55+
56+
override fun hashCode() = partsList.hashCode()
4257
}
4358

4459
/**

src/test/kotlin/WordTest.kt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,27 @@ package de.joshuagleitze.stringnotation
33
import ch.tutteli.atrium.api.fluent.en_GB.asIterable
44
import ch.tutteli.atrium.api.fluent.en_GB.containsExactly
55
import ch.tutteli.atrium.api.fluent.en_GB.feature
6+
import ch.tutteli.atrium.api.fluent.en_GB.notToBe
7+
import ch.tutteli.atrium.api.fluent.en_GB.toBe
68
import ch.tutteli.atrium.api.verbs.expect
79
import org.junit.jupiter.api.Test
810

911
class WordTest {
12+
@Test
13+
fun `implements #equals`() {
14+
val aInstance = Word("a")
15+
expect(aInstance).toBe(aInstance)
16+
expect(aInstance).toBe(Word("a"))
17+
expect(Word("a")).notToBe(Word("A"))
18+
}
19+
20+
@Test
21+
fun `implements #hashCode`() {
22+
expect(Word("a"))
23+
.feature(Word::hashCode)
24+
.toBe(Word("a").hashCode())
25+
}
26+
1027
@Test
1128
fun `exposes parts as list`() {
1229
expect(Word("with", "parts")).feature(Word::partsList).containsExactly("with", "parts")
@@ -24,22 +41,34 @@ class WordTest {
2441
@Test
2542
fun `allows to add parts`() {
2643
expect((Word("with") + "more" + "parts"))
27-
.feature(Word::partsList)
28-
.containsExactly("with", "more", "parts")
44+
.toBe(Word("with", "more", "parts"))
2945
}
3046

3147
@Test
3248
fun `allows to add words`() {
3349
expect(Word("with") + Word("more", "parts"))
34-
.feature(Word::partsList)
35-
.containsExactly("with", "more", "parts")
50+
.toBe(Word("with", "more", "parts"))
3651
}
3752

3853
@Test
39-
fun `allows to transform parts`() {
54+
fun `allows to map parts`() {
4055
expect(Word("a", "b", "c"))
4156
.feature(Word::mapParts, String::toUpperCase)
42-
.feature(Word::partsList)
43-
.containsExactly("A", "B", "C");
57+
.toBe(Word("A", "B", "C"))
58+
}
59+
60+
@Test
61+
fun `allows to flatMap parts`() {
62+
expect(Word("a", "b"))
63+
.feature(Word::flatMapParts) { it -> sequenceOf("${it}1", "${it}2") }
64+
.toBe(Word("a1", "a2", "b1", "b2"))
65+
}
66+
67+
@Test
68+
fun `allows to parse parts from a notation`() {
69+
expect("these are words with UpperCamelCase")
70+
.feature(String::fromNotation, NormalWords)
71+
.feature(Word::partsFromNotation, UpperCamelCase)
72+
.toBe(Word("these", "are", "words", "with", "upper", "camel", "case"))
4473
}
4574
}

0 commit comments

Comments
 (0)