From 0865ef2fc57ad1e567fd4456a33961bab009b92e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Sun, 12 Mar 2023 10:45:24 +0100 Subject: [PATCH] feat(test): Implemented tests for interim numbers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Tegnér --- src/test/java/InterimnummerTest.java | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/java/InterimnummerTest.java diff --git a/src/test/java/InterimnummerTest.java b/src/test/java/InterimnummerTest.java new file mode 100644 index 0000000..91089a1 --- /dev/null +++ b/src/test/java/InterimnummerTest.java @@ -0,0 +1,67 @@ +import java.io.IOException; + +import dev.personnummer.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import static org.junit.jupiter.api.Assertions.*; + +public class InterimnummerTest { + private static Options opts = new Options(true, true); + + @BeforeAll + public static void setup() throws IOException { + DataProvider.initialize(); + } + + @ParameterizedTest + @MethodSource("DataProvider#getValidInterimNumbers") + public void testValidateInterim(PersonnummerData ssn) { + assertTrue(Personnummer.valid(ssn.longFormat, opts)); + assertTrue(Personnummer.valid(ssn.shortFormat, opts)); + } + + @ParameterizedTest + @MethodSource("DataProvider#getInvalidInterimNumbers") + public void testValidateInvalidInterim(PersonnummerData ssn) { + assertFalse(Personnummer.valid(ssn.longFormat, opts)); + assertFalse(Personnummer.valid(ssn.shortFormat, opts)); + } + + @ParameterizedTest + @MethodSource("DataProvider#getValidInterimNumbers") + public void testFormatLongInterim(PersonnummerData ssn) throws PersonnummerException { + Personnummer pnr = Personnummer.parse(ssn.longFormat, opts); + + assertEquals(pnr.format(false), ssn.longFormat); + assertEquals(pnr.format(), ssn.separatedFormat); + } + + @ParameterizedTest + @MethodSource("DataProvider#getValidInterimNumbers") + public void testFormatShortInterim(PersonnummerData ssn) throws PersonnummerException { + Personnummer pnr = Personnummer.parse(ssn.shortFormat, opts); + + assertEquals(pnr.format(false), ssn.longFormat); + assertEquals(pnr.format(), ssn.separatedFormat); + } + + @ParameterizedTest + @MethodSource("DataProvider#getInvalidInterimNumbers") + public void testInvalidInterimThrows(PersonnummerData ssn) { + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.shortFormat, opts)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.longFormat, opts)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.separatedLong, opts)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.separatedFormat, opts)); + } + + @ParameterizedTest + @MethodSource("DataProvider#getValidInterimNumbers") + public void testInterimThrowsIfNotActive(PersonnummerData ssn) { + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.shortFormat)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.longFormat)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.separatedLong)); + assertThrows(PersonnummerException.class, () -> Personnummer.parse(ssn.separatedFormat)); + } +}