Skip to content

Commit

Permalink
Update Font command and move extension function, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattking committed Jul 28, 2024
1 parent 8ed0605 commit 53fdf77
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 16 deletions.
15 changes: 0 additions & 15 deletions src/main/kotlin/info/mking/k2zpl/builder/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import info.mking.k2zpl.command.FieldData
import info.mking.k2zpl.command.FieldOrientation
import info.mking.k2zpl.command.FieldOrigin
import info.mking.k2zpl.command.FieldSeparator
import info.mking.k2zpl.command.Font
import info.mking.k2zpl.command.FontAt
import info.mking.k2zpl.command.GraphicField
import info.mking.k2zpl.command.LabelLength
Expand All @@ -23,20 +22,6 @@ import info.mking.k2zpl.command.options.ZplPrintSpeed
import info.mking.k2zpl.command.options.ZplTextAlignment
import info.mking.k2zpl.command.options.ZplYesNo

/**
* Sets the font for text fields.
* @param font The font to use.
* @param height The height of the font.
* @param width The width of the font.
*/
fun ZplBuilder.font(font: ZplFont, height: Int, width: Int) {
command(Font(
font = font,
height = height,
width = width
))
}

/**
* Sets the font with additional path parameter.
* @param orientation The orientation of the font.
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.font
import info.mking.k2zpl.command.options.ZplDpiSetting
import info.mking.k2zpl.command.options.ZplFont
import kotlin.math.roundToInt
Expand Down
22 changes: 21 additions & 1 deletion src/main/kotlin/info/mking/k2zpl/command/Font.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package info.mking.k2zpl.command

import info.mking.k2zpl.builder.ZplBuilder
import info.mking.k2zpl.builder.command
import info.mking.k2zpl.command.options.ZplFont

internal data class Font(val font: ZplFont, val height: Int, val width: Int) : ZplCommand {
Expand All @@ -9,5 +11,23 @@ internal data class Font(val font: ZplFont, val height: Int, val width: Int) : Z
}

override val command: CharSequence = "^A"
override val parameters: LinkedHashMap<CharSequence, Any?> = linkedMapOf("f" to font.code, "h" to height, "w" to width)
override val parameters: LinkedHashMap<CharSequence, Any?> =
linkedMapOf("f" to font.code, "h" to height, "w" to width)
}


/**
* Sets the font for text fields.
* @param font The font to use.
* @param height The height of the font.
* @param width The width of the font.
*/
fun ZplBuilder.font(font: ZplFont, height: Int, width: Int) {
command(
Font(
font = font,
height = height,
width = width
)
)
}
4 changes: 4 additions & 0 deletions src/main/kotlin/info/mking/k2zpl/command/options/ZplFont.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ enum class ZplFont(val code: Char) {
G('G'),
H('H'),
GS('0'); // Graphic Symbol font

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

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

val font = Font(font = ZplFont.A, height = 30, width = 20)

describe("Font") {
it("outputs the correct command") {
font.testBuildString() shouldBe "^AA,30,20"
}
it("correctly uses font parameter") {
ZplFont.entries.forEach {
font.copy(font = it).testBuildString() shouldBe "^A${it},30,20"
}
}
it("requires valid parameters") {
table(headers("width", "height"),
row(9, 10),
row(32001, 10),
row(10, 9),
row(10, 32001)
).forAll { width, height ->
shouldThrow<IllegalArgumentException> {
font.copy(width = width, height = height)
}
}
}
}
describe("font extension function") {
it("outputs the correct command") {
val result = k2zpl {
font(font = ZplFont.C, height = 20, width = 15)
}
result shouldBe "^AC,20,15\n"
}
}
})

0 comments on commit 53fdf77

Please sign in to comment.