Fixed generating the correct amount of lines according to specification (that is no empty lines after 10th line and no line separator after 12th line)

This commit is contained in:
dankito 2024-10-03 19:02:18 +02:00
parent 0d32afda75
commit 3c129a9e46
2 changed files with 75 additions and 12 deletions

View File

@ -65,14 +65,23 @@ open class EpcQrCodeGenerator {
epcQrCodeBuilder.appendLine(epcQrCode.iban)
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.amount?.let { (epcQrCode.currencyCode ?: EpcQrCodeValues.CurrencyCodeDefaultValue) + it })) // TODO: ensure that amount is converted to the correct format
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.purposeCode))
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.remittanceReference))
if (epcQrCode.remittanceText != null || epcQrCode.originatorInformation != null) {
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.remittanceText))
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.remittanceReference))
} else {
epcQrCodeBuilder.append(mapNullable(epcQrCode.remittanceReference))
}
if (epcQrCode.remittanceText != null || epcQrCode.originatorInformation != null) {
if (epcQrCode.originatorInformation != null) {
epcQrCodeBuilder.appendLine(mapNullable(epcQrCode.remittanceText))
} else {
epcQrCodeBuilder.append(mapNullable(epcQrCode.remittanceText))
}
}
if (epcQrCode.originatorInformation != null) {
epcQrCodeBuilder.appendLine(epcQrCode.originatorInformation)
epcQrCodeBuilder.append(epcQrCode.originatorInformation)
}
return epcQrCodeBuilder.toString()

View File

@ -11,10 +11,12 @@ class EpcQrCodeGeneratorTest {
private const val ReceiverIban = "DE01234567890123456789"
}
private val underTest = EpcQrCodeGenerator()
@Test
fun basicDataOnly() {
fun mandatoryDataOnly() {
val config = basicDataConfig()
val result = underTest.generateAsString(config)
@ -25,36 +27,88 @@ class EpcQrCodeGeneratorTest {
1
SCT
Liebe
DE01234567890123456789
$ReceiverName
$ReceiverIban
""".trimIndent())
assertEquals(10, result.lines().size) // if no reference text or note to user is set, then QR code may only contain the minimum of 10 lines and no empty lines afterwards
}
@Test
fun png() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.PNG))
fun includingReferenceText() {
val referenceText = "Her mit der Knete"
val config = basicDataConfig(referenceText = referenceText)
// don't know why, in JavaScript it's 1000643, on all other platforms 10446 bytes
assertTrue(listOf(10446, 1000643).contains(result.bytes.size))
val result = underTest.generateAsString(config)
assertEquals(actual = result, expected = """
BCD
002
1
SCT
$ReceiverName
$ReceiverIban
$referenceText
""".trimIndent())
assertEquals(11, result.lines().size) // no note to user -> no 12th line may be contained in generated QR code
}
@Test
fun includingNoteToUser() {
val noteToUser = "Mach uns reich, Baby"
val config = basicDataConfig(noteToUser = noteToUser)
val result = underTest.generateAsString(config)
assertEquals(actual = result, expected = """
BCD
002
1
SCT
$ReceiverName
$ReceiverIban
$noteToUser
""".trimIndent())
assertEquals(12, result.lines().size) // note to user set -> contains the maximum of 12 lines but no line separator at end of 12th line
}
@Test
fun png() {
val result = underTest.generateEpcQrCode(basicDataConfig(imageFormat = ImageFormat.PNG))
// don't know why, in JavaScript it's 1000643, on all other platforms 10459 bytes
assertTrue(listOf(10459, 1000643).contains(result.bytes.size))
}
@Test
fun bmp() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.BMP))
val result = underTest.generateEpcQrCode(basicDataConfig(imageFormat = ImageFormat.BMP))
assertEquals(1000054, result.bytes.size)
}
@Test
fun tga() {
val result = underTest.generateEpcQrCode(basicDataConfig(ImageFormat.TGA))
val result = underTest.generateEpcQrCode(basicDataConfig(imageFormat = ImageFormat.TGA))
assertEquals(1000018, result.bytes.size)
}
private fun basicDataConfig(imageFormat: ImageFormat = ImageFormat.PNG) = EpcQrCodeConfig(ReceiverName, ReceiverIban, imageFormat = imageFormat)
private fun basicDataConfig(referenceText: String? = null, noteToUser: String? = null, imageFormat: ImageFormat = ImageFormat.PNG) =
EpcQrCodeConfig(ReceiverName, ReceiverIban, reference = referenceText, noteToUser = noteToUser, imageFormat = imageFormat)
}