Best Swift-snapshot-testing code snippet using _PlistReferencingEncoder
PlistEncoder.swift
Source:PlistEncoder.swift
...227 defer { self.codingPath.removeLast() }228 return _PlistUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)229 }230 public mutating func superEncoder() -> Encoder {231 return _PlistReferencingEncoder(referencing: self.encoder, at: _PlistKey.super, wrapping: self.container)232 }233 public mutating func superEncoder(forKey key: Key) -> Encoder {234 return _PlistReferencingEncoder(referencing: self.encoder, at: key, wrapping: self.container)235 }236}237fileprivate struct _PlistUnkeyedEncodingContainer : UnkeyedEncodingContainer {238 // MARK: Properties239 /// A reference to the encoder we're writing to.240 private let encoder: _PlistEncoder241 /// A reference to the container we're writing to.242 private let container: NSMutableArray243 /// The path of coding keys taken to get to this point in encoding.244 private(set) public var codingPath: [CodingKey]245 /// The number of elements encoded into the container.246 public var count: Int {247 return self.container.count248 }249 // MARK: - Initialization250 /// Initializes `self` with the given references.251 fileprivate init(referencing encoder: _PlistEncoder, codingPath: [CodingKey], wrapping container: NSMutableArray) {252 self.encoder = encoder253 self.codingPath = codingPath254 self.container = container255 }256 // MARK: - UnkeyedEncodingContainer Methods257 public mutating func encodeNil() throws { self.container.add(_plistNullNSString) }258 public mutating func encode(_ value: Bool) throws { self.container.add(self.encoder.box(value)) }259 public mutating func encode(_ value: Int) throws { self.container.add(self.encoder.box(value)) }260 public mutating func encode(_ value: Int8) throws { self.container.add(self.encoder.box(value)) }261 public mutating func encode(_ value: Int16) throws { self.container.add(self.encoder.box(value)) }262 public mutating func encode(_ value: Int32) throws { self.container.add(self.encoder.box(value)) }263 public mutating func encode(_ value: Int64) throws { self.container.add(self.encoder.box(value)) }264 public mutating func encode(_ value: UInt) throws { self.container.add(self.encoder.box(value)) }265 public mutating func encode(_ value: UInt8) throws { self.container.add(self.encoder.box(value)) }266 public mutating func encode(_ value: UInt16) throws { self.container.add(self.encoder.box(value)) }267 public mutating func encode(_ value: UInt32) throws { self.container.add(self.encoder.box(value)) }268 public mutating func encode(_ value: UInt64) throws { self.container.add(self.encoder.box(value)) }269 public mutating func encode(_ value: Float) throws { self.container.add(self.encoder.box(value)) }270 public mutating func encode(_ value: Double) throws { self.container.add(self.encoder.box(value)) }271 public mutating func encode(_ value: String) throws { self.container.add(self.encoder.box(value)) }272 public mutating func encode<T : Encodable>(_ value: T) throws {273 self.encoder.codingPath.append(_PlistKey(index: self.count))274 defer { self.encoder.codingPath.removeLast() }275 self.container.add(try self.encoder.box(value))276 }277 public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> {278 self.codingPath.append(_PlistKey(index: self.count))279 defer { self.codingPath.removeLast() }280 let dictionary = NSMutableDictionary()281 self.container.add(dictionary)282 let container = _PlistKeyedEncodingContainer<NestedKey>(referencing: self.encoder, codingPath: self.codingPath, wrapping: dictionary)283 return KeyedEncodingContainer(container)284 }285 public mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {286 self.codingPath.append(_PlistKey(index: self.count))287 defer { self.codingPath.removeLast() }288 let array = NSMutableArray()289 self.container.add(array)290 return _PlistUnkeyedEncodingContainer(referencing: self.encoder, codingPath: self.codingPath, wrapping: array)291 }292 public mutating func superEncoder() -> Encoder {293 return _PlistReferencingEncoder(referencing: self.encoder, at: self.container.count, wrapping: self.container)294 }295}296extension _PlistEncoder : SingleValueEncodingContainer {297 // MARK: - SingleValueEncodingContainer Methods298 private func assertCanEncodeNewValue() {299 precondition(self.canEncodeNewValue, "Attempt to encode value through single value container when previously value already encoded.")300 }301 public func encodeNil() throws {302 assertCanEncodeNewValue()303 self.storage.push(container: _plistNullNSString)304 }305 public func encode(_ value: Bool) throws {306 assertCanEncodeNewValue()307 self.storage.push(container: self.box(value))308 }309 public func encode(_ value: Int) throws {310 assertCanEncodeNewValue()311 self.storage.push(container: self.box(value))312 }313 public func encode(_ value: Int8) throws {314 assertCanEncodeNewValue()315 self.storage.push(container: self.box(value))316 }317 public func encode(_ value: Int16) throws {318 assertCanEncodeNewValue()319 self.storage.push(container: self.box(value))320 }321 public func encode(_ value: Int32) throws {322 assertCanEncodeNewValue()323 self.storage.push(container: self.box(value))324 }325 public func encode(_ value: Int64) throws {326 assertCanEncodeNewValue()327 self.storage.push(container: self.box(value))328 }329 public func encode(_ value: UInt) throws {330 assertCanEncodeNewValue()331 self.storage.push(container: self.box(value))332 }333 public func encode(_ value: UInt8) throws {334 assertCanEncodeNewValue()335 self.storage.push(container: self.box(value))336 }337 public func encode(_ value: UInt16) throws {338 assertCanEncodeNewValue()339 self.storage.push(container: self.box(value))340 }341 public func encode(_ value: UInt32) throws {342 assertCanEncodeNewValue()343 self.storage.push(container: self.box(value))344 }345 public func encode(_ value: UInt64) throws {346 assertCanEncodeNewValue()347 self.storage.push(container: self.box(value))348 }349 public func encode(_ value: String) throws {350 assertCanEncodeNewValue()351 self.storage.push(container: self.box(value))352 }353 public func encode(_ value: Float) throws {354 assertCanEncodeNewValue()355 self.storage.push(container: self.box(value))356 }357 public func encode(_ value: Double) throws {358 assertCanEncodeNewValue()359 self.storage.push(container: self.box(value))360 }361 public func encode<T : Encodable>(_ value: T) throws {362 assertCanEncodeNewValue()363 try self.storage.push(container: self.box(value))364 }365}366// MARK: - Concrete Value Representations367extension _PlistEncoder {368 /// Returns the given value boxed in a container appropriate for pushing onto the container stack.369 fileprivate func box(_ value: Bool) -> NSObject { return NSNumber(value: value) }370 fileprivate func box(_ value: Int) -> NSObject { return NSNumber(value: value) }371 fileprivate func box(_ value: Int8) -> NSObject { return NSNumber(value: value) }372 fileprivate func box(_ value: Int16) -> NSObject { return NSNumber(value: value) }373 fileprivate func box(_ value: Int32) -> NSObject { return NSNumber(value: value) }374 fileprivate func box(_ value: Int64) -> NSObject { return NSNumber(value: value) }375 fileprivate func box(_ value: UInt) -> NSObject { return NSNumber(value: value) }376 fileprivate func box(_ value: UInt8) -> NSObject { return NSNumber(value: value) }377 fileprivate func box(_ value: UInt16) -> NSObject { return NSNumber(value: value) }378 fileprivate func box(_ value: UInt32) -> NSObject { return NSNumber(value: value) }379 fileprivate func box(_ value: UInt64) -> NSObject { return NSNumber(value: value) }380 fileprivate func box(_ value: Float) -> NSObject { return NSNumber(value: value) }381 fileprivate func box(_ value: Double) -> NSObject { return NSNumber(value: value) }382 fileprivate func box(_ value: String) -> NSObject { return NSString(string: value) }383 fileprivate func box<T : Encodable>(_ value: T) throws -> NSObject {384 return try self.box_(value) ?? NSDictionary()385 }386 fileprivate func box_<T : Encodable>(_ value: T) throws -> NSObject? {387 if T.self == Date.self || T.self == NSDate.self {388 // PropertyListSerialization handles NSDate directly.389 return (value as! NSDate)390 } else if T.self == Data.self || T.self == NSData.self {391 // PropertyListSerialization handles NSData directly.392 return (value as! NSData)393 }394 // The value should request a container from the _PlistEncoder.395 let depth = self.storage.count396 do {397 try value.encode(to: self)398 } catch let error {399 // If the value pushed a container before throwing, pop it back off to restore state.400 if self.storage.count > depth {401 let _ = self.storage.popContainer()402 }403 throw error404 }405 // The top container should be a new container.406 guard self.storage.count > depth else {407 return nil408 }409 return self.storage.popContainer()410 }411}412// MARK: - _PlistReferencingEncoder413/// _PlistReferencingEncoder is a special subclass of _PlistEncoder which has its own storage, but references the contents of a different encoder.414/// It's used in superEncoder(), which returns a new encoder for encoding a superclass -- the lifetime of the encoder should not escape the scope it's created in, but it doesn't necessarily know when it's done being used (to write to the original container).415fileprivate class _PlistReferencingEncoder : _PlistEncoder {416 // MARK: Reference types.417 /// The type of container we're referencing.418 private enum Reference {419 /// Referencing a specific index in an array container.420 case array(NSMutableArray, Int)421 /// Referencing a specific key in a dictionary container.422 case dictionary(NSMutableDictionary, String)423 }424 // MARK: - Properties425 /// The encoder we're referencing.426 private let encoder: _PlistEncoder427 /// The container reference itself.428 private let reference: Reference429 // MARK: - Initialization...
_PlistReferencingEncoder
Using AI Code Generation
1import XCTest2import SnapshotTesting3class _PlistReferencingEncoderTests: XCTestCase {4 func test() {5 let data = try! JSONEncoder().encode(Example())6 let plist = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)7 let encoder = _PlistReferencingEncoder()8 let encoded = try! encoder.encode(plist)9 assertSnapshot(matching: encoded, as: .lines)10 }11}12struct Example: Encodable {13}14import XCTest15import SnapshotTesting16class _PlistReferencingEncoderTests: XCTestCase {17 func test() {18 let data = try! JSONEncoder().encode(Example())19 let plist = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)20 let encoder = _PlistReferencingEncoder()21 let encoded = try! encoder.encode(plist)22 assertSnapshot(matching: encoded, as: .lines)23 }24}25struct Example: Encodable {26}27import XCTest28import SnapshotTesting29class _PlistReferencingEncoderTests: XCTestCase {30 func test() {31 let data = try! JSONEncoder().encode(Example())32 let plist = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)33 let encoder = _PlistReferencingEncoder()34 let encoded = try! encoder.encode(plist)35 assertSnapshot(matching: encoded, as: .lines)36 }37}38struct Example: Encodable {39}40import XCTest41import SnapshotTesting42class _PlistReferencingEncoderTests: XCTestCase {43 func test() {44 let data = try! JSONEncoder().encode(Example())45 let plist = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)46 let encoder = _PlistReferencingEncoder()
_PlistReferencingEncoder
Using AI Code Generation
1import SnapshotTesting2import XCTest3class TestClass: XCTestCase {4 func testExample() {5 let encoder = _PlistReferencingEncoder()6 let data = try! encoder.encode(value)7 let string = String(data: data, encoding: .utf8)!8 print(string)9 }10}11import SnapshotTesting12import XCTest13class TestClass: XCTestCase {14 func testExample() {15 let encoder = _XMLEncoder()16 let data = try! encoder.encode(value)17 let string = String(data: data, encoding: .utf8)!18 print(string)19 }20}21import SnapshotTesting22import XCTest23class TestClass: XCTestCase {24 func testExample() {25 let encoder = _JSONEncoder()26 let data = try! encoder.encode(value)27 let string = String(data: data, encoding: .utf8)!28 print(string)29 }30}31import SnapshotTesting32import XCTest33class TestClass: XCTestCase {34 func testExample() {35 let encoder = _PropertyListEncoder()36 let data = try! encoder.encode(value)37 let string = String(data: data, encoding: .utf8)!38 print(string)39 }40}41import SnapshotTesting42import XCTest43class TestClass: XCTestCase {44 func testExample() {
_PlistReferencingEncoder
Using AI Code Generation
1import XCTest2import SnapshotTesting3class SnapshotTests: XCTestCase {4 func test() {5 assertSnapshot(matching: value, as: .json)6 }7}8import XCTest9import SnapshotTesting10class SnapshotTests: XCTestCase {11 func test() {12 assertSnapshot(matching: value, as: .json)13 }14}15import XCTest16import SnapshotTesting17class SnapshotTests: XCTestCase {18 func test() {19 assertSnapshot(matching: value, as: .json)20 }21}22/Users/username/Downloads/Swift-snapshot-testing/1.swift:8: error: -[SnapshotTests.SnapshotTests test] : failed - Snapshot comparison failed: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})23 Executed 1 test, with 1 failure (0 unexpected) in 0.001 (0.001) seconds
_PlistReferencingEncoder
Using AI Code Generation
1let encoder = _PlistReferencingEncoder()2let result = try encoder.encode(["a": "b"])3let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)4print(plist)5let encoder = PropertyListEncoder()6let result = try encoder.encode(["a": "b"])7let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)8print(plist)9let encoder = _PlistReferencingEncoder()10let result = try encoder.encode(["a": "b"])11let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)12print(plist)13let encoder = PropertyListEncoder()14let result = try encoder.encode(["a": "b"])15let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)16print(plist)17let encoder = _PlistReferencingEncoder()18let result = try encoder.encode(["a": "b"])19let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)20print(plist)21let encoder = PropertyListEncoder()22let result = try encoder.encode(["a": "b"])23let plist = try PropertyListSerialization.propertyList(from: result, options: [], format: nil)24print(plist)25let encoder = _PlistReferencingEncoder()26let result = try encoder.encode(["a": "b"])
_PlistReferencingEncoder
Using AI Code Generation
1import Foundation2import XCTest3import SnapshotTesting4import _SwiftSnapshotTesting5class SwiftSnapshotTestingTests: XCTestCase {6 func testPlistEncoding() {7 let encoder = _PlistReferencingEncoder()8 let plist = try! encoder.encode(["hello": "world"])9 assertSnapshot(matching: plist, as: .plist)10 }11}12import Foundation13import XCTest14import SnapshotTesting15import _SwiftSnapshotTesting16class SwiftSnapshotTestingTests: XCTestCase {17 func testPlistEncoding() {18 let encoder = _PlistReferencingEncoder()19 let plist = try! encoder.encode(["hello": "world"])20 assertSnapshot(matching: plist, as: .plist)21 }22}23import Foundation24import XCTest25import SnapshotTesting26import _SwiftSnapshotTesting27class SwiftSnapshotTestingTests: XCTestCase {28 func testPlistEncoding() {29 let encoder = _PlistReferencingEncoder()30 let plist = try! encoder.encode(["hello": "world"])31 assertSnapshot(matching: plist, as: .plist)32 }33}34import Foundation35import XCTest36import SnapshotTesting37import _SwiftSnapshotTesting38class SwiftSnapshotTestingTests: XCTestCase {39 func testPlistEncoding() {40 let encoder = _PlistReferencingEncoder()41 let plist = try! encoder.encode(["hello": "world"])42 assertSnapshot(matching: plist, as: .plist)43 }44}45import Foundation46import XCTest47import SnapshotTesting48import _SwiftSnapshotTesting49class SwiftSnapshotTestingTests: XCTestCase {50 func testPlistEncoding() {51 let encoder = _PlistReferencingEncoder()52 let plist = try! encoder.encode(["hello": "world"])53 assertSnapshot(matching: plist, as: .plist)54 }55}56import Foundation57import XCTest58import SnapshotTesting59import _SwiftSnapshot
_PlistReferencingEncoder
Using AI Code Generation
1let encoder = _PlistReferencingEncoder()2let plist = try! encoder.encode(StructToEncode())3let data = try! PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0)4try! data.write(to: URL(fileURLWithPath: "/path/to/file.plist"))5let decoder = _PlistReferencingDecoder()6let plist = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)7let decodedStruct = try! decoder.decode(StructToDecode.self, from: plist)8let data = try! PropertyListSerialization.data(fromPropertyList: plist, format: .binary, options: 0)
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!!