1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf.kotlin 9 10 import com.google.common.testing.EqualsTester 11 import com.google.common.truth.Truth.assertThat 12 import com.google.protobuf.kotlin.test.ExampleExtensibleMessage 13 import com.google.protobuf.kotlin.test.ExampleExtensibleMessageOuterClass as TestProto 14 import kotlin.test.assertFailsWith 15 import org.junit.Test 16 import org.junit.runner.RunWith 17 import org.junit.runners.JUnit4 18 19 /** Tests for [DslList]. */ 20 @RunWith(JUnit4::class) 21 @OptIn(OnlyForUseByGeneratedProtoCode::class) 22 class ExtensionListTest { 23 class DummyProxy private constructor() : DslProxy() 24 25 @Test matchesListnull26 fun matchesList() { 27 assertThat( 28 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, listOf(1, 2, 3)) 29 ) 30 .containsExactly(1, 2, 3) 31 .inOrder() 32 } 33 34 @Test reflectsChangesInListnull35 fun reflectsChangesInList() { 36 val mutableList = mutableListOf(1, 2, 3) 37 val extensionList = 38 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, mutableList) 39 mutableList.add(4) 40 assertThat(extensionList).containsExactly(1, 2, 3, 4).inOrder() 41 } 42 43 @Test extensionListIsNotMutablenull44 fun extensionListIsNotMutable() { 45 val extensionList = 46 ExtensionList<Int, ExampleExtensibleMessage>( 47 TestProto.repeatedExtension, 48 mutableListOf(1, 2, 3), 49 ) 50 assertThat(extensionList is MutableList<*>).isFalse() 51 } 52 53 @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 54 @Test extensionListIsNotEvenSecretlyMutablenull55 fun extensionListIsNotEvenSecretlyMutable() { 56 val extensionList = 57 ExtensionList<Int, ExampleExtensibleMessage>( 58 TestProto.repeatedExtension, 59 mutableListOf(1, 2, 3), 60 ) 61 val extensionListAsJavaUtil = extensionList as java.util.List<Int> 62 assertFailsWith<UnsupportedOperationException> { extensionListAsJavaUtil.add(4) } 63 } 64 65 @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 66 @Test extensionList_IteratorIsNotEvenSecretlyMutablenull67 fun extensionList_IteratorIsNotEvenSecretlyMutable() { 68 val extensionList = 69 ExtensionList<Int, ExampleExtensibleMessage>( 70 TestProto.repeatedExtension, 71 mutableListOf(1, 2, 3), 72 ) 73 val iterator = extensionList.iterator() as java.util.Iterator<Int> 74 iterator.next() 75 76 assertFailsWith<UnsupportedOperationException> { iterator.remove() } 77 } 78 79 @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 80 @Test extensionList_ListIteratorIsNotEvenSecretlyMutablenull81 fun extensionList_ListIteratorIsNotEvenSecretlyMutable() { 82 val extensionList = 83 ExtensionList<Int, ExampleExtensibleMessage>( 84 TestProto.repeatedExtension, 85 mutableListOf(1, 2, 3), 86 ) 87 val iterator = extensionList.listIterator() as java.util.ListIterator<Int> 88 iterator.next() 89 90 assertFailsWith<UnsupportedOperationException> { iterator.remove() } 91 } 92 93 @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") 94 @Test extensionList_ListIteratorIndexIsNotEvenSecretlyMutablenull95 fun extensionList_ListIteratorIndexIsNotEvenSecretlyMutable() { 96 val extensionList = 97 ExtensionList<Int, ExampleExtensibleMessage>( 98 TestProto.repeatedExtension, 99 mutableListOf(1, 2, 3), 100 ) 101 val iterator = extensionList.listIterator(1) as java.util.ListIterator<Int> 102 iterator.next() 103 104 assertFailsWith<UnsupportedOperationException> { iterator.remove() } 105 } 106 107 @Test expectedToStringnull108 fun expectedToString() { 109 assertThat( 110 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, listOf(1, 2)) 111 .toString() 112 ) 113 .isEqualTo("[1, 2]") 114 } 115 116 @Test equalitynull117 fun equality() { 118 EqualsTester() 119 .addEqualityGroup( 120 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, listOf(1, 2)), 121 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.differentExtension, listOf(1, 2)), 122 listOf(1, 2), 123 ) 124 .addEqualityGroup( 125 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, listOf(2, 2)), 126 listOf(2, 2), 127 ) 128 .addEqualityGroup( 129 ExtensionList<Int, ExampleExtensibleMessage>(TestProto.repeatedExtension, emptyList()), 130 emptyList<Int>(), 131 ) 132 .testEquals() 133 } 134 } 135