From c9db9b115b7260711c7bb8cd8e941fc44a71c787 Mon Sep 17 00:00:00 2001 From: Matthew Pope Date: Thu, 23 May 2024 22:08:06 -0700 Subject: [PATCH] Adds unit tests for _private_hasFirstAnnotation() --- .../ion/impl/IonRawTextWriterTest_1_1.kt | 34 +++++++++++++- .../impl/bin/IonRawBinaryWriterTest_1_1.kt | 46 +++++++++++++++++-- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/amazon/ion/impl/IonRawTextWriterTest_1_1.kt b/src/test/java/com/amazon/ion/impl/IonRawTextWriterTest_1_1.kt index 4d7ae773d..8ae1ef935 100644 --- a/src/test/java/com/amazon/ion/impl/IonRawTextWriterTest_1_1.kt +++ b/src/test/java/com/amazon/ion/impl/IonRawTextWriterTest_1_1.kt @@ -6,7 +6,9 @@ import com.amazon.ion.* import com.amazon.ion.system.* import java.math.BigDecimal import java.math.BigInteger +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -22,7 +24,7 @@ class IonRawTextWriterTest_1_1 { private inline fun ionWriter( out: StringBuilder = StringBuilder(), builderConfigurator: IonTextWriterBuilder_1_1.() -> Unit = { /* noop */ }, - block: IonRawTextWriter_1_1.() -> Unit, + block: IonRawTextWriter_1_1.() -> Unit = {}, ): IonRawTextWriter_1_1 { val b = standardBuilder() .apply(builderConfigurator) @@ -552,6 +554,36 @@ class IonRawTextWriterTest_1_1 { } } + @Test + fun `_private_hasFirstAnnotation() should return false when there are no annotations`() { + val rawWriter = ionWriter() + assertFalse(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, SystemSymbols.ION)) + } + + @Test + fun `_private_hasFirstAnnotation() should return true if only the sid matches`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.ION_SID) + assertTrue(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, null)) + } + + @Test + fun `_private_hasFirstAnnotation() should return true if only the text matches`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.ION) + assertTrue(rawWriter._private_hasFirstAnnotation(-1, SystemSymbols.ION)) + } + + @Test + fun `_private_hasFirstAnnotation() should return false if the first annotation does not match the sid or text`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.IMPORTS_SID) + rawWriter.writeAnnotations(SystemSymbols.ION) + rawWriter.writeAnnotations(SystemSymbols.ION_SID) + // Matches the second and third annotations, but not the first one. + assertFalse(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, SystemSymbols.ION)) + } + @Test fun `write int`() { assertWriterOutputEquals( diff --git a/src/test/java/com/amazon/ion/impl/bin/IonRawBinaryWriterTest_1_1.kt b/src/test/java/com/amazon/ion/impl/bin/IonRawBinaryWriterTest_1_1.kt index a726e77ac..9740cca76 100644 --- a/src/test/java/com/amazon/ion/impl/bin/IonRawBinaryWriterTest_1_1.kt +++ b/src/test/java/com/amazon/ion/impl/bin/IonRawBinaryWriterTest_1_1.kt @@ -9,6 +9,7 @@ import java.io.ByteArrayOutputStream import java.math.BigDecimal import java.math.BigInteger import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows @@ -17,13 +18,18 @@ import org.junit.jupiter.params.provider.CsvSource class IonRawBinaryWriterTest_1_1 { + private fun ionWriter( + baos: ByteArrayOutputStream = ByteArrayOutputStream() + ) = IonRawBinaryWriter_1_1( + out = baos, + buffer = WriteBuffer(BlockAllocatorProviders.basicProvider().vendAllocator(32)) {}, + lengthPrefixPreallocation = 1, + ) + + private inline fun writeAsHexString(autoClose: Boolean = true, block: IonRawBinaryWriter_1_1.() -> Unit): String { val baos = ByteArrayOutputStream() - val rawWriter = IonRawBinaryWriter_1_1( - out = baos, - buffer = WriteBuffer(BlockAllocatorProviders.basicProvider().vendAllocator(32)) {}, - lengthPrefixPreallocation = 1, - ) + val rawWriter = ionWriter(baos) block.invoke(rawWriter) if (autoClose) rawWriter.close() @OptIn(ExperimentalStdlibApi::class) @@ -805,6 +811,36 @@ class IonRawBinaryWriterTest_1_1 { } } + @Test + fun `_private_hasFirstAnnotation() should return false when there are no annotations`() { + val rawWriter = ionWriter() + assertFalse(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, SystemSymbols.ION)) + } + + @Test + fun `_private_hasFirstAnnotation() should return true if only the sid matches`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.ION_SID) + assertTrue(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, null)) + } + + @Test + fun `_private_hasFirstAnnotation() should return true if only the text matches`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.ION) + assertTrue(rawWriter._private_hasFirstAnnotation(-1, SystemSymbols.ION)) + } + + @Test + fun `_private_hasFirstAnnotation() should return false if the first annotation does not match the sid or text`() { + val rawWriter = ionWriter() + rawWriter.writeAnnotations(SystemSymbols.IMPORTS_SID) + rawWriter.writeAnnotations(SystemSymbols.ION) + rawWriter.writeAnnotations(SystemSymbols.ION_SID) + // Matches the second and third annotations, but not the first one. + assertFalse(rawWriter._private_hasFirstAnnotation(SystemSymbols.ION_SID, SystemSymbols.ION)) + } + @Test fun `write int`() { assertWriterOutputEquals(