diff --git a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt index 17b330b..a38869d 100644 --- a/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt +++ b/EpcQrCode/src/commonMain/kotlin/net/codinux/banking/epcqrcode/EpcQrCodeGenerator.kt @@ -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() diff --git a/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt b/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt index 074d817..80c47ac 100644 --- a/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt +++ b/EpcQrCode/src/commonTest/kotlin/net.codinux.banking.epcqrcode/EpcQrCodeGeneratorTest.kt @@ -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 includingReferenceText() { + val referenceText = "Her mit der Knete" + val config = basicDataConfig(referenceText = referenceText) + + 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.PNG)) + val result = underTest.generateEpcQrCode(basicDataConfig(imageFormat = ImageFormat.PNG)) - // don't know why, in JavaScript it's 1000643, on all other platforms 10446 bytes - assertTrue(listOf(10446, 1000643).contains(result.bytes.size)) + // 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) } \ No newline at end of file