-
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.
Fix FieldOrigin parameters, add tests
- Loading branch information
1 parent
0eedbc8
commit 2b9a606
Showing
7 changed files
with
96 additions
and
20 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
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,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 | ||
) | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/info/mking/k2zpl/command/options/ZplJustification.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,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() | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/test/kotlin/info/mking/k2zpl/command/FieldOriginTest.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,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) | ||
} | ||
} | ||
} | ||
} | ||
}) |