Skip to content

Commit

Permalink
Add FieldBlock test, correct parameter restrictions.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattking committed Jul 29, 2024
1 parent 30de0ed commit ec2997b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 27 deletions.
20 changes: 0 additions & 20 deletions src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package info.mking.k2zpl.builder

import info.mking.k2zpl.command.EndFormat
import info.mking.k2zpl.command.FieldBlock
import info.mking.k2zpl.command.FieldData
import info.mking.k2zpl.command.FieldOrientation
import info.mking.k2zpl.command.FieldOrigin
Expand All @@ -18,27 +17,8 @@ import info.mking.k2zpl.command.ZplCommand
import info.mking.k2zpl.command.options.ZplFieldOrientation
import info.mking.k2zpl.command.options.ZplFont
import info.mking.k2zpl.command.options.ZplPrintSpeed
import info.mking.k2zpl.command.options.ZplTextAlignment
import info.mking.k2zpl.command.options.ZplYesNo

/**
* Formats a text block.
* @param width The width of the text block.
* @param lines 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, lines: Int, lineSpacing: Int, alignment: ZplTextAlignment, hangingIndent: Int) {
command(FieldBlock(
width = width,
lines = lines,
lineSpacing = lineSpacing,
alignment = alignment,
hangingIndent = hangingIndent
))
}

/**
* Sets the field orientation for text fields.
* @param orientation The orientation of the field.
Expand Down
48 changes: 42 additions & 6 deletions src/main/kotlin/info/mking/k2zpl/command/FieldBlock.kt
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
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ enum class ZplTextAlignment(val code: Char) {
LEFT('L'),
CENTER('C'),
RIGHT('R'),
JUSTIFY('J')
JUSTIFY('J');

override fun toString(): String {
return code.toString()
}
}
73 changes: 73 additions & 0 deletions src/test/kotlin/info/mking/k2zpl/command/FieldBlockTest.kt
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"
}

}
})

0 comments on commit ec2997b

Please sign in to comment.