Best Swift-snapshot-testing code snippet using _PlistDecoder
PlistEncoder.swift
Source:PlistEncoder.swift
...520 /// - returns: A value of the requested type.521 /// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not a valid property list.522 /// - throws: An error if any value throws an error during decoding.523 internal func decode<T : Decodable>(_ type: T.Type, fromTopLevel container: Any) throws -> T {524 let decoder = _PlistDecoder(referencing: container, options: self.options)525 guard let value = try decoder.unbox(container, as: type) else {526 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: [], debugDescription: "The given data did not contain a top-level value."))527 }528 return value529 }530}531// MARK: - _PlistDecoder532fileprivate class _PlistDecoder : Decoder {533 // MARK: Properties534 /// The decoder's storage.535 fileprivate var storage: _PlistDecodingStorage536 /// Options set on the top-level decoder.537 fileprivate let options: PropertyListDecoder._Options538 /// The path to the current point in encoding.539 fileprivate(set) public var codingPath: [CodingKey]540 /// Contextual user-provided information for use during encoding.541 public var userInfo: [CodingUserInfoKey : Any] {542 return self.options.userInfo543 }544 // MARK: - Initialization545 /// Initializes `self` with the given top-level container and options.546 fileprivate init(referencing container: Any, at codingPath: [CodingKey] = [], options: PropertyListDecoder._Options) {547 self.storage = _PlistDecodingStorage()548 self.storage.push(container: container)549 self.codingPath = codingPath550 self.options = options551 }552 // MARK: - Decoder Methods553 public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {554 guard !(self.storage.topContainer is NSNull) else {555 throw DecodingError.valueNotFound(KeyedDecodingContainer<Key>.self,556 DecodingError.Context(codingPath: self.codingPath,557 debugDescription: "Cannot get keyed decoding container -- found null value instead."))558 }559 guard let topContainer = self.storage.topContainer as? [String : Any] else {560 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)561 }562 let container = _PlistKeyedDecodingContainer<Key>(referencing: self, wrapping: topContainer)563 return KeyedDecodingContainer(container)564 }565 public func unkeyedContainer() throws -> UnkeyedDecodingContainer {566 guard !(self.storage.topContainer is NSNull) else {567 throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,568 DecodingError.Context(codingPath: self.codingPath,569 debugDescription: "Cannot get unkeyed decoding container -- found null value instead."))570 }571 guard let topContainer = self.storage.topContainer as? [Any] else {572 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: self.storage.topContainer)573 }574 return _PlistUnkeyedDecodingContainer(referencing: self, wrapping: topContainer)575 }576 public func singleValueContainer() throws -> SingleValueDecodingContainer {577 return self578 }579}580// MARK: - Decoding Storage581fileprivate struct _PlistDecodingStorage {582 // MARK: Properties583 /// The container stack.584 /// Elements may be any one of the plist types (NSNumber, Date, String, Array, [String : Any]).585 private(set) fileprivate var containers: [Any] = []586 // MARK: - Initialization587 /// Initializes `self` with no containers.588 fileprivate init() {}589 // MARK: - Modifying the Stack590 fileprivate var count: Int {591 return self.containers.count592 }593 fileprivate var topContainer: Any {594 precondition(!self.containers.isEmpty, "Empty container stack.")595 return self.containers.last!596 }597 fileprivate mutating func push(container: Any) {598 self.containers.append(container)599 }600 fileprivate mutating func popContainer() {601 precondition(!self.containers.isEmpty, "Empty container stack.")602 self.containers.removeLast()603 }604}605// MARK: Decoding Containers606fileprivate struct _PlistKeyedDecodingContainer<K : CodingKey> : KeyedDecodingContainerProtocol {607 typealias Key = K608 // MARK: Properties609 /// A reference to the decoder we're reading from.610 private let decoder: _PlistDecoder611 /// A reference to the container we're reading from.612 private let container: [String : Any]613 /// The path of coding keys taken to get to this point in decoding.614 private(set) public var codingPath: [CodingKey]615 // MARK: - Initialization616 /// Initializes `self` by referencing the given decoder and container.617 fileprivate init(referencing decoder: _PlistDecoder, wrapping container: [String : Any]) {618 self.decoder = decoder619 self.container = container620 self.codingPath = decoder.codingPath621 }622 // MARK: - KeyedDecodingContainerProtocol Methods623 public var allKeys: [Key] {624 return self.container.keys.compactMap { Key(stringValue: $0) }625 }626 public func contains(_ key: Key) -> Bool {627 return self.container[key.stringValue] != nil628 }629 public func decodeNil(forKey key: Key) throws -> Bool {630 guard let entry = self.container[key.stringValue] else {631 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))632 }633 guard let value = entry as? String else {634 return false635 }636 return value == _plistNull637 }638 public func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool {639 guard let entry = self.container[key.stringValue] else {640 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))641 }642 self.decoder.codingPath.append(key)643 defer { self.decoder.codingPath.removeLast() }644 guard let value = try self.decoder.unbox(entry, as: Bool.self) else {645 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))646 }647 return value648 }649 public func decode(_ type: Int.Type, forKey key: Key) throws -> Int {650 guard let entry = self.container[key.stringValue] else {651 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))652 }653 self.decoder.codingPath.append(key)654 defer { self.decoder.codingPath.removeLast() }655 guard let value = try self.decoder.unbox(entry, as: Int.self) else {656 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))657 }658 return value659 }660 public func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 {661 guard let entry = self.container[key.stringValue] else {662 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))663 }664 self.decoder.codingPath.append(key)665 defer { self.decoder.codingPath.removeLast() }666 guard let value = try self.decoder.unbox(entry, as: Int8.self) else {667 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))668 }669 return value670 }671 public func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 {672 guard let entry = self.container[key.stringValue] else {673 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))674 }675 self.decoder.codingPath.append(key)676 defer { self.decoder.codingPath.removeLast() }677 guard let value = try self.decoder.unbox(entry, as: Int16.self) else {678 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))679 }680 return value681 }682 public func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 {683 guard let entry = self.container[key.stringValue] else {684 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))685 }686 self.decoder.codingPath.append(key)687 defer { self.decoder.codingPath.removeLast() }688 guard let value = try self.decoder.unbox(entry, as: Int32.self) else {689 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))690 }691 return value692 }693 public func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 {694 guard let entry = self.container[key.stringValue] else {695 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))696 }697 self.decoder.codingPath.append(key)698 defer { self.decoder.codingPath.removeLast() }699 guard let value = try self.decoder.unbox(entry, as: Int64.self) else {700 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))701 }702 return value703 }704 public func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt {705 guard let entry = self.container[key.stringValue] else {706 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))707 }708 self.decoder.codingPath.append(key)709 defer { self.decoder.codingPath.removeLast() }710 guard let value = try self.decoder.unbox(entry, as: UInt.self) else {711 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))712 }713 return value714 }715 public func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 {716 guard let entry = self.container[key.stringValue] else {717 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))718 }719 self.decoder.codingPath.append(key)720 defer { self.decoder.codingPath.removeLast() }721 guard let value = try self.decoder.unbox(entry, as: UInt8.self) else {722 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))723 }724 return value725 }726 public func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 {727 guard let entry = self.container[key.stringValue] else {728 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))729 }730 self.decoder.codingPath.append(key)731 defer { self.decoder.codingPath.removeLast() }732 guard let value = try self.decoder.unbox(entry, as: UInt16.self) else {733 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))734 }735 return value736 }737 public func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 {738 guard let entry = self.container[key.stringValue] else {739 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))740 }741 self.decoder.codingPath.append(key)742 defer { self.decoder.codingPath.removeLast() }743 guard let value = try self.decoder.unbox(entry, as: UInt32.self) else {744 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))745 }746 return value747 }748 public func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 {749 guard let entry = self.container[key.stringValue] else {750 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))751 }752 self.decoder.codingPath.append(key)753 defer { self.decoder.codingPath.removeLast() }754 guard let value = try self.decoder.unbox(entry, as: UInt64.self) else {755 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))756 }757 return value758 }759 public func decode(_ type: Float.Type, forKey key: Key) throws -> Float {760 guard let entry = self.container[key.stringValue] else {761 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))762 }763 self.decoder.codingPath.append(key)764 defer { self.decoder.codingPath.removeLast() }765 guard let value = try self.decoder.unbox(entry, as: Float.self) else {766 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))767 }768 return value769 }770 public func decode(_ type: Double.Type, forKey key: Key) throws -> Double {771 guard let entry = self.container[key.stringValue] else {772 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))773 }774 self.decoder.codingPath.append(key)775 defer { self.decoder.codingPath.removeLast() }776 guard let value = try self.decoder.unbox(entry, as: Double.self) else {777 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))778 }779 return value780 }781 public func decode(_ type: String.Type, forKey key: Key) throws -> String {782 guard let entry = self.container[key.stringValue] else {783 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))784 }785 self.decoder.codingPath.append(key)786 defer { self.decoder.codingPath.removeLast() }787 guard let value = try self.decoder.unbox(entry, as: String.self) else {788 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))789 }790 return value791 }792 public func decode<T : Decodable>(_ type: T.Type, forKey key: Key) throws -> T {793 guard let entry = self.container[key.stringValue] else {794 throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "No value associated with key \(key) (\"\(key.stringValue)\")."))795 }796 self.decoder.codingPath.append(key)797 defer { self.decoder.codingPath.removeLast() }798 guard let value = try self.decoder.unbox(entry, as: type) else {799 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead."))800 }801 return value802 }803 public func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer<NestedKey> {804 self.decoder.codingPath.append(key)805 defer { self.decoder.codingPath.removeLast() }806 guard let value = self.container[key.stringValue] else {807 throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,808 DecodingError.Context(codingPath: self.codingPath,809 debugDescription: "Cannot get nested keyed container -- no value found for key \"\(key.stringValue)\""))810 }811 guard let dictionary = value as? [String : Any] else {812 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)813 }814 let container = _PlistKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary)815 return KeyedDecodingContainer(container)816 }817 public func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer {818 self.decoder.codingPath.append(key)819 defer { self.decoder.codingPath.removeLast() }820 guard let value = self.container[key.stringValue] else {821 throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,822 DecodingError.Context(codingPath: self.codingPath,823 debugDescription: "Cannot get nested unkeyed container -- no value found for key \"\(key.stringValue)\""))824 }825 guard let array = value as? [Any] else {826 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)827 }828 return _PlistUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)829 }830 private func _superDecoder(forKey key: CodingKey) throws -> Decoder {831 self.decoder.codingPath.append(key)832 defer { self.decoder.codingPath.removeLast() }833 let value: Any = self.container[key.stringValue] ?? NSNull()834 return _PlistDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)835 }836 public func superDecoder() throws -> Decoder {837 return try _superDecoder(forKey: _PlistKey.super)838 }839 public func superDecoder(forKey key: Key) throws -> Decoder {840 return try _superDecoder(forKey: key)841 }842}843fileprivate struct _PlistUnkeyedDecodingContainer : UnkeyedDecodingContainer {844 // MARK: Properties845 /// A reference to the decoder we're reading from.846 private let decoder: _PlistDecoder847 /// A reference to the container we're reading from.848 private let container: [Any]849 /// The path of coding keys taken to get to this point in decoding.850 private(set) public var codingPath: [CodingKey]851 /// The index of the element we're about to decode.852 private(set) public var currentIndex: Int853 // MARK: - Initialization854 /// Initializes `self` by referencing the given decoder and container.855 fileprivate init(referencing decoder: _PlistDecoder, wrapping container: [Any]) {856 self.decoder = decoder857 self.container = container858 self.codingPath = decoder.codingPath859 self.currentIndex = 0860 }861 // MARK: - UnkeyedDecodingContainer Methods862 public var count: Int? {863 return self.container.count864 }865 public var isAtEnd: Bool {866 return self.currentIndex >= self.count!867 }868 public mutating func decodeNil() throws -> Bool {869 guard !self.isAtEnd else {870 throw DecodingError.valueNotFound(Any?.self, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))871 }872 if self.container[self.currentIndex] is NSNull {873 self.currentIndex += 1874 return true875 } else {876 return false877 }878 }879 public mutating func decode(_ type: Bool.Type) throws -> Bool {880 guard !self.isAtEnd else {881 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))882 }883 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))884 defer { self.decoder.codingPath.removeLast() }885 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Bool.self) else {886 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))887 }888 self.currentIndex += 1889 return decoded890 }891 public mutating func decode(_ type: Int.Type) throws -> Int {892 guard !self.isAtEnd else {893 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))894 }895 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))896 defer { self.decoder.codingPath.removeLast() }897 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else {898 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))899 }900 self.currentIndex += 1901 return decoded902 }903 public mutating func decode(_ type: Int8.Type) throws -> Int8 {904 guard !self.isAtEnd else {905 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))906 }907 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))908 defer { self.decoder.codingPath.removeLast() }909 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int8.self) else {910 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))911 }912 self.currentIndex += 1913 return decoded914 }915 public mutating func decode(_ type: Int16.Type) throws -> Int16 {916 guard !self.isAtEnd else {917 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))918 }919 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))920 defer { self.decoder.codingPath.removeLast() }921 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int16.self) else {922 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))923 }924 self.currentIndex += 1925 return decoded926 }927 public mutating func decode(_ type: Int32.Type) throws -> Int32 {928 guard !self.isAtEnd else {929 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))930 }931 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))932 defer { self.decoder.codingPath.removeLast() }933 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int32.self) else {934 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))935 }936 self.currentIndex += 1937 return decoded938 }939 public mutating func decode(_ type: Int64.Type) throws -> Int64 {940 guard !self.isAtEnd else {941 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))942 }943 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))944 defer { self.decoder.codingPath.removeLast() }945 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int64.self) else {946 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))947 }948 self.currentIndex += 1949 return decoded950 }951 public mutating func decode(_ type: UInt.Type) throws -> UInt {952 guard !self.isAtEnd else {953 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))954 }955 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))956 defer { self.decoder.codingPath.removeLast() }957 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt.self) else {958 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))959 }960 self.currentIndex += 1961 return decoded962 }963 public mutating func decode(_ type: UInt8.Type) throws -> UInt8 {964 guard !self.isAtEnd else {965 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))966 }967 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))968 defer { self.decoder.codingPath.removeLast() }969 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt8.self) else {970 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))971 }972 self.currentIndex += 1973 return decoded974 }975 public mutating func decode(_ type: UInt16.Type) throws -> UInt16 {976 guard !self.isAtEnd else {977 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))978 }979 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))980 defer { self.decoder.codingPath.removeLast() }981 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt16.self) else {982 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))983 }984 self.currentIndex += 1985 return decoded986 }987 public mutating func decode(_ type: UInt32.Type) throws -> UInt32 {988 guard !self.isAtEnd else {989 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))990 }991 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))992 defer { self.decoder.codingPath.removeLast() }993 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt32.self) else {994 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))995 }996 self.currentIndex += 1997 return decoded998 }999 public mutating func decode(_ type: UInt64.Type) throws -> UInt64 {1000 guard !self.isAtEnd else {1001 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))1002 }1003 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1004 defer { self.decoder.codingPath.removeLast() }1005 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: UInt64.self) else {1006 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))1007 }1008 self.currentIndex += 11009 return decoded1010 }1011 public mutating func decode(_ type: Float.Type) throws -> Float {1012 guard !self.isAtEnd else {1013 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))1014 }1015 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1016 defer { self.decoder.codingPath.removeLast() }1017 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Float.self) else {1018 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))1019 }1020 self.currentIndex += 11021 return decoded1022 }1023 public mutating func decode(_ type: Double.Type) throws -> Double {1024 guard !self.isAtEnd else {1025 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))1026 }1027 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1028 defer { self.decoder.codingPath.removeLast() }1029 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Double.self) else {1030 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))1031 }1032 self.currentIndex += 11033 return decoded1034 }1035 public mutating func decode(_ type: String.Type) throws -> String {1036 guard !self.isAtEnd else {1037 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))1038 }1039 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1040 defer { self.decoder.codingPath.removeLast() }1041 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: String.self) else {1042 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))1043 }1044 self.currentIndex += 11045 return decoded1046 }1047 public mutating func decode<T : Decodable>(_ type: T.Type) throws -> T {1048 guard !self.isAtEnd else {1049 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Unkeyed container is at end."))1050 }1051 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1052 defer { self.decoder.codingPath.removeLast() }1053 guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: type) else {1054 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_PlistKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))1055 }1056 self.currentIndex += 11057 return decoded1058 }1059 public mutating func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type) throws -> KeyedDecodingContainer<NestedKey> {1060 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1061 defer { self.decoder.codingPath.removeLast() }1062 guard !self.isAtEnd else {1063 throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,1064 DecodingError.Context(codingPath: self.codingPath,1065 debugDescription: "Cannot get nested keyed container -- unkeyed container is at end."))1066 }1067 let value = self.container[self.currentIndex]1068 guard !(value is NSNull) else {1069 throw DecodingError.valueNotFound(KeyedDecodingContainer<NestedKey>.self,1070 DecodingError.Context(codingPath: self.codingPath,1071 debugDescription: "Cannot get keyed decoding container -- found null value instead."))1072 }1073 guard let dictionary = value as? [String : Any] else {1074 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: value)1075 }1076 self.currentIndex += 11077 let container = _PlistKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: dictionary)1078 return KeyedDecodingContainer(container)1079 }1080 public mutating func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer {1081 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1082 defer { self.decoder.codingPath.removeLast() }1083 guard !self.isAtEnd else {1084 throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,1085 DecodingError.Context(codingPath: self.codingPath,1086 debugDescription: "Cannot get nested unkeyed container -- unkeyed container is at end."))1087 }1088 let value = self.container[self.currentIndex]1089 guard !(value is NSNull) else {1090 throw DecodingError.valueNotFound(UnkeyedDecodingContainer.self,1091 DecodingError.Context(codingPath: self.codingPath,1092 debugDescription: "Cannot get keyed decoding container -- found null value instead."))1093 }1094 guard let array = value as? [Any] else {1095 throw DecodingError._typeMismatch(at: self.codingPath, expectation: [Any].self, reality: value)1096 }1097 self.currentIndex += 11098 return _PlistUnkeyedDecodingContainer(referencing: self.decoder, wrapping: array)1099 }1100 public mutating func superDecoder() throws -> Decoder {1101 self.decoder.codingPath.append(_PlistKey(index: self.currentIndex))1102 defer { self.decoder.codingPath.removeLast() }1103 guard !self.isAtEnd else {1104 throw DecodingError.valueNotFound(Decoder.self, DecodingError.Context(codingPath: self.codingPath,1105 debugDescription: "Cannot get superDecoder() -- unkeyed container is at end."))1106 }1107 let value = self.container[self.currentIndex]1108 self.currentIndex += 11109 return _PlistDecoder(referencing: value, at: self.decoder.codingPath, options: self.decoder.options)1110 }1111}1112extension _PlistDecoder : SingleValueDecodingContainer {1113 // MARK: SingleValueDecodingContainer Methods1114 private func expectNonNull<T>(_ type: T.Type) throws {1115 guard !self.decodeNil() else {1116 throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.codingPath, debugDescription: "Expected \(type) but found null value instead."))1117 }1118 }1119 public func decodeNil() -> Bool {1120 guard let string = self.storage.topContainer as? String else {1121 return false1122 }1123 return string == _plistNull1124 }1125 public func decode(_ type: Bool.Type) throws -> Bool {1126 try expectNonNull(Bool.self)1127 return try self.unbox(self.storage.topContainer, as: Bool.self)!1128 }1129 public func decode(_ type: Int.Type) throws -> Int {1130 try expectNonNull(Int.self)1131 return try self.unbox(self.storage.topContainer, as: Int.self)!1132 }1133 public func decode(_ type: Int8.Type) throws -> Int8 {1134 try expectNonNull(Int8.self)1135 return try self.unbox(self.storage.topContainer, as: Int8.self)!1136 }1137 public func decode(_ type: Int16.Type) throws -> Int16 {1138 try expectNonNull(Int16.self)1139 return try self.unbox(self.storage.topContainer, as: Int16.self)!1140 }1141 public func decode(_ type: Int32.Type) throws -> Int32 {1142 try expectNonNull(Int32.self)1143 return try self.unbox(self.storage.topContainer, as: Int32.self)!1144 }1145 public func decode(_ type: Int64.Type) throws -> Int64 {1146 try expectNonNull(Int64.self)1147 return try self.unbox(self.storage.topContainer, as: Int64.self)!1148 }1149 public func decode(_ type: UInt.Type) throws -> UInt {1150 try expectNonNull(UInt.self)1151 return try self.unbox(self.storage.topContainer, as: UInt.self)!1152 }1153 public func decode(_ type: UInt8.Type) throws -> UInt8 {1154 try expectNonNull(UInt8.self)1155 return try self.unbox(self.storage.topContainer, as: UInt8.self)!1156 }1157 public func decode(_ type: UInt16.Type) throws -> UInt16 {1158 try expectNonNull(UInt16.self)1159 return try self.unbox(self.storage.topContainer, as: UInt16.self)!1160 }1161 public func decode(_ type: UInt32.Type) throws -> UInt32 {1162 try expectNonNull(UInt32.self)1163 return try self.unbox(self.storage.topContainer, as: UInt32.self)!1164 }1165 public func decode(_ type: UInt64.Type) throws -> UInt64 {1166 try expectNonNull(UInt64.self)1167 return try self.unbox(self.storage.topContainer, as: UInt64.self)!1168 }1169 public func decode(_ type: Float.Type) throws -> Float {1170 try expectNonNull(Float.self)1171 return try self.unbox(self.storage.topContainer, as: Float.self)!1172 }1173 public func decode(_ type: Double.Type) throws -> Double {1174 try expectNonNull(Double.self)1175 return try self.unbox(self.storage.topContainer, as: Double.self)!1176 }1177 public func decode(_ type: String.Type) throws -> String {1178 try expectNonNull(String.self)1179 return try self.unbox(self.storage.topContainer, as: String.self)!1180 }1181 public func decode<T : Decodable>(_ type: T.Type) throws -> T {1182 try expectNonNull(type)1183 return try self.unbox(self.storage.topContainer, as: type)!1184 }1185}1186// MARK: - Concrete Value Representations1187extension _PlistDecoder {1188 /// Returns the given value unboxed from a container.1189 fileprivate func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {1190 if let string = value as? String, string == _plistNull { return nil }1191 if let number = value as? NSNumber {1192 // TODO: Add a flag to coerce non-boolean numbers into Bools?1193 if number === kCFBooleanTrue as NSNumber {1194 return true1195 } else if number === kCFBooleanFalse as NSNumber {1196 return false1197 }1198 /* FIXME: If swift-corelibs-foundation doesn't change to use NSNumber, this code path will need to be included and tested:1199 } else if let bool = value as? Bool {1200 return bool1201 */...
_PlistDecoder
Using AI Code Generation
1import SnapshotTesting2import XCTest3class _PlistDecoder: Snapshotting {4 public func snapshot(value: Any) -> String {5 let data = try! PropertyListSerialization.data(fromPropertyList: value, format: .xml, options: 0)6 return String(data: data, encoding: .utf8)!7 }8 public func diffing(snapshot: String) -> (String) -> (String) {9 return { other in10 }11 }12}13extension Snapshotting where Value == Any, Format == String {14 public static let plist = _PlistDecoder()15}16struct User: Codable {17}18class SnapshotTests: XCTestCase {19 func testPlist() {20 let user = User(id: 1, name: "Blob")21 assertSnapshot(matching: user, as: .plist)22 }23}24import SnapshotTesting25import XCTest26class _PlistEncoder: Snapshotting {27 public func snapshot(value: Any) -> String {28 let data = try! PropertyListSerialization.data(fromPropertyList: value, format: .xml, options: 0)29 return String(data: data, encoding: .utf8)!30 }31 public func diffing(snapshot: String) -> (String) -> (String) {32 return { other in33 }34 }35}36extension Snapshotting where Value == Any, Format == String {37 public static let plist = _PlistEncoder()38}39struct User: Codable {40}41class SnapshotTests: XCTestCase {42 func testPlist() {43 let user = User(id: 1, name: "Blob")44 assertSnapshot(matching: user, as: .plist)45 }46}
_PlistDecoder
Using AI Code Generation
1import XCTest2import SnapshotTesting3class _PlistDecoderTests: XCTestCase {4 func testDecode() {5 {6 }7 """.data(using: .utf8)!8 let decoder = _PlistDecoder()9 let value = try! decoder.decode([String: String].self, from: data)10 assertSnapshot(matching: value, as: .dump)11 }12}13import XCTest14import SnapshotTesting15class _PlistEncoderTests: XCTestCase {16 func testEncode() {17 let encoder = _PlistEncoder()18 let data = try! encoder.encode(value)19 assertSnapshot(matching: data, as: .raw)20 }21}22import XCTest23import SnapshotTesting24class _PlistKeyedEncodingContainerTests: XCTestCase {25 func testEncode() {26 let encoder = _PlistEncoder()27 let data = try! encoder.encode(value)28 assertSnapshot(matching: data, as: .raw)29 }30}31import XCTest32import SnapshotTesting33class _PlistUnkeyedEncodingContainerTests: XCTestCase {34 func testEncode() {35 let encoder = _PlistEncoder()36 let data = try! encoder.encode(value)37 assertSnapshot(matching: data, as: .raw)38 }39}40import XCTest41import SnapshotTesting42class _PlistSingleValueEncodingContainerTests: XCTestCase {43 func testEncode() {44 let encoder = _PlistEncoder()45 let data = try! encoder.encode(value)46 assertSnapshot(matching: data, as: .raw)47 }48}49import XCTest50import SnapshotTesting
_PlistDecoder
Using AI Code Generation
1import Foundation2import SnapshotTesting3{4}5""".data(using: .utf8)!6let decoder = _PlistDecoder()7let foo = try! decoder.decode(Foo.self, from: json)8struct Foo: Codable {9}10import Foundation11import SnapshotTesting12let foo = Foo(id: 1, name: "Foo")13let encoder = _PlistEncoder()14let data = try! encoder.encode(foo)15let json = String(data: data, encoding: .utf8)!16struct Foo: Codable {17}18import Foundation19import SnapshotTesting20let foo = Foo(id: 1, name: "Foo")21assertSnapshot(matching: foo, as: .plist)22struct Foo: Codable {23}24import Foundation25import SnapshotTesting26let foo = Foo(id: 1, name: "Foo")27assertSnapshot(matching: foo, as: .json)28struct Foo: Codable {29}30import Foundation31import SnapshotTesting32let foo = Foo(id: 1, name: "Foo")33assertSnapshot(matching: foo, as: .dump)34struct Foo: Codable {35}36import Foundation37import SnapshotTesting38let foo = Foo(id: 1, name: "Foo")39assertSnapshot(matching: foo, as: .raw)40struct Foo: Codable {41}42import Foundation43import SnapshotTesting44let foo = Foo(id: 1, name: "Foo")45assertSnapshot(matching: foo, as: .image)46struct Foo: Codable {
_PlistDecoder
Using AI Code Generation
1let decoder = _PlistDecoder()2let actual = try decoder.decode(Actual.self, from: actualData)3let expected = try decoder.decode(Expected.self, from: expectedData)4XCTAssertEqual(actual, expected)5let encoder = _PlistEncoder()6let actual = try encoder.encode(actualData)7let expected = try encoder.encode(expectedData)8XCTAssertEqual(actual, expected)9let decoder = _PlistDecoder()10let actual = try decoder.decode(Actual.self, from: actualData)11let expected = try decoder.decode(Expected.self, from: expectedData)12XCTAssertEqual(actual, expected)13let encoder = _PlistEncoder()14let actual = try encoder.encode(actualData)15let expected = try encoder.encode(expectedData)16XCTAssertEqual(actual, expected)17let decoder = _PlistDecoder()18let actual = try decoder.decode(Actual.self, from: actualData)19let expected = try decoder.decode(Expected.self, from: expectedData)20XCTAssertEqual(actual, expected)21let encoder = _PlistEncoder()22let actual = try encoder.encode(actualData)23let expected = try encoder.encode(expectedData)24XCTAssertEqual(actual, expected)25let decoder = _PlistDecoder()26let actual = try decoder.decode(Actual.self, from: actualData)27let expected = try decoder.decode(Expected.self, from: expectedData)28XCTAssertEqual(actual, expected)29let encoder = _PlistEncoder()30let actual = try encoder.encode(actualData)31let expected = try encoder.encode(expectedData)32XCTAssertEqual(actual, expected)
_PlistDecoder
Using AI Code Generation
1let decoder = _PlistDecoder()2let data = try Data(contentsOf: URL(fileURLWithPath: "1.plist"))3let object = try decoder.decode(AnyObject.self, from: data)4let decoder = _PlistDecoder()5let data = try Data(contentsOf: URL(fileURLWithPath: "2.plist"))6let object = try decoder.decode(AnyObject.self, from: data)7let decoder = _PlistDecoder()8let data = try Data(contentsOf: URL(fileURLWithPath: "3.plist"))9let object = try decoder.decode(AnyObject.self, from: data)10let decoder = _PlistDecoder()11let data = try Data(contentsOf: URL(fileURLWithPath: "4.plist"))12let object = try decoder.decode(AnyObject.self, from: data)13let decoder = _PlistDecoder()14let data = try Data(contentsOf: URL(fileURLWithPath: "5.plist"))15let object = try decoder.decode(AnyObject.self, from: data)16let decoder = _PlistDecoder()17let data = try Data(contentsOf: URL(fileURLWithPath: "6.plist"))18let object = try decoder.decode(AnyObject.self, from: data)19let decoder = _PlistDecoder()20let data = try Data(contentsOf: URL(fileURLWithPath: "7.plist"))21let object = try decoder.decode(AnyObject.self, from: data)22let decoder = _PlistDecoder()23let data = try Data(contentsOf: URL(fileURLWithPath: "8.plist"))24let object = try decoder.decode(AnyObject.self, from: data)
_PlistDecoder
Using AI Code Generation
1import XCTest2import SnapshotTesting3class SnapshotTestingTests: XCTestCase {4 func testDecoding() {5 let decoder = _PlistDecoder()6 let value = try! decoder.decode([String: Int].self, from: """7 assertSnapshot(matching: value, as: .dump)8 }9}10import XCTest11import SnapshotTesting12class SnapshotTestingTests: XCTestCase {13 func testEncoding() {14 let encoder = _PlistEncoder()15 let data = try! encoder.encode(value)16 let string = String(data: data, encoding: .utf8)!17 assertSnapshot(matching: string, as: .lines)18 }19}20import XCTest21import SnapshotTesting22class SnapshotTestingTests: XCTestCase {23 func testEncoding() {24 let encoder = _PlistEncoder()25 let data = try! encoder.encode(value)26 let string = String(data: data, encoding: .utf8)!27 assertSnapshot(matching: string, as: .lines)28 }29}30import XCTest31import SnapshotTesting32class SnapshotTestingTests: XCTestCase {33 func testEncoding() {34 let encoder = _PlistEncoder()35 let data = try! encoder.encode(value)36 let string = String(data: data, encoding: .utf8)!37 assertSnapshot(matching: string, as: .lines)38 }39}40import XCTest41import SnapshotTesting42class SnapshotTestingTests: XCTestCase {43 func testEncoding() {44 let encoder = _PlistEncoder()45 let data = try! encoder.encode(value)46 let string = String(data: data, encoding: .utf8)!47 assertSnapshot(matching: string, as: .lines)48 }49}
_PlistDecoder
Using AI Code Generation
1import Foundation2import SnapshotTesting3import XCTest4final class SnapshotTestingTests: XCTestCase {5 func testExample() {6 {7 }8 """.data(using: .utf8)!9 let decoder = _PlistDecoder()10 let person = try! decoder.decode(Person.self, from: json)11 print(person)12 assertSnapshot(matching: person, as: .dump)13 }14}15import Foundation16import SnapshotTesting17import XCTest18final class SnapshotTestingTests: XCTestCase {19 func testExample() {20 {21 }22 """.data(using: .utf8)!23 let decoder = _JSONDecoder()24 let person = try! decoder.decode(Person.self, from: json)25 print(person)26 assertSnapshot(matching: person, as: .dump)27 }28}29import Foundation30import SnapshotTesting31import XCTest32final class SnapshotTestingTests: XCTestCase {33 func testExample() {34 {35 }36 """.data(using: .utf8)!37 let decoder = JSONDecoder()38 let person = try! decoder.decode(Person.self, from: json)39 print(person)40 assertSnapshot(matching: person, as: .dump)41 }42}43import Foundation44import SnapshotTesting45import XCTest46final class SnapshotTestingTests: XCTestCase {47 func testExample() {48 {49 }50 """.data(using: .utf8)!
_PlistDecoder
Using AI Code Generation
1import Foundation2import XCTest3import SnapshotTesting4class MyTestCase: XCTestCase {5 func testSomething() {6 let decoder = _PlistDecoder()7 let data = Data()8 let result = try! decoder.decode(Any.self, from: data)9 }10}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!