Skip to content

Commit

Permalink
Update BarCode
Browse files Browse the repository at this point in the history
- Add ZplYesNo to handle translating booleans to Y/N
- Add BarCodeTest
  • Loading branch information
itsmattking committed Jul 28, 2024
1 parent 0ff69a6 commit cf57069
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 34 deletions.
31 changes: 6 additions & 25 deletions src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package info.mking.k2zpl.builder

import info.mking.k2zpl.command.BarCode
import info.mking.k2zpl.command.EndFormat
import info.mking.k2zpl.command.FieldBlock
import info.mking.k2zpl.command.FieldData
Expand All @@ -28,6 +27,7 @@ import info.mking.k2zpl.command.options.ZplMediaMode
import info.mking.k2zpl.command.options.ZplPreprintedLabelHandling
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.
Expand Down Expand Up @@ -102,30 +102,6 @@ fun ZplBuilder.fieldBlock(width: Int, lines: Int, lineSpacing: Int, alignment: Z
))
}

/**
* Creates a Code 39 barcode.
* @param orientation The orientation of the barcode.
* @param checkDigit Whether to include a check digit.
* @param height The height of the barcode.
* @param line The line thickness of the barcode.
* @param lineAbove Whether to include a line above the barcode.
*/
fun ZplBuilder.code39Barcode(
orientation: ZplFieldOrientation,
checkDigit: Boolean,
height: Int,
line: Int,
lineAbove: Boolean
) {
command(BarCode(
orientation = orientation,
checkDigit = checkDigit,
height = height,
line = line,
lineAbove = lineAbove
))
}

/**
* Sets the field orientation for text fields.
* @param orientation The orientation of the field.
Expand Down Expand Up @@ -270,4 +246,9 @@ fun ZplBuilder.field(x: Int = 0, y: Int = 0, data: String) {
*/
fun ZplBuilder.field(x: Int, y: Int, font: ZplFont, fontHeight: Int, fontWidth: Int, data: String) {
addField(x, y, font, fontHeight, fontWidth, data)
}

fun Boolean.toZplYesNo(): ZplYesNo = when(this) {
true -> ZplYesNo.YES
else -> ZplYesNo.NO
}
45 changes: 38 additions & 7 deletions src/main/kotlin/info/mking/k2zpl/command/BarCode.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package info.mking.k2zpl.command

import info.mking.k2zpl.builder.ZplBuilder
import info.mking.k2zpl.builder.command
import info.mking.k2zpl.builder.toZplYesNo
import info.mking.k2zpl.command.options.ZplBarcodeType
import info.mking.k2zpl.command.options.ZplFieldOrientation
import info.mking.k2zpl.command.options.ZplYesNo

internal data class BarCode(
val type: BarcodeType = BarcodeType.CODE_39,
val orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
val checkDigit: Boolean,
val type: ZplBarcodeType,
val orientation: ZplFieldOrientation,
val checkDigit: ZplYesNo,
val height: Int,
val line: Int,
val lineAbove: Boolean
val lineAbove: ZplYesNo
) : ZplCommand {
init {
require(height in 1..32000) { "Height must be between 1 and 32000" }
Expand All @@ -23,8 +28,34 @@ internal data class BarCode(
"l" to line,
"la" to lineAbove.toString()
)
}

enum class BarcodeType {
CODE_39
}
/**
* Creates a Code 39 barcode.
* @param barcodeType Barcode type
* @param orientation The orientation of the barcode.
* @param checkDigit Whether to include a check digit.
* @param height The height of the barcode.
* @param line The line thickness of the barcode.
* @param lineAbove Whether to include a line above the barcode.
*/
fun ZplBuilder.barcode(
barcodeType: ZplBarcodeType = ZplBarcodeType.CODE_39,
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
checkDigit: Boolean,
height: Int,
line: Int,
lineAbove: Boolean
) {
command(
BarCode(
type = barcodeType,
orientation = orientation,
checkDigit = checkDigit.toZplYesNo(),
height = height,
line = line,
lineAbove = lineAbove.toZplYesNo()
)
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package info.mking.k2zpl.command.options

enum class ZplBarcodeType {
CODE_39
}
10 changes: 10 additions & 0 deletions src/main/kotlin/info/mking/k2zpl/command/options/ZplYesNo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package info.mking.k2zpl.command.options

enum class ZplYesNo(val value: String) {
YES("Y"),
NO("N");

override fun toString(): String {
return value
}
}
76 changes: 76 additions & 0 deletions src/test/kotlin/info/mking/k2zpl/command/BarCodeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package info.mking.k2zpl.command

import info.mking.k2zpl.command.options.ZplBarcodeType
import info.mking.k2zpl.command.options.ZplFieldOrientation
import info.mking.k2zpl.command.options.ZplYesNo
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 BarCodeTest : DescribeSpec({

isolationMode = IsolationMode.InstancePerLeaf

val barCode = BarCode(
type = ZplBarcodeType.CODE_39,
orientation = ZplFieldOrientation.NORMAL,
checkDigit = ZplYesNo.NO,
height = 10,
line = 7,
lineAbove = ZplYesNo.YES
)

describe("Barcode") {
it("outputs correct command") {
val result = barCode.testBuildString()
result shouldBe "^B1N,N,10,7,Y"
}
it("uses orientation parameter properly") {
ZplFieldOrientation.entries.forEach {
barCode.copy(orientation = it).testBuildString() shouldBe "^B1${it.code},N,10,7,Y"
}
}
it("uses checkDigit parameter properly") {
ZplYesNo.entries.forEach {
barCode.copy(checkDigit = it).testBuildString() shouldBe "^B1N,${it},10,7,Y"
}
}
it("uses lineAbove parameter properly") {
ZplYesNo.entries.forEach {
barCode.copy(lineAbove = it).testBuildString() shouldBe "^B1N,N,10,7,${it}"
}
}
it("requires valid parameters") {
table(
headers("height", "line"),
row(32001, 1),
row(0, 1),
row(1, 0),
row(1, 8)
).forAll { height, line ->
shouldThrow<IllegalArgumentException> {
barCode.copy(
height = height,
line = line,
)
}
}
}

}
describe("barcode extension") {
it("outputs the correct command") {
val result = k2zpl {
barcode(checkDigit = false, height = 10, line = 7, lineAbove = true)
}
result shouldBe "^B1N,N,10,7,Y\n"
}
}
})
4 changes: 2 additions & 2 deletions src/test/kotlin/info/mking/k2zpl/command/GraphicBoxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class GraphicBoxTest : DescribeSpec({
}
}
}
describe("graphicBox helper") {
describe("graphicBox extension") {
it("has correct minimum argument output") {
val result = k2zpl {
graphicBox(100, 50)
Expand All @@ -87,7 +87,7 @@ class GraphicBoxTest : DescribeSpec({
result shouldBe "^GB100,50,10,W,3\n"
}
}
describe("line helper") {
describe("line extension") {
it("has correct minimum argument output") {
val result = k2zpl {
line(1000, 1)
Expand Down

0 comments on commit cf57069

Please sign in to comment.