1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16+
1617import GRPCCore
17- import XCTest
18+ import Testing
1819
19- final class MetadataTests : XCTestCase {
20- func testInitFromSequence( ) {
20+ @Suite ( " Metadata " )
21+ struct MetadataTests {
22+ @Test ( " Initialize from Sequence " )
23+ func initFromSequence( ) {
2124 let elements : [ Metadata . Element ] = [
2225 ( key: " key1 " , value: " value1 " ) ,
2326 ( key: " key2 " , value: " value2 " ) ,
@@ -26,218 +29,227 @@ final class MetadataTests: XCTestCase {
2629
2730 let metadata = Metadata ( elements)
2831 let expected : Metadata = [ " key1 " : " value1 " , " key2 " : " value2 " , " key3 " : " value3 " ]
29-
30- XCTAssertEqual ( metadata, expected)
32+ #expect( metadata == expected)
3133 }
3234
33- func testAddStringValue( ) {
35+ @Test ( " Add string Value " )
36+ func addStringValue( ) {
3437 var metadata = Metadata ( )
35- XCTAssertTrue ( metadata. isEmpty)
38+ #expect ( metadata. isEmpty)
3639
3740 metadata. addString ( " testValue " , forKey: " testString " )
38- XCTAssertEqual ( metadata. count, 1 )
41+ #expect ( metadata. count == 1 )
3942
4043 let sequence = metadata [ stringValues: " testString " ]
4144 var iterator = sequence. makeIterator ( )
42- XCTAssertEqual ( iterator. next ( ) , " testValue " )
43- XCTAssertNil ( iterator. next ( ) )
45+ #expect ( iterator. next ( ) == " testValue " )
46+ #expect ( iterator. next ( ) == nil )
4447 }
4548
46- func testAddBinaryValue( ) {
49+ @Test ( " Add binary value " )
50+ func addBinaryValue( ) {
4751 var metadata = Metadata ( )
48- XCTAssertTrue ( metadata. isEmpty)
52+ #expect ( metadata. isEmpty)
4953
5054 metadata. addBinary ( Array ( " base64encodedString " . utf8) , forKey: " testBinary-bin " )
51- XCTAssertEqual ( metadata. count, 1 )
55+ #expect ( metadata. count == 1 )
5256
5357 let sequence = metadata [ binaryValues: " testBinary-bin " ]
5458 var iterator = sequence. makeIterator ( )
55- XCTAssertEqual ( iterator. next ( ) , Array ( " base64encodedString " . utf8) )
56- XCTAssertNil ( iterator. next ( ) )
59+ #expect ( iterator. next ( ) == Array ( " base64encodedString " . utf8) )
60+ #expect ( iterator. next ( ) == nil )
5761 }
5862
59- func testCreateFromDictionaryLiteral( ) {
63+ @Test ( " Initialize from dictionary literal " )
64+ func initFromDictionaryLiteral( ) {
6065 let metadata : Metadata = [
6166 " testKey " : " stringValue " ,
6267 " testKey-bin " : . binary( Array ( " base64encodedString " . utf8) ) ,
6368 ]
64- XCTAssertEqual ( metadata. count, 2 )
69+ #expect ( metadata. count == 2 )
6570
6671 let stringSequence = metadata [ stringValues: " testKey " ]
6772 var stringIterator = stringSequence. makeIterator ( )
68- XCTAssertEqual ( stringIterator. next ( ) , " stringValue " )
69- XCTAssertNil ( stringIterator. next ( ) )
73+ #expect ( stringIterator. next ( ) == " stringValue " )
74+ #expect ( stringIterator. next ( ) == nil )
7075
7176 let binarySequence = metadata [ binaryValues: " testKey-bin " ]
7277 var binaryIterator = binarySequence. makeIterator ( )
73- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " base64encodedString " . utf8) )
74- XCTAssertNil ( binaryIterator. next ( ) )
78+ #expect ( binaryIterator. next ( ) == Array ( " base64encodedString " . utf8) )
79+ #expect ( binaryIterator. next ( ) == nil )
7580 }
7681
77- func testReplaceOrAddValue( ) {
78- var metadata : Metadata = [
79- " testKey " : " value1 " ,
80- " testKey " : " value2 " ,
81- ]
82- XCTAssertEqual ( metadata. count, 2 )
82+ @Suite ( " Replace or add value " )
83+ struct ReplaceOrAdd {
84+ @Suite ( " String " )
85+ struct StringValues {
86+ var metadata : Metadata = [
87+ " key1 " : " value1 " ,
88+ " key1 " : " value2 " ,
89+ ]
90+
91+ @Test ( " Add different key " )
92+ mutating func addNewKey( ) async throws {
93+ self . metadata. replaceOrAddString ( " value3 " , forKey: " key2 " )
94+ #expect( Array ( self . metadata [ stringValues: " key1 " ] ) == [ " value1 " , " value2 " ] )
95+ #expect( Array ( self . metadata [ stringValues: " key2 " ] ) == [ " value3 " ] )
96+ #expect( self . metadata. count == 3 )
97+ }
98+
99+ @Test ( " Replace values for existing key " )
100+ mutating func replaceValues( ) async throws {
101+ self . metadata. replaceOrAddString ( " value3 " , forKey: " key1 " )
102+ #expect( Array ( self . metadata [ stringValues: " key1 " ] ) == [ " value3 " ] )
103+ #expect( self . metadata. count == 1 )
104+ }
105+ }
83106
84- var sequence = metadata [ stringValues: " testKey " ]
85- var iterator = sequence. makeIterator ( )
86- XCTAssertEqual ( iterator. next ( ) , " value1 " )
87- XCTAssertEqual ( iterator. next ( ) , " value2 " )
88- XCTAssertNil ( iterator. next ( ) )
89-
90- metadata. replaceOrAddString ( " anotherValue " , forKey: " testKey2 " )
91- XCTAssertEqual ( metadata. count, 3 )
92- sequence = metadata [ stringValues: " testKey " ]
93- iterator = sequence. makeIterator ( )
94- XCTAssertEqual ( iterator. next ( ) , " value1 " )
95- XCTAssertEqual ( iterator. next ( ) , " value2 " )
96- XCTAssertNil ( iterator. next ( ) )
97- sequence = metadata [ stringValues: " testKey2 " ]
98- iterator = sequence. makeIterator ( )
99- XCTAssertEqual ( iterator. next ( ) , " anotherValue " )
100- XCTAssertNil ( iterator. next ( ) )
101-
102- metadata. replaceOrAddString ( " newValue " , forKey: " testKey " )
103- XCTAssertEqual ( metadata. count, 2 )
104- sequence = metadata [ stringValues: " testKey " ]
105- iterator = sequence. makeIterator ( )
106- XCTAssertEqual ( iterator. next ( ) , " newValue " )
107- XCTAssertNil ( iterator. next ( ) )
108- sequence = metadata [ stringValues: " testKey2 " ]
109- iterator = sequence. makeIterator ( )
110- XCTAssertEqual ( iterator. next ( ) , " anotherValue " )
111- XCTAssertNil ( iterator. next ( ) )
107+ @Suite ( " Binary " )
108+ struct BinaryValues {
109+ var metadata : Metadata = [
110+ " key1-bin " : [ 0 ] ,
111+ " key1-bin " : [ 1 ] ,
112+ ]
113+
114+ @Test ( " Add different key " )
115+ mutating func addNewKey( ) async throws {
116+ self . metadata. replaceOrAddBinary ( [ 2 ] , forKey: " key2-bin " )
117+ #expect( Array ( self . metadata [ binaryValues: " key1-bin " ] ) == [ [ 0 ] , [ 1 ] ] )
118+ #expect( Array ( self . metadata [ binaryValues: " key2-bin " ] ) == [ [ 2 ] ] )
119+ #expect( self . metadata. count == 3 )
120+ }
121+
122+ @Test ( " Replace values for existing key " )
123+ mutating func replaceValues( ) async throws {
124+ self . metadata. replaceOrAddBinary ( [ 2 ] , forKey: " key1-bin " )
125+ #expect( Array ( self . metadata [ binaryValues: " key1-bin " ] ) == [ [ 2 ] ] )
126+ #expect( self . metadata. count == 1 )
127+ }
128+ }
112129 }
113130
114- func testReserveCapacity( ) {
131+ @Test ( " Reserve more capacity increases capacity " )
132+ func reserveMoreCapacity( ) {
115133 var metadata = Metadata ( )
116- XCTAssertEqual ( metadata. capacity, 0 )
134+ #expect( metadata. capacity == 0 )
135+
136+ metadata. reserveCapacity ( 10 )
137+ #expect( metadata. capacity == 10 )
138+ }
117139
140+ @Test ( " Reserve less capacity doesn't reduce capacity " )
141+ func reserveCapacity( ) {
142+ var metadata = Metadata ( )
143+ #expect( metadata. capacity == 0 )
118144 metadata. reserveCapacity ( 10 )
119- XCTAssertEqual ( metadata. capacity, 10 )
145+ #expect( metadata. capacity == 10 )
146+ metadata. reserveCapacity ( 0 )
147+ #expect( metadata. capacity == 10 )
120148 }
121149
122- func testValuesIteration( ) {
150+ @Test ( " Iterate over all values for a key " )
151+ func iterateOverValuesForKey( ) {
123152 let metadata : Metadata = [
124- " testKey -bin" : " string1 " ,
125- " testKey -bin" : . binary ( . init ( " data1 " . utf8 ) ) ,
126- " testKey -bin" : " string2 " ,
127- " testKey -bin" : . binary ( . init ( " data2 " . utf8 ) ) ,
128- " testKey -bin" : " string3 " ,
129- " testKey -bin" : . binary ( . init ( " data3 " . utf8 ) ) ,
153+ " key -bin" : " 1 " ,
154+ " key -bin" : [ 1 ] ,
155+ " key -bin" : " 2 " ,
156+ " key -bin" : [ 2 ] ,
157+ " key -bin" : " 3 " ,
158+ " key -bin" : [ 3 ] ,
130159 ]
131- XCTAssertEqual ( metadata. count, 6 )
132160
133- let sequence = metadata [ " testKey-bin " ]
134- var iterator = sequence. makeIterator ( )
135- XCTAssertEqual ( iterator. next ( ) , . string( " string1 " ) )
136- XCTAssertEqual ( iterator. next ( ) , . binary( . init( " data1 " . utf8) ) )
137- XCTAssertEqual ( iterator. next ( ) , . string( " string2 " ) )
138- XCTAssertEqual ( iterator. next ( ) , . binary( . init( " data2 " . utf8) ) )
139- XCTAssertEqual ( iterator. next ( ) , . string( " string3 " ) )
140- XCTAssertEqual ( iterator. next ( ) , . binary( . init( " data3 " . utf8) ) )
141- XCTAssertNil ( iterator. next ( ) )
161+ #expect( Array ( metadata [ " key-bin " ] ) == [ " 1 " , [ 1 ] , " 2 " , [ 2 ] , " 3 " , [ 3 ] ] )
142162 }
143163
144- func testStringValuesIteration( ) {
164+ @Test ( " Iterate over string values for a key " )
165+ func iterateOverStringsForKey( ) {
145166 let metadata : Metadata = [
146- " testKey -bin" : " string1 " ,
147- " testKey -bin" : . binary ( . init ( " data1 " . utf8 ) ) ,
148- " testKey -bin" : " string2 " ,
149- " testKey -bin" : . binary ( . init ( " data2 " . utf8 ) ) ,
150- " testKey -bin" : " string3 " ,
151- " testKey -bin" : . binary ( . init ( " data3 " . utf8 ) ) ,
167+ " key -bin" : " 1 " ,
168+ " key -bin" : [ 1 ] ,
169+ " key -bin" : " 2 " ,
170+ " key -bin" : [ 2 ] ,
171+ " key -bin" : " 3 " ,
172+ " key -bin" : [ 3 ] ,
152173 ]
153- XCTAssertEqual ( metadata. count, 6 )
154174
155- let stringSequence = metadata [ stringValues: " testKey-bin " ]
156- var stringIterator = stringSequence. makeIterator ( )
157- XCTAssertEqual ( stringIterator. next ( ) , " string1 " )
158- XCTAssertEqual ( stringIterator. next ( ) , " string2 " )
159- XCTAssertEqual ( stringIterator. next ( ) , " string3 " )
160- XCTAssertNil ( stringIterator. next ( ) )
175+ #expect( Array ( metadata [ stringValues: " key-bin " ] ) == [ " 1 " , " 2 " , " 3 " ] )
161176 }
162177
163- func testBinaryValuesIteration_InvalidBase64EncodedStrings( ) {
178+ @Test ( " Iterate over binary values for a key " )
179+ func iterateOverBinaryForKey( ) {
164180 let metadata : Metadata = [
165- " testKey -bin" : " invalidBase64- 1" ,
166- " testKey -bin" : . binary ( . init ( " data1 " . utf8 ) ) ,
167- " testKey -bin" : " invalidBase64- 2" ,
168- " testKey -bin" : . binary ( . init ( " data2 " . utf8 ) ) ,
169- " testKey -bin" : " invalidBase64- 3" ,
170- " testKey -bin" : . binary ( . init ( " data3 " . utf8 ) ) ,
181+ " key -bin" : " 1 " ,
182+ " key -bin" : [ 1 ] ,
183+ " key -bin" : " 2 " ,
184+ " key -bin" : [ 2 ] ,
185+ " key -bin" : " 3 " ,
186+ " key -bin" : [ 3 ] ,
171187 ]
172- XCTAssertEqual ( metadata. count, 6 )
173188
174- let binarySequence = metadata [ binaryValues: " testKey-bin " ]
175- var binaryIterator = binarySequence. makeIterator ( )
176- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data1 " . utf8) )
177- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data2 " . utf8) )
178- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data3 " . utf8) )
179- XCTAssertNil ( binaryIterator. next ( ) )
189+ #expect( Array ( metadata [ binaryValues: " key-bin " ] ) == [ [ 1 ] , [ 2 ] , [ 3 ] ] )
180190 }
181191
182- func testBinaryValuesIteration_ValidBase64EncodedStrings( ) {
192+ @Test ( " Iterate over base64 encoded binary values for a key " )
193+ func iterateOverBase64BinaryEncodedValuesForKey( ) {
183194 let metadata : Metadata = [
184- " testKey -bin" : " c3RyaW5nMQ== " ,
185- " testKey -bin" : . binary( . init( " data1 " . utf8) ) ,
186- " testKey -bin" : " c3RyaW5nMg== " ,
187- " testKey -bin" : . binary( . init( " data2 " . utf8) ) ,
188- " testKey -bin" : " c3RyaW5nMw== " ,
189- " testKey -bin" : . binary( . init( " data3 " . utf8) ) ,
195+ " key -bin" : " c3RyaW5nMQ== " ,
196+ " key -bin" : . binary( . init( " data1 " . utf8) ) ,
197+ " key -bin" : " c3RyaW5nMg== " ,
198+ " key -bin" : . binary( . init( " data2 " . utf8) ) ,
199+ " key -bin" : " c3RyaW5nMw== " ,
200+ " key -bin" : . binary( . init( " data3 " . utf8) ) ,
190201 ]
191- XCTAssertEqual ( metadata. count, 6 )
192202
193- let binarySequence = metadata [ binaryValues: " testKey-bin " ]
194- var binaryIterator = binarySequence. makeIterator ( )
195- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " string1 " . utf8) )
196- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data1 " . utf8) )
197- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " string2 " . utf8) )
198- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data2 " . utf8) )
199- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " string3 " . utf8) )
200- XCTAssertEqual ( binaryIterator. next ( ) , Array ( " data3 " . utf8) )
201- XCTAssertNil ( binaryIterator. next ( ) )
203+ let expected : [ [ UInt8 ] ] = [
204+ Array ( " string1 " . utf8) ,
205+ Array ( " data1 " . utf8) ,
206+ Array ( " string2 " . utf8) ,
207+ Array ( " data2 " . utf8) ,
208+ Array ( " string3 " . utf8) ,
209+ Array ( " data3 " . utf8) ,
210+ ]
211+
212+ #expect( Array ( metadata [ binaryValues: " key-bin " ] ) == expected)
202213 }
203214
204- func testKeysAreCaseInsensitive( ) {
215+ @Test ( " Subscripts are case-insensitive " )
216+ func subscriptIsCaseInsensitive( ) {
205217 let metadata : Metadata = [
206- " testkey1 " : " value1 " ,
207- " TESTKEY2 " : " value2 " ,
218+ " key1 " : " value1 " ,
219+ " KEY2 " : " value2 " ,
208220 ]
209- XCTAssertEqual ( metadata. count, 2 )
210221
211- var stringSequence = metadata [ stringValues: " TESTKEY1 " ]
212- var stringIterator = stringSequence. makeIterator ( )
213- XCTAssertEqual ( stringIterator. next ( ) , " value1 " )
214- XCTAssertNil ( stringIterator. next ( ) )
222+ #expect( Array ( metadata [ stringValues: " key1 " ] ) == [ " value1 " ] )
223+ #expect( Array ( metadata [ stringValues: " KEY1 " ] ) == [ " value1 " ] )
215224
216- stringSequence = metadata [ stringValues: " testkey2 " ]
217- stringIterator = stringSequence. makeIterator ( )
218- XCTAssertEqual ( stringIterator. next ( ) , " value2 " )
219- XCTAssertNil ( stringIterator. next ( ) )
225+ #expect( Array ( metadata [ stringValues: " key2 " ] ) == [ " value2 " ] )
226+ #expect( Array ( metadata [ stringValues: " KEY2 " ] ) == [ " value2 " ] )
220227 }
221228
222- func testRemoveAllWhere( ) {
223- let metadata : Metadata = [
224- " testKey1 " : " value1 " ,
225- " testKey2 " : " value2 " ,
226- " testKey3 " : " value1 " ,
229+ @Suite ( " Remove all " )
230+ struct RemoveAll {
231+ var metadata : Metadata = [
232+ " key1 " : " value1 " ,
233+ " key2 " : " value2 " ,
234+ " key3 " : " value1 " ,
227235 ]
228236
229- var metadata1 = metadata
230- metadata1. removeAll { _, value in
231- value == " value1 "
237+ @Test ( " Where value matches " )
238+ mutating func removeAllWhereValueMatches( ) async throws {
239+ self . metadata. removeAll { _, value in
240+ value == " value1 "
241+ }
242+
243+ #expect( self . metadata == [ " key2 " : " value2 " ] )
232244 }
233245
234- XCTAssertEqual ( metadata1, [ " testKey2 " : " value2 " ] )
246+ @Test ( " Where key matches " )
247+ mutating func removeAllWhereKeyMatches( ) async throws {
248+ self . metadata. removeAll { key, _ in
249+ key == " key2 "
250+ }
235251
236- var metadata2 = metadata
237- metadata2. removeAll { key, _ in
238- key == " testKey2 "
252+ #expect( self . metadata == [ " key1 " : " value1 " , " key3 " : " value1 " ] )
239253 }
240-
241- XCTAssertEqual ( metadata2, [ " testKey1 " : " value1 " , " testKey3 " : " value1 " ] )
242254 }
243255}
0 commit comments