-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FieldBlock test, correct parameter restrictions.
- Loading branch information
1 parent
30de0ed
commit ec2997b
Showing
4 changed files
with
120 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,55 @@ | ||
package info.mking.k2zpl.command | ||
|
||
import info.mking.k2zpl.builder.ZplBuilder | ||
import info.mking.k2zpl.builder.command | ||
import info.mking.k2zpl.command.options.ZplTextAlignment | ||
|
||
internal data class FieldBlock( | ||
val width: Int, val lines: Int, val lineSpacing: Int, val alignment: ZplTextAlignment, val hangingIndent: Int | ||
val width: Int, | ||
val maxLines: Int, | ||
val lineSpacing: Int, | ||
val alignment: ZplTextAlignment, | ||
val hangingIndent: Int | ||
) : ZplCommand { | ||
init { | ||
require(width in 1..32000) { "Width must be between 1 and 32000" } | ||
require(lines in 1..999) { "Lines must be between 1 and 999" } | ||
require(lineSpacing in 0..32000) { "Line spacing must be between 0 and 32000" } | ||
require(hangingIndent in 0..32000) { "Hanging indent must be between 0 and 32000" } | ||
require(maxLines in 1..9999) { "Lines must be between 1 and 9999" } | ||
require(lineSpacing in -9999..9999) { "Line spacing must be between -9999 and 9999" } | ||
require(hangingIndent in 0..9999) { "Hanging indent must be between 0 and 9999" } | ||
} | ||
|
||
override val command: CharSequence = "^FB" | ||
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf( | ||
"w" to width, "l" to lines, "s" to lineSpacing, "a" to alignment.code, "h" to hangingIndent | ||
"w" to width, | ||
"l" to maxLines, | ||
"s" to lineSpacing, | ||
"a" to alignment.code, | ||
"h" to hangingIndent | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Formats a text block. | ||
* @param width The width of the text block. | ||
* @param maxLines The number of lines in the text block. | ||
* @param lineSpacing The space between lines. | ||
* @param alignment The text alignment within the block. | ||
* @param hangingIndent The hanging indent for the block. | ||
*/ | ||
fun ZplBuilder.fieldBlock( | ||
width: Int, | ||
maxLines: Int = 1, | ||
lineSpacing: Int = 0, | ||
alignment: ZplTextAlignment = ZplTextAlignment.LEFT, | ||
hangingIndent: Int = 0 | ||
) { | ||
command( | ||
FieldBlock( | ||
width = width, | ||
maxLines = maxLines, | ||
lineSpacing = lineSpacing, | ||
alignment = alignment, | ||
hangingIndent = hangingIndent | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/kotlin/info/mking/k2zpl/command/FieldBlockTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package info.mking.k2zpl.command | ||
|
||
import info.mking.k2zpl.command.options.ZplTextAlignment | ||
import info.mking.k2zpl.k2zpl | ||
import info.mking.k2zpl.testBuildString | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.IsolationMode | ||
import io.kotest.core.spec.style.DescribeSpec | ||
import io.kotest.data.forAll | ||
import io.kotest.data.headers | ||
import io.kotest.data.row | ||
import io.kotest.data.table | ||
import io.kotest.matchers.shouldBe | ||
|
||
class FieldBlockTest : DescribeSpec({ | ||
isolationMode = IsolationMode.InstancePerLeaf | ||
|
||
val fieldBlock = FieldBlock( | ||
width = 10, | ||
maxLines = 1, | ||
lineSpacing = 10, | ||
alignment = ZplTextAlignment.LEFT, | ||
hangingIndent = 0 | ||
) | ||
|
||
describe("FieldBlock") { | ||
it("outputs correct command") { | ||
fieldBlock.testBuildString() shouldBe "^FB10,1,10,L,0" | ||
} | ||
it("requires valid parameters") { | ||
table( | ||
headers("width", "maxLines", "lineSpacing", "hangingIndent"), | ||
row(0, 1, 1, 0), | ||
row(32001, 1, 1, 0), | ||
row(1, 0, 1, 0), | ||
row(1, 10000, 1, 0), | ||
row(1, 1, -10000, 0), | ||
row(1, 1, 10000, 0), | ||
row(1, 1, 1, -1), | ||
row(1, 1, 1, 10000) | ||
).forAll { width, maxLines, lineSpacing, hangingIndent -> | ||
shouldThrow<IllegalArgumentException> { | ||
fieldBlock.copy( | ||
width = width, | ||
maxLines = maxLines, | ||
lineSpacing = lineSpacing, | ||
hangingIndent = hangingIndent | ||
) | ||
} | ||
} | ||
} | ||
it("uses alignment parameter correctly") { | ||
ZplTextAlignment.entries.forEach { | ||
fieldBlock.copy(alignment = it).testBuildString() shouldBe "^FB10,1,10,$it,0" | ||
} | ||
} | ||
} | ||
describe("fieldBlock extension function") { | ||
it("outputs correct command") { | ||
val result = k2zpl { | ||
fieldBlock(width = 100, maxLines = 10, lineSpacing = 10, alignment = ZplTextAlignment.CENTER, hangingIndent = 10) | ||
} | ||
result shouldBe "^FB100,10,10,C,10\n" | ||
} | ||
it("outputs correct command with default parameters") { | ||
val result = k2zpl { | ||
fieldBlock(width = 100) | ||
} | ||
result shouldBe "^FB100,1,0,L,0\n" | ||
} | ||
|
||
} | ||
}) |