Skip to content

Commit

Permalink
Merge pull request #31 from jGleitz/word-parts-from-notation
Browse files Browse the repository at this point in the history
feat: Word#flatMapParts & Word#partsInNotation
  • Loading branch information
jGleitz authored Feb 18, 2020
2 parents 481b83d + c26edb1 commit 231aef0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/Word.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ class Word(val parts: Sequence<String>) {
*/
fun mapParts(transform: (String) -> String) = Word(parts.map(transform))

/**
* Creates a new word, with all its parts transformed by the provided [transform] function, which may return more than one new part for
* every existing part.
*/
fun flatMapParts(transform: (String) -> Sequence<String>) = Word(parts.flatMap(transform))

/**
* Creates a new words, with all its parts parsed by the provided [notation]. Allows to parse words that use a combination of notations.
*/
fun partsFromNotation(notation: StringNotation) = Word(parts.flatMap { it.fromNotation(notation).parts })

/**
* Appends a part to this word.
*/
Expand All @@ -39,6 +50,10 @@ class Word(val parts: Sequence<String>) {
operator fun plus(word: Word) = Word(parts + word.parts)

override fun toString() = "Word(${parts.joinToString { "\"$it\"" }})"

override fun equals(other: Any?) = this === other || (other is Word && partsList == other.partsList)

override fun hashCode() = partsList.hashCode()
}

/**
Expand Down
43 changes: 36 additions & 7 deletions src/test/kotlin/WordTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ package de.joshuagleitze.stringnotation
import ch.tutteli.atrium.api.fluent.en_GB.asIterable
import ch.tutteli.atrium.api.fluent.en_GB.containsExactly
import ch.tutteli.atrium.api.fluent.en_GB.feature
import ch.tutteli.atrium.api.fluent.en_GB.notToBe
import ch.tutteli.atrium.api.fluent.en_GB.toBe
import ch.tutteli.atrium.api.verbs.expect
import org.junit.jupiter.api.Test

class WordTest {
@Test
fun `implements #equals`() {
val aInstance = Word("a")
expect(aInstance).toBe(aInstance)
expect(aInstance).toBe(Word("a"))
expect(Word("a")).notToBe(Word("A"))
}

@Test
fun `implements #hashCode`() {
expect(Word("a"))
.feature(Word::hashCode)
.toBe(Word("a").hashCode())
}

@Test
fun `exposes parts as list`() {
expect(Word("with", "parts")).feature(Word::partsList).containsExactly("with", "parts")
Expand All @@ -24,22 +41,34 @@ class WordTest {
@Test
fun `allows to add parts`() {
expect((Word("with") + "more" + "parts"))
.feature(Word::partsList)
.containsExactly("with", "more", "parts")
.toBe(Word("with", "more", "parts"))
}

@Test
fun `allows to add words`() {
expect(Word("with") + Word("more", "parts"))
.feature(Word::partsList)
.containsExactly("with", "more", "parts")
.toBe(Word("with", "more", "parts"))
}

@Test
fun `allows to transform parts`() {
fun `allows to map parts`() {
expect(Word("a", "b", "c"))
.feature(Word::mapParts, String::toUpperCase)
.feature(Word::partsList)
.containsExactly("A", "B", "C");
.toBe(Word("A", "B", "C"))
}

@Test
fun `allows to flatMap parts`() {
expect(Word("a", "b"))
.feature(Word::flatMapParts) { it -> sequenceOf("${it}1", "${it}2") }
.toBe(Word("a1", "a2", "b1", "b2"))
}

@Test
fun `allows to parse parts from a notation`() {
expect("these are words with UpperCamelCase")
.feature(String::fromNotation, NormalWords)
.feature(Word::partsFromNotation, UpperCamelCase)
.toBe(Word("these", "are", "words", "with", "upper", "camel", "case"))
}
}

0 comments on commit 231aef0

Please sign in to comment.