|
| 1 | +// |
| 2 | +// XMLDecoderUnkeyedTests.swift |
| 3 | +// |
| 4 | +// Copyright (c) 2016 - 2025 Nuno Dias |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | + |
| 24 | +import Foundation |
| 25 | +import Testing |
| 26 | +import XMLKit |
| 27 | + |
| 28 | +@Suite("XMLDecoder - Unkeyed") |
| 29 | +struct XMLDecoderUnkeyedTests: XMLKitTestable { |
| 30 | + @Test |
| 31 | + func decodeUnkeyedValues() throws { |
| 32 | + // Given |
| 33 | + let decoder: XMLDecoder = .init() |
| 34 | + |
| 35 | + let data = """ |
| 36 | + <root> |
| 37 | + <value>abc</value> |
| 38 | + <value>efg</value> |
| 39 | + <value>xyz</value> |
| 40 | + </root> |
| 41 | + """.data(using: .utf8)! |
| 42 | + |
| 43 | + let expected: Root = .init(value: ["abc", "efg", "xyz"]) |
| 44 | + |
| 45 | + // When |
| 46 | + let actual = try decoder.decode(Root.self, from: data) |
| 47 | + |
| 48 | + // Then |
| 49 | + #expect(expected == actual) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + func decodeUnkeyedSingleValue() throws { |
| 54 | + // Given |
| 55 | + let decoder: XMLDecoder = .init() |
| 56 | + |
| 57 | + let data = """ |
| 58 | + <root> |
| 59 | + <value>abc</value> |
| 60 | + </root> |
| 61 | + """.data(using: .utf8)! |
| 62 | + |
| 63 | + let expected: Root = .init(value: ["abc"]) |
| 64 | + |
| 65 | + // When |
| 66 | + let actual = try decoder.decode(Root.self, from: data) |
| 67 | + |
| 68 | + // Then |
| 69 | + #expect(expected == actual) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + func decodeUnkeyedNil() throws { |
| 74 | + // Given |
| 75 | + let decoder: XMLDecoder = .init() |
| 76 | + |
| 77 | + let data = """ |
| 78 | + <root/> |
| 79 | + """.data(using: .utf8)! |
| 80 | + |
| 81 | + let expected: Root = .init(value: nil) |
| 82 | + |
| 83 | + // When |
| 84 | + let actual = try decoder.decode(Root.self, from: data) |
| 85 | + |
| 86 | + // Then |
| 87 | + #expect(expected == actual) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +extension XMLDecoderUnkeyedTests { |
| 92 | + struct Root: Decodable, Equatable { |
| 93 | + var value: [String]? |
| 94 | + } |
| 95 | +} |
0 commit comments