Skip to content

Commit

Permalink
Fix FieldOrigin parameters, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattking committed Jul 29, 2024
1 parent 0eedbc8 commit 2b9a606
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 20 deletions.
12 changes: 0 additions & 12 deletions src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,15 @@ package info.mking.k2zpl.builder

import info.mking.k2zpl.command.EndFormat
import info.mking.k2zpl.command.FieldData
import info.mking.k2zpl.command.FieldOrigin
import info.mking.k2zpl.command.FieldSeparator
import info.mking.k2zpl.command.GraphicField
import info.mking.k2zpl.command.LabelLength
import info.mking.k2zpl.command.PrintQuantity
import info.mking.k2zpl.command.StartFormat
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.ZplYesNo

/**
* Sets the origin for a field.
* @param x The x-coordinate of the field origin.
* @param y The y-coordinate of the field origin.
* @param alignment The alignment of the field (optional).
*/
fun ZplBuilder.fieldOrigin(x: Int, y: Int, alignment: ZplFieldOrientation? = null) {
command(FieldOrigin(x = x, y = y, alignment = alignment))
}

/**
* Draws a graphic field.
* @param format The format of the graphic field.
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/info/mking/k2zpl/builder/ZplBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package info.mking.k2zpl.builder
import info.mking.k2zpl.command.CustomCommand
import info.mking.k2zpl.command.Font
import info.mking.k2zpl.command.ZplCommand
import info.mking.k2zpl.command.fieldOrigin
import info.mking.k2zpl.command.font
import info.mking.k2zpl.command.options.ZplDpiSetting
import info.mking.k2zpl.command.options.ZplFieldOrientation
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/info/mking/k2zpl/command/BarCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package info.mking.k2zpl.command
import info.mking.k2zpl.builder.ZplBuilder
import info.mking.k2zpl.builder.command
import info.mking.k2zpl.builder.fieldData
import info.mking.k2zpl.builder.fieldOrigin
import info.mking.k2zpl.builder.fieldSeparator
import info.mking.k2zpl.builder.toZplYesNo
import info.mking.k2zpl.command.options.ZplBarcodeType
Expand Down
41 changes: 36 additions & 5 deletions src/main/kotlin/info/mking/k2zpl/command/FieldOrigin.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package info.mking.k2zpl.command

import info.mking.k2zpl.command.options.ZplFieldOrientation
import info.mking.k2zpl.builder.ZplBuilder
import info.mking.k2zpl.builder.command
import info.mking.k2zpl.command.options.ZplJustification

internal data class FieldOrigin(
val x: Int,
val y: Int,
val justification: ZplJustification
) : ZplCommand {
init {
require(x in 0..32000) { "x must be within 0 to 32000" }
require(y in 0..32000) { "y must be within 0 to 32000" }
}

internal data class FieldOrigin(val x: Int, val y: Int, val alignment: ZplFieldOrientation? = null) :
ZplCommand {
override val command: CharSequence = "^FO"
override val parameters: LinkedHashMap<CharSequence, Any?> = buildLinkedMap {
putAll(linkedMapOf("x" to x, "y" to y))
if (alignment != null) put("a", alignment.code)
put("x", x)
put("y", y)
put("j", justification)
}
}

/**
* Sets the origin for a field.
* @param x The x-coordinate of the field origin.
* @param y The y-coordinate of the field origin.
* @param justification The alignment of the field
*/
fun ZplBuilder.fieldOrigin(
x: Int,
y: Int,
justification: ZplJustification = ZplJustification.LEFT
) {
command(
FieldOrigin(
x = x,
y = y,
justification = justification
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package info.mking.k2zpl.command.options

enum class ZplJustification(private val value: Char) {
LEFT('0'),
RIGHT('1'),
AUTO('2');

override fun toString(): String {
return value.toString()
}
}
4 changes: 2 additions & 2 deletions src/test/kotlin/info/mking/k2zpl/command/BarCodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class BarCodeTest : DescribeSpec({
)
}
result shouldBe """
^FO10,10
^FO10,10,0
^B1N,N,10,7,Y
^FD1234567890
^FS
Expand All @@ -93,7 +93,7 @@ class BarCodeTest : DescribeSpec({
barcode(data = "1234567890", x = 10, y = 10, height = 10, lineThickness = 7)
}
result shouldBe """
^FO10,10
^FO10,10,0
^B1N,N,10,7,N
^FD1234567890
^FS
Expand Down
46 changes: 46 additions & 0 deletions src/test/kotlin/info/mking/k2zpl/command/FieldOriginTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package info.mking.k2zpl.command

import info.mking.k2zpl.command.options.ZplJustification
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 FieldOriginTest : DescribeSpec({
isolationMode = IsolationMode.InstancePerLeaf

val fieldOrigin = FieldOrigin(
x = 10,
y = 10,
justification = ZplJustification.LEFT
)

describe("FieldOrigin") {
it("outputs correct command") {
fieldOrigin.testBuildString() shouldBe "^FO10,10,0"
}
it("uses alignment parameter correctly") {
ZplJustification.entries.forEach {
fieldOrigin.copy(justification = it).testBuildString() shouldBe "^FO10,10,$it"
}
}
it("requires valid parameters") {
table(
headers("x", "y"),
row(-1, 0),
row(32001, 0),
row(0, -1),
row(0, 32001)
).forAll { x, y ->
shouldThrow<IllegalArgumentException> {
fieldOrigin.copy(x = x, y = y)
}
}
}
}
})

0 comments on commit 2b9a606

Please sign in to comment.