From 2bb413e3e8626681865b53c8dbe4137bd99e9abb Mon Sep 17 00:00:00 2001 From: Markus Fisch Date: Tue, 7 Jun 2022 20:43:15 +0200 Subject: [PATCH] Add tests for MATMSG format --- .../binaryeye/actions/mail/MatMsgAction.kt | 30 +++++++------ .../binaryeye/actions/mail/MatMsgTest.kt | 43 +++++++++++++++++++ .../binaryeye/actions/vtype/VTypeTest.kt | 1 - .../android/binaryeye/actions/web/WebTest.kt | 1 - 4 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgTest.kt diff --git a/app/src/main/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgAction.kt b/app/src/main/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgAction.kt index 560d8d53..2b96e96e 100644 --- a/app/src/main/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgAction.kt +++ b/app/src/main/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgAction.kt @@ -19,31 +19,37 @@ object MatMsgAction : IntentAction() { context: Context, data: ByteArray ): Intent? { - val encoded = String(data) - // Allow arbitrary order. - val to = encoded.extractFirst("""[:;]TO:([\w.%@+-]+);""") - val sub = encoded.extractFirst("""[:;]SUB:([^;]+);""") - val body = encoded.extractFirst("""[:;]BODY:([^;]+);""") + val mm = MatMsg(String(data)) // Allow incomplete but not completely missing data. - if (to == null && sub == null && body == null) { + if (mm.isEmpty()) { return null } return Intent( Intent.ACTION_SENDTO, Uri.parse("mailto:") ).apply { - to?.let { - putExtra(Intent.EXTRA_EMAIL, arrayOf(to)) + mm.to?.let { + putExtra(Intent.EXTRA_EMAIL, arrayOf(mm.to)) } - sub?.let { - putExtra(Intent.EXTRA_SUBJECT, sub) + mm.sub?.let { + putExtra(Intent.EXTRA_SUBJECT, mm.sub) } - body?.let { - putExtra(Intent.EXTRA_TEXT, body) + mm.body?.let { + putExtra(Intent.EXTRA_TEXT, mm.body) } } } } +// Public so this can be tested. +class MatMsg(encoded: String) { + // Allow arbitrary order. + val to = encoded.extractFirst("""[:;]TO:([\w.%@+-]+);""") + val sub = encoded.extractFirst("""[:;]SUB:([^;]+);""") + val body = encoded.extractFirst("""[:;]BODY:([^;]+);""") + + fun isEmpty() = to == null && sub == null && body == null +} + private fun String.extractFirst(regex: String) = regex.toRegex().find(this)?.groupValues?.get(1) diff --git a/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgTest.kt b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgTest.kt new file mode 100644 index 00000000..5fbcd4e2 --- /dev/null +++ b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/mail/MatMsgTest.kt @@ -0,0 +1,43 @@ +package de.markusfisch.android.binaryeye.actions.mail + +import junit.framework.TestCase.assertEquals +import org.junit.Test + +class MatMsgTest { + @Test + fun to() { + val mm = MatMsg( + "MATMSG:TO:someone@example.org;;" + ) + assertEquals(mm.to, "someone@example.org") + } + + @Test + fun toSub() { + val mm = MatMsg( + "MATMSG:TO:someone@example.org;SUB:Stuff;;" + ) + assertEquals(mm.to, "someone@example.org") + assertEquals(mm.sub, "Stuff") + } + + @Test + fun toSubBody() { + val mm = MatMsg( + "MATMSG:TO:someone@example.org;SUB:Stuff;BODY:This is some text;;" + ) + assertEquals(mm.to, "someone@example.org") + assertEquals(mm.sub, "Stuff") + assertEquals(mm.body, "This is some text") + } + + @Test + fun differentOrder() { + val mm = MatMsg( + "MATMSG:SUB:Stuff;BODY:This is some text;TO:someone@example.org;;" + ) + assertEquals(mm.to, "someone@example.org") + assertEquals(mm.sub, "Stuff") + assertEquals(mm.body, "This is some text") + } +} diff --git a/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/vtype/VTypeTest.kt b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/vtype/VTypeTest.kt index d852cf34..1aacbc10 100644 --- a/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/vtype/VTypeTest.kt +++ b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/vtype/VTypeTest.kt @@ -1,6 +1,5 @@ package de.markusfisch.android.binaryeye.actions.vtype -import de.markusfisch.android.binaryeye.simpleFail import junit.framework.TestCase.* import org.junit.Test diff --git a/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/web/WebTest.kt b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/web/WebTest.kt index 5f434dbb..709271d2 100644 --- a/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/web/WebTest.kt +++ b/app/src/test/kotlin/de/markusfisch/android/binaryeye/actions/web/WebTest.kt @@ -1,6 +1,5 @@ package de.markusfisch.android.binaryeye.actions.web -import de.markusfisch.android.binaryeye.simpleFail import junit.framework.TestCase.* import org.junit.Test