From 796b7642ea8605f496dfc8b832489f95231bc09f Mon Sep 17 00:00:00 2001 From: Matt King Date: Sun, 28 Jul 2024 16:46:43 +0100 Subject: [PATCH] Update PrintWidth to use correct parameters, add tests. --- .../info/mking/k2zpl/builder/Extensions.kt | 9 ---- .../info/mking/k2zpl/command/PrintWidth.kt | 14 +++++- .../mking/k2zpl/command/PrintWidthTest.kt | 44 +++++++++++++++++++ 3 files changed, 57 insertions(+), 10 deletions(-) create mode 100644 src/test/kotlin/info/mking/k2zpl/command/PrintWidthTest.kt diff --git a/src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt b/src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt index 3199784..1caf563 100644 --- a/src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt +++ b/src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt @@ -17,7 +17,6 @@ import info.mking.k2zpl.command.LabelShift import info.mking.k2zpl.command.MediaDarkness import info.mking.k2zpl.command.PrintQuantity import info.mking.k2zpl.command.PrintRate -import info.mking.k2zpl.command.PrintWidth import info.mking.k2zpl.command.StartFormat import info.mking.k2zpl.command.ZplCommand import info.mking.k2zpl.command.options.ZplFieldOrientation @@ -26,14 +25,6 @@ import info.mking.k2zpl.command.options.ZplPrintSpeed import info.mking.k2zpl.command.options.ZplTextAlignment import info.mking.k2zpl.command.options.ZplYesNo -/** - * Sets the print width of the label. - * @param width The width of the label. - */ -fun ZplBuilder.printWidth(width: Int) { - command(PrintWidth(width = width)) -} - /** * Sets the label home position. * @param x The x-coordinate of the label home. diff --git a/src/main/kotlin/info/mking/k2zpl/command/PrintWidth.kt b/src/main/kotlin/info/mking/k2zpl/command/PrintWidth.kt index 117c893..15e83a5 100644 --- a/src/main/kotlin/info/mking/k2zpl/command/PrintWidth.kt +++ b/src/main/kotlin/info/mking/k2zpl/command/PrintWidth.kt @@ -1,10 +1,22 @@ package info.mking.k2zpl.command +import info.mking.k2zpl.builder.ZplBuilder +import info.mking.k2zpl.builder.command + internal data class PrintWidth(val width: Int) : ZplCommand { init { - require(width in 1..32000) { "Width must be between 1 and 32000" } + require(width in 2 .. 32000) { "Width must be greater than 2. Value is also capped at width of the actual label." } } override val command: CharSequence = "^PW" override val parameters: LinkedHashMap = linkedMapOf("w" to width) +} + + +/** + * Sets the print width of the label. + * @param width The width of the label. + */ +fun ZplBuilder.printWidth(width: Int) { + command(PrintWidth(width = width)) } \ No newline at end of file diff --git a/src/test/kotlin/info/mking/k2zpl/command/PrintWidthTest.kt b/src/test/kotlin/info/mking/k2zpl/command/PrintWidthTest.kt new file mode 100644 index 0000000..58b84d2 --- /dev/null +++ b/src/test/kotlin/info/mking/k2zpl/command/PrintWidthTest.kt @@ -0,0 +1,44 @@ +package info.mking.k2zpl.command + +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 PrintWidthTest : DescribeSpec({ + isolationMode = IsolationMode.InstancePerLeaf + + val printWidth = PrintWidth( + width = 640 + ) + + describe("PrintWidth") { + it("outputs correct command") { + printWidth.testBuildString() shouldBe "^PW640" + } + it("requires valid parameters") { + table(headers("width"), + row(0), + row(32001) + ).forAll { + shouldThrow { + printWidth.copy(width = it) + } + } + } + } + describe("printWidth extension function") { + it("outputs the correct command") { + val result = k2zpl { + printWidth(640) + } + result shouldBe "^PW640\n" + } + } +}) \ No newline at end of file