Skip to content

Commit

Permalink
[#14] Update barcode helper
Browse files Browse the repository at this point in the history
PR feedback:

- Make `BarCode` an `interface`
- Use `table` entries for tests
- Doc consistency fixes
  • Loading branch information
itsmattking committed Oct 5, 2024
1 parent ada2f97 commit 54ed234
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.sainsburys.k2zpl.command.barcode
import com.sainsburys.k2zpl.command.ZplCommand
import com.sainsburys.k2zpl.command.options.ZplFieldOrientation

internal abstract class BarCode : ZplCommand {
abstract val orientation: ZplFieldOrientation
abstract val height: Int
internal interface BarCode : ZplCommand {
val orientation: ZplFieldOrientation
val height: Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal data class BarCode128(
val lineAbove: ZplYesNo,
val checkDigit: ZplYesNo,
val mode: ZplBarCode128Mode
) : BarCode() {
) : BarCode {

init {
require(height in 1..32000) { "Height must be between 1 and 32000" }
Expand All @@ -41,19 +41,19 @@ internal data class BarCode128(
* @param data data encoded in the barcode
* @param x horizontal position
* @param y vertical position
* @param orientation The orientation of the barcode.
* @param height The height of the barcode.
* @param orientation The orientation of the barcode
* @param height the height of the barcode
* @param interpretationLine print interpretation line
* @param lineAbove print interpretation line above code
* @param checkDigit UCC check digit
* @param checkDigit whether to include a UCC check digit
* @param mode barcode mode
*/
fun ZplBuilder.barcode128(
data: String,
x: Int,
y: Int,
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
height: Int,
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
interpretationLine: Boolean = false,
lineAbove: Boolean = false,
checkDigit: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal data class BarCode39(
val checkDigit: ZplYesNo,
val line: ZplYesNo,
val lineAbove: ZplYesNo,
) : BarCode() {
) : BarCode {

init {
require(height in 1..32000) { "Height must be between 1 and 32000" }
Expand All @@ -38,19 +38,19 @@ internal data class BarCode39(
* @param data data encoded in the barcode
* @param x horizontal position
* @param y vertical position
* @param orientation The orientation of the barcode.
* @param checkDigit Mod-43 check digit
* @param height The height of the barcode.
* @param orientation the orientation of the barcode.
* @param checkDigit whether to include a Mod-43 check digit
* @param height the height of the barcode.
* @param interpretationLine print interpretation line
* @param lineAbove print interpretation line above code
*/
fun ZplBuilder.barcode39(
data: String,
x: Int,
y: Int,
height: Int,
orientation: ZplFieldOrientation = ZplFieldOrientation.NORMAL,
checkDigit: Boolean = false,
height: Int,
interpretationLine: Boolean = false,
lineAbove: Boolean = false
) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/com/sainsburys/k2zpl/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.sainsburys.k2zpl

import com.sainsburys.k2zpl.command.ZplCommand
import io.kotest.data.row
import kotlin.enums.EnumEntries

fun ZplCommand.testBuildString() = buildString { build(this) }

inline fun <reified T : Enum<T>> EnumEntries<T>.toRows() = map(::row)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sainsburys.k2zpl.command
import com.sainsburys.k2zpl.command.options.ZplTextAlignment
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand Down Expand Up @@ -50,7 +51,7 @@ class FieldBlockTest : DescribeSpec({
}
}
it("uses alignment parameter correctly") {
ZplTextAlignment.entries.forEach {
table(headers("alignment"), ZplTextAlignment.entries.toRows()).forAll {
fieldBlock.copy(alignment = it).testBuildString() shouldBe "^FB10,1,10,$it,0"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sainsburys.k2zpl.command
import com.sainsburys.k2zpl.command.options.ZplJustification
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand All @@ -26,7 +27,7 @@ class FieldOriginTest : DescribeSpec({
fieldOrigin.testBuildString() shouldBe "^FO10,10,0"
}
it("uses alignment parameter correctly") {
ZplJustification.entries.forEach {
table(headers("justification"), ZplJustification.entries.toRows()).forAll {
fieldOrigin.copy(justification = it).testBuildString() shouldBe "^FO10,10,$it"
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/com/sainsburys/k2zpl/command/FontTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplFont
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand All @@ -23,12 +24,12 @@ class FontTest : DescribeSpec({
font.testBuildString() shouldBe "^AAN,30,20"
}
it("correctly uses font parameter") {
ZplFont.entries.forEach {
table(headers("font"), ZplFont.entries.toRows()).forAll {
font.copy(font = it).testBuildString() shouldBe "^A${it}N,30,20"
}
}
it("correctly uses orientation parameter") {
ZplFieldOrientation.entries.forEach {
table(headers("orientation"), ZplFieldOrientation.entries.toRows()).forAll {
font.copy(orientation = it).testBuildString() shouldBe "^AA${it},30,20"
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/kotlin/com/sainsburys/k2zpl/command/MediaModeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import com.sainsburys.k2zpl.command.options.ZplMediaMode
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
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.table
import io.kotest.matchers.shouldBe

class MediaModeTest : DescribeSpec({
Expand All @@ -18,12 +22,12 @@ class MediaModeTest : DescribeSpec({
mediaMode.testBuildString() shouldBe "^MMC,N"
}
it("uses mediaMode parameter properly") {
ZplMediaMode.entries.forEach {
table(headers("mediaMode"), ZplMediaMode.entries.toRows()).forAll {
mediaMode.copy(mediaMode = it).testBuildString() shouldBe "^MM${it.value},N"
}
}
it("uses prePeelSelect parameter properly") {
ZplYesNo.entries.forEach {
table(headers("prePeelSelect"), ZplYesNo.entries.toRows()).forAll {
mediaMode.copy(prePeelSelect = it).testBuildString() shouldBe "^MMC,${it}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand Down Expand Up @@ -32,30 +33,33 @@ class BarCode128Test : DescribeSpec({
result shouldBe "^BCN,10,N,N,N,N"
}
it("uses orientation parameter properly") {
ZplFieldOrientation.entries.forEach {
table(
headers("orientation"),
ZplFieldOrientation.entries.map(::row)
).forAll {
subject.copy(orientation = it).testBuildString() shouldBe "^BC${it.code},10,N,N,N,N"
}
}
it("uses height parameter properly") {
subject.copy(height = 100).testBuildString() shouldBe "^BCN,100,N,N,N,N"
}
it("uses line parameter properly") {
ZplYesNo.entries.forEach {
table(headers("line"), ZplYesNo.entries.toRows()).forAll {
subject.copy(line = it).testBuildString() shouldBe "^BCN,10,${it},N,N,N"
}
}
it("uses lineAbove parameter properly") {
ZplYesNo.entries.forEach {
table(headers("lineAbove"), ZplYesNo.entries.toRows()).forAll {
subject.copy(lineAbove = it).testBuildString() shouldBe "^BCN,10,N,${it},N,N"
}
}
it("uses checkDigit parameter properly") {
ZplYesNo.entries.forEach {
table(headers("checkDigit"), ZplYesNo.entries.toRows()).forAll {
subject.copy(checkDigit = it).testBuildString() shouldBe "^BCN,10,N,N,${it},N"
}
}
it("uses mode parameter properly") {
ZplBarCode128Mode.entries.forEach {
table(headers("mode"), ZplBarCode128Mode.entries.toRows()).forAll {
subject.copy(mode = it).testBuildString() shouldBe "^BCN,10,N,N,N,${it}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sainsburys.k2zpl.command.options.ZplFieldOrientation
import com.sainsburys.k2zpl.command.options.ZplYesNo
import com.sainsburys.k2zpl.k2zpl
import com.sainsburys.k2zpl.testBuildString
import com.sainsburys.k2zpl.toRows
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.DescribeSpec
Expand Down Expand Up @@ -31,29 +32,29 @@ class BarCode39Test : DescribeSpec({
result shouldBe "^B3N,N,10,N,N"
}
it("uses orientation parameter properly") {
ZplFieldOrientation.entries.forEach {
table(headers("orientation"), ZplFieldOrientation.entries.toRows()).forAll {
subject.copy(orientation = it).testBuildString() shouldBe "^B3${it.code},N,10,N,N"
}
}
it("uses height parameter properly") {
subject.copy(height = 100).testBuildString() shouldBe "^B3N,N,100,N,N"
}
it("uses checkDigit parameter properly") {
ZplYesNo.entries.forEach {
table(headers("checkDigit"), ZplYesNo.entries.toRows()).forAll {
subject.copy(checkDigit = it).testBuildString() shouldBe "^B3N,${it},10,N,N"
}
}
it("uses line parameter properly") {
ZplYesNo.entries.forEach {
table(headers("line"), ZplYesNo.entries.toRows()).forAll {
subject.copy(line = it).testBuildString() shouldBe "^B3N,N,10,${it},N"
}
}
it("uses lineAbove parameter properly") {
ZplYesNo.entries.forEach {
table(headers("lineAbove"), ZplYesNo.entries.toRows()).forAll {
subject.copy(lineAbove = it).testBuildString() shouldBe "^B3N,N,10,N,${it}"
}
}
it("requires valid parameters") {
it("requires valid height parameters") {
table(
headers("height"),
row(32001),
Expand Down

0 comments on commit 54ed234

Please sign in to comment.