Skip to content

Commit

Permalink
Merge branch 'increase-coverage' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_pollard committed Nov 7, 2023
2 parents 88b249e + 6026f9b commit 9e9d56a
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.github.benpollarduk.ktvn.logic.listeners.Moves
@Suppress("TooManyFunctions")
public class Layout private constructor(setup: (Layout) -> Unit) {
private val mutablePositions: MutableList<CharacterPosition> = mutableListOf()

private var moves: Moves = object : Moves {
override fun invoke(
character: Character,
Expand All @@ -27,12 +28,19 @@ public class Layout private constructor(setup: (Layout) -> Unit) {
// nothing
}
}

private var moveAcknowledgement: Acknowledges = object : Acknowledges {
override fun waitFor() {
// nothing
}
}

/**
* Get the number of characters in this layout.
*/
public val characters: Int
get() = mutablePositions.size

init {
setup(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public class Chapter private constructor(setup: (Chapter) -> Unit) {
public var indexOfCurrentScene: Int = 0
private set

/**
* Get the number of the [Scene] in this [Chapter].
*/
public val numberOfScenes: Int
get() = scenes.size

/**
* Get the current [Scene].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class Scene private constructor(setup: (Scene) -> Unit) {
public var name: String = "Scene"
private set

/**
* Get the number of [Step] that make up this [Scene].
*/
public val numberOfSteps: Int
get() = content.size

init {
setup(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ import com.github.benpollarduk.ktvn.logic.Flags
public class Story private constructor(setup: (Story) -> Unit) {
private val chapters: MutableList<Chapter> = mutableListOf()

/**
* Get the name of this [Chapter].
*/
public var name: String = "Story"
private set

/**
* Get the index of the current [Chapter].
*/
public var indexOfCurrentChapter: Int = 0
private set

/**
* Get the number of the [Chapter] in this [Story].
*/
public val numberOfChapters: Int
get() = chapters.size

/**
* Get the flags
*/
Expand Down Expand Up @@ -70,6 +82,13 @@ public class Story private constructor(setup: (Story) -> Unit) {
return 0
}

/**
* Set the [name] of this [Story].
*/
public infix fun name(name: String) {
this.name = name
}

/**
* Add a [chapter] to this [Story].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,75 @@ class CharacterTest {
// Then
Assertions.assertEquals(happy, character.emotion)
}

@Test
fun `given a character when says then speaks is called`() {
// Given
var called = false
val speaks = object : Speaks {
override fun invoke(character: Character, line: String, acknowledgement: Acknowledges) {
called = true
}
}
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
character.says("")

// Then
Assertions.assertTrue(called)
}

@Test
fun `given a character when ask then asks is called`() {
// Given
var called = false
val asks = object : Asks {
override fun invoke(character: Character, question: Question, answers: Answers): Answer {
called = true
return Answer.answer { }
}

override fun invoke(narrator: Narrator, question: Question, answers: Answers): Answer {
return Answer.answer { }
}
}
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
character.asks(Question.question { })

// Then
Assertions.assertTrue(called)
}

@Test
fun `given a character when looks then emotes is called`() {
// Given
var called = false
val emotes = object : Emotes {
override fun invoke(character: Character, emotion: Emotion, acknowledgement: Acknowledges) {
called = true
}
}
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
character.looks(happy)

// Then
Assertions.assertTrue(called)
}

@Test
fun `given a character when looks happy then emotion is happy`() {
// Given
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
character.looks(happy)

// Then
Assertions.assertEquals(happy, character.emotion)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.github.benpollarduk.ktvn.characters

import com.github.benpollarduk.ktvn.logic.Answer
import com.github.benpollarduk.ktvn.logic.Answer.Companion.answer
import com.github.benpollarduk.ktvn.logic.Question
import com.github.benpollarduk.ktvn.logic.Question.Companion.question
import com.github.benpollarduk.ktvn.logic.listeners.Acknowledges
import com.github.benpollarduk.ktvn.logic.listeners.Answers
import com.github.benpollarduk.ktvn.logic.listeners.Asks
import com.github.benpollarduk.ktvn.logic.listeners.Narrates
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class NarratorTest {
private val narrates = object : Narrates {
override fun invoke(line: String, acknowledgement: Acknowledges) {
// nothing
}
}

private val asks = object : Asks {
override fun invoke(character: Character, question: Question, answers: Answers): Answer {
return question.answers.first()
}

override fun invoke(narrator: Narrator, question: Question, answers: Answers): Answer {
return question.answers.first()
}
}

private val acknowledges = object : Acknowledges {
override fun waitFor() {
// nothing
}
}

private val answers = object : Answers {
override fun waitFor(question: Question): Answer {
return question.answers.first()
}
}

@Test
fun `given a narrator when narrate then narrates is called`() {
// Given
var called = false
val narrates = object : Narrates {
override fun invoke(line: String, acknowledgement: Acknowledges) {
called = true
}
}
val narrator = Narrator(narrates, asks, acknowledges, answers)

// When
narrator.narrates("")

// Then
Assertions.assertTrue(called)
}

@Test
fun `given a narrator when ask then asks is called`() {
// Given
var called = false
val asks = object : Asks {
override fun invoke(character: Character, question: Question, answers: Answers): Answer {
return answer { }
}

override fun invoke(narrator: Narrator, question: Question, answers: Answers): Answer {
called = true
return answer { }
}
}
val narrator = Narrator(narrates, asks, acknowledges, answers)

// When
narrator.asks(question { })

// Then
Assertions.assertTrue(called)
}
}
143 changes: 143 additions & 0 deletions ktvn/src/test/kotlin/com/github/benpollarduk/ktvn/layout/LayoutTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package com.github.benpollarduk.ktvn.layout

import com.github.benpollarduk.ktvn.characters.Character
import com.github.benpollarduk.ktvn.characters.Emotion
import com.github.benpollarduk.ktvn.characters.Narrator
import com.github.benpollarduk.ktvn.layout.Positions.left
import com.github.benpollarduk.ktvn.layout.Positions.right
import com.github.benpollarduk.ktvn.logic.Answer
import com.github.benpollarduk.ktvn.logic.Question
import com.github.benpollarduk.ktvn.logic.listeners.Acknowledges
import com.github.benpollarduk.ktvn.logic.listeners.Answers
import com.github.benpollarduk.ktvn.logic.listeners.Asks
import com.github.benpollarduk.ktvn.logic.listeners.Emotes
import com.github.benpollarduk.ktvn.logic.listeners.Moves
import com.github.benpollarduk.ktvn.logic.listeners.Speaks
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class LayoutTest {
private val speaks = object : Speaks {
override fun invoke(character: Character, line: String, acknowledgement: Acknowledges) {
// nothing
}
}

private val emotes = object : Emotes {
override fun invoke(character: Character, emotion: Emotion, acknowledgement: Acknowledges) {
// nothing
}
}

private val asks = object : Asks {
override fun invoke(character: Character, question: Question, answers: Answers): Answer {
return question.answers.first()
}

override fun invoke(narrator: Narrator, question: Question, answers: Answers): Answer {
return question.answers.first()
}
}

private val acknowledges = object : Acknowledges {
override fun waitFor() {
// nothing
}
}

private val answers = object : Answers {
override fun waitFor(question: Question): Answer {
return question.answers.first()
}
}

@Test
fun `given layout when add character then one character`() {
// Given
val layout = Layout.createLayout { }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
layout.add(character, left)

// Then
Assertions.assertEquals(1, layout.characters)
}

@Test
fun `given layout when add left of then one character`() {
// Given
val layout = Layout.createLayout { }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
layout.addLeftOf(character)

// Then
Assertions.assertEquals(1, layout.characters)
}

@Test
fun `given layout when add right of then one character`() {
// Given
val layout = Layout.createLayout { }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
layout.addRightOf(character)

// Then
Assertions.assertEquals(1, layout.characters)
}

@Test
fun `given layout when add above then one character`() {
// Given
val layout = Layout.createLayout { }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
layout.addAbove(character)

// Then
Assertions.assertEquals(1, layout.characters)
}

@Test
fun `given layout when add below then one character`() {
// Given
val layout = Layout.createLayout { }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)

// When
layout.addBelow(character)

// Then
Assertions.assertEquals(1, layout.characters)
}

@Test
fun `given layout when move character then moves is called`() {
// Given
var called = false
val moves = object : Moves {
override fun invoke(
character: Character,
fromPosition: Position,
toPosition: Position,
acknowledgement: Acknowledges
) {
called = true
}
}
val layout = Layout.createLayout { it setMoves moves }
val character = Character("", speaks, emotes, asks, acknowledges, acknowledges, answers)
layout.add(character, left)

// When
layout.move(character, right)

// Then
Assertions.assertTrue(called)
}
}
Loading

0 comments on commit 9e9d56a

Please sign in to comment.