Skip to content

Commit

Permalink
Address comments #1 cont
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-hui committed Jun 18, 2019
1 parent 909f30e commit 3676ff4
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 87 deletions.
Binary file added .vscode/ipch/11bd5f77b4f6e078/FSTAPIHelpers.ipch
Binary file not shown.
Binary file added .vscode/ipch/11bd5f77b4f6e078/mmap_address.bin
Binary file not shown.
18 changes: 5 additions & 13 deletions Firestore/Example/Firestore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
113190791F42202FDE1ABC14 /* FIRQuerySnapshotTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E04F202154AA00B64F25 /* FIRQuerySnapshotTests.mm */; };
11F8EE69182C9699E90A9E3D /* database_info_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D92E20235D22000A432D /* database_info_test.cc */; };
12158DFCEE09D24B7988A340 /* maybe_document.pb.cc in Sources */ = {isa = PBXBuildFile; fileRef = 618BBE7E20B89AAC00B5BCE7 /* maybe_document.pb.cc */; };
1235769522B86E65007DDFA9 /* FirestoreEncoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1235769422B86E65007DDFA9 /* FirestoreEncoderTests.swift */; };
127CC0D222B3ADDC00A3E42A /* CodableTimestampTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B65C996438B84DBC7616640 /* CodableTimestampTests.swift */; };
127CC0D322B3ADE400A3E42A /* CodableDocumentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C024C236FE145FE79EE50E5 /* CodableDocumentTests.swift */; };
1291D9F5300AFACD1FBD262D /* array_sorted_map_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54EB764C202277B30088B8F3 /* array_sorted_map_test.cc */; };
12BB9ED1CA98AA52B92F497B /* log_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = 54C2294E1FECABAE007D065B /* log_test.cc */; };
12DB753599571E24DCED0C2C /* FIRValidationTests.mm in Sources */ = {isa = PBXBuildFile; fileRef = 5492E06D202154D600B64F25 /* FIRValidationTests.mm */; };
Expand Down Expand Up @@ -759,6 +759,7 @@
0473AFFF5567E667A125347B /* ordered_code_benchmark.cc */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.cpp; path = ordered_code_benchmark.cc; sourceTree = "<group>"; };
0EE5300F8233D14025EF0456 /* string_apple_test.mm */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.cpp.objcpp; path = string_apple_test.mm; sourceTree = "<group>"; };
11984BA0A99D7A7ABA5B0D90 /* Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS/Pods-Firestore_Example_iOS-Firestore_SwiftTests_iOS.release.xcconfig"; sourceTree = "<group>"; };
1235769422B86E65007DDFA9 /* FirestoreEncoderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FirestoreEncoderTests.swift; sourceTree = "<group>"; };
1277F98C20D2DF0867496976 /* Pods-Firestore_IntegrationTests_iOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_IntegrationTests_iOS.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_IntegrationTests_iOS/Pods-Firestore_IntegrationTests_iOS.debug.xcconfig"; sourceTree = "<group>"; };
12F4357299652983A615F886 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
132E32997D781B896672D30A /* reference_set_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = reference_set_test.cc; sourceTree = "<group>"; };
Expand Down Expand Up @@ -921,7 +922,6 @@
54E9282A1F339CAD00C1953E /* XCTestCase+Await.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "XCTestCase+Await.h"; sourceTree = "<group>"; };
54EB764C202277B30088B8F3 /* array_sorted_map_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = array_sorted_map_test.cc; sourceTree = "<group>"; };
5918805E993304321A05E82B /* Pods_Firestore_Example_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Firestore_Example_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5C024C236FE145FE79EE50E5 /* CodableDocumentTests.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; path = CodableDocumentTests.swift; sourceTree = "<group>"; };
5CAE131920FFFED600BE9A4A /* Firestore_Benchmarks_iOS.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Firestore_Benchmarks_iOS.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5CAE131D20FFFED600BE9A4A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5CC9650120A0E93200A2D6A1 /* FSTLRUGarbageCollectorTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FSTLRUGarbageCollectorTests.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1206,14 +1206,6 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
3C67A5E0CE90E64676E39899 /* . */ = {
isa = PBXGroup;
children = (
620C1427763BA5D3CCFB5A1F /* BridgingHeader.h */,
);
name = .;
sourceTree = "<group>";
};
543B4F0520A91E4B001F506D /* App */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1368,7 +1360,7 @@
5495EB012040E90200EBA509 /* Codable */ = {
isa = PBXGroup;
children = (
5C024C236FE145FE79EE50E5 /* CodableDocumentTests.swift */,
1235769422B86E65007DDFA9 /* FirestoreEncoderTests.swift */,
5495EB022040E90200EBA509 /* CodableGeoPointTests.swift */,
7B65C996438B84DBC7616640 /* CodableTimestampTests.swift */,
);
Expand Down Expand Up @@ -1408,7 +1400,7 @@
54C9EDF22040E16300A969CD /* SwiftTests */ = {
isa = PBXGroup;
children = (
3C67A5E0CE90E64676E39899 /* . */,
620C1427763BA5D3CCFB5A1F /* BridgingHeader.h */,
544A20ED20F6C046004E52CD /* API */,
5495EB012040E90200EBA509 /* Codable */,
54C9EDF52040E16300A969CD /* Info.plist */,
Expand Down Expand Up @@ -3414,9 +3406,9 @@
buildActionMask = 2147483647;
files = (
544A20EE20F6C10C004E52CD /* BasicCompileTests.swift in Sources */,
127CC0D322B3ADE400A3E42A /* CodableDocumentTests.swift in Sources */,
5495EB032040E90200EBA509 /* CodableGeoPointTests.swift in Sources */,
127CC0D222B3ADDC00A3E42A /* CodableTimestampTests.swift in Sources */,
1235769522B86E65007DDFA9 /* FirestoreEncoderTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ extension Firestore {
public func decode<T: Decodable>(_: T.Type, from container: [String: Any]) throws -> T {
let decoder = _FirestoreDecoder(referencing: container)
guard let value = try decoder.unbox(container, as: T.self) else {
throw DecodingError.valueNotFound(T.self, DecodingError.Context(codingPath: [], debugDescription: "The given dictionary was invalid"))
throw DecodingError.valueNotFound(
T.self,
DecodingError.Context(codingPath: [],
debugDescription: "The given dictionary was invalid")
)
}
return value
}
Expand All @@ -46,9 +50,10 @@ class _FirestoreDecoder: Decoder {

// MARK: Properties

/// A stack of data containers storing data containers to decode. When a new data container is being decoded,
/// a corresponding storage is pushed to the stack; and when that container (and all of its children containers)
/// has been decoded, it is poped out such that the decoding can proceed with new top storage.
/// A stack of data containers storing data containers to decode. When a new data
/// container is being decoded, a corresponding storage is pushed to the stack;
/// and when that container (and all of its children containers) has been decoded,
/// it is popped out such that the decoding can proceed with new top storage.
fileprivate var storage: _FirestoreDecodingStorage

/// The path to the current point in the container tree. Given the root container, one could
Expand Down
8 changes: 4 additions & 4 deletions Firestore/Swift/Tests/BridgingHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

#ifndef BridgingHeader_h
#define BridgingHeader_h
#ifndef FIRESTORE_SWIFT_TESTS_BRIDGINGHEADER_H_
#define FIRESTORE_SWIFT_TESTS_BRIDGINGHEADER_H_

#import "FSTAPIHelpers.h"
#import "Firestore/Example/Tests/Util/FSTAPIHelpers.h"

#endif /* BridgingHeader_h */
#endif /* FIRESTORE_SWIFT_TESTS_BRIDGINGHEADER_H_ */
2 changes: 1 addition & 1 deletion Firestore/Swift/Tests/Codable/CodableGeoPointTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Google
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
17 changes: 10 additions & 7 deletions Firestore/Swift/Tests/Codable/CodableTimestampTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class CodableTimestampTests: XCTestCase {
func testTimestampEncodes() {
let timestamp = Timestamp(seconds: 37, nanoseconds: 123)

let jsonData = try! JSONEncoder().encode(timestamp)
let json = String(data: jsonData, encoding: .utf8)!
do {
let jsonData = try JSONEncoder().encode(timestamp)
let json = String(data: jsonData, encoding: .utf8)!

// The ordering of attributes in the JSON output is not guaranteed, nor is the rounding of
// the values so just verify that each required property is present and that the value
// starts as expected.
XCTAssert(json.contains("\"seconds\":37"))
XCTAssert(json.contains("\"nanoseconds\":123"))
// The ordering of attributes in the JSON output is not guaranteed, so just verify that
// each required property is present.
XCTAssert(json.contains("\"seconds\":37"))
XCTAssert(json.contains("\"nanoseconds\":123"))
} catch {
XCTFail("Error: \(error)")
}
}

func testTimestampDecodes() {
Expand Down
73 changes: 15 additions & 58 deletions Firestore/Swift/Tests/Codable/FirestoreEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import Foundation
import FirebaseFirestore
@testable import FirebaseFirestoreSwift
import FirebaseFirestoreSwift
import XCTest

private func assertRoundTrip<X: Equatable & Codable>(model: X, encoded: [String: Any]) -> Void {
Expand Down Expand Up @@ -62,7 +62,7 @@ private func assertDecodingThrows<X: Equatable & Codable>(_ model: [String: Any]
XCTFail("Failed to throw")
}

class CodableDocumentTests: XCTestCase {
class FirestoreEncoderTests: XCTestCase {
func testInt() {
struct Model: Codable, Equatable {
let x: Int
Expand All @@ -77,33 +77,13 @@ class CodableDocumentTests: XCTestCase {
_ = assertEncodes(Model(), encoded: [String: Any]())
}

func testNil() {
func testString() {
struct Model: Codable, Equatable {
let x: Int?
}
let model = Model(x: nil)
let dict = ["x": nil] as [String: Int?]
let encodedDict = try! Firestore.Encoder().encode(model)
XCTAssertNil(encodedDict["x"])
let model2 = try? Firestore.Decoder().decode(Model.self, from: dict as [String: Any])
XCTAssertNil(model2)
}

func testIntNilString() {
struct Model: Codable, Equatable {
let i: Int
let x: Int?
let s: String
}
let model = Model(i: 7, x: nil, s: "abc")
let model = Model(s: "abc")
let encodedDict = try! Firestore.Encoder().encode(model)
XCTAssertNil(encodedDict["x"])
XCTAssertTrue(encodedDict.keys.contains("i"))

// TODO: - handle encoding keys with nil values
// See https://stackoverflow.com/questions/47266862/encode-nil-value-as-null-with-jsonencoder
// and https://bugs.swift.org/browse/SR-9232
// XCTAssertTrue(encodedDict.keys.contains("x"))
XCTAssertEqual(encodedDict["s"] as! String, "abc")
}

func testOptional() {
Expand All @@ -118,6 +98,11 @@ class CodableDocumentTests: XCTestCase {
assertDecodingThrows(["x": 42, "opt": "abc"], encoded: Model(x: 42, opt: nil))
assertDecodingThrows(["x": 45.55, "opt": 5], encoded: Model(x: 42, opt: nil))
assertDecodingThrows(["opt": 5], encoded: Model(x: 42, opt: nil))

// TODO: - handle encoding keys with nil values
// See https://stackoverflow.com/questions/47266862/encode-nil-value-as-null-with-jsonencoder
// and https://bugs.swift.org/browse/SR-9232
// XCTAssertTrue(encodedDict.keys.contains("x"))
}

func testEnum() {
Expand Down Expand Up @@ -197,7 +182,7 @@ class CodableDocumentTests: XCTestCase {
assertRoundTrip(model: model, encoded: ["date": date])
}

// Uncomment if we decide to reenable embedded DocumentReference's
// Uncomment when we decide to reenable embedded DocumentReference's
//
// func testDocumentReference() {
// struct Model: Codable, Equatable {
Expand Down Expand Up @@ -303,84 +288,56 @@ class CodableDocumentTests: XCTestCase {
assertRoundTrip(model: model, encoded: dict)
}

func testCodingKeys() {
func testCodingKeysCanCustomizeEncodingAndDecoding() {
struct Model: Codable, Equatable {
var s: String
var ms: String
var d: Double
var md: Double
var i: Int
var mi: Int
var b: Bool
var mb: Bool

// Use CodingKeys to only encode part of the struct.
enum CodingKeys: String, CodingKey {
case s
case d
case i
case b
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
s = try values.decode(String.self, forKey: .s)
d = try values.decode(Double.self, forKey: .d)
i = try values.decode(Int.self, forKey: .i)
b = try values.decode(Bool.self, forKey: .b)
ms = "filler"
md = 42.42
mi = -9
mb = false
}

public init(ins: String, inms: String, ind: Double, inmd: Double, ini: Int, inmi: Int, inb: Bool, inmb: Bool) {
public init(ins: String, inms: String, ind: Double, inmd: Double) {
s = ins
d = ind
i = ini
b = inb
ms = inms
md = inmd
mi = inmi
mb = inmb
}
}
let model = Model(
ins: "abc",
inms: "dummy",
ind: 123.3,
inmd: 0,
ini: -4444,
inmi: 0,
inb: true,
inmb: true
inmd: 0
)
let dict = [
"s": "abc",
"d": 123.3,
"i": -4444,
"b": true,
] as [String: Any]

let model2 = try! Firestore.Decoder().decode(Model.self, from: dict)
XCTAssertEqual(model.s, model2.s)
XCTAssertEqual(model.d, model2.d)
XCTAssertEqual(model.i, model2.i)
XCTAssertEqual(model.b, model2.b)
XCTAssertEqual(model2.ms, "filler")
XCTAssertEqual(model2.md, 42.42)
XCTAssertEqual(model2.mi, -9)
XCTAssertEqual(model2.mb, false)

let encodedDict = try! Firestore.Encoder().encode(model)
XCTAssertEqual(encodedDict["s"] as! String, "abc")
XCTAssertEqual(encodedDict["d"] as! Double, 123.3)
XCTAssertEqual(encodedDict["i"] as! Int, -4444)
XCTAssertEqual(encodedDict["b"] as! Bool, true)
XCTAssertNil(encodedDict["ms"])
XCTAssertNil(encodedDict["md"])
XCTAssertNil(encodedDict["mi"])
XCTAssertNil(encodedDict["mb"])
}

func testNestedObjects() {
Expand Down Expand Up @@ -541,7 +498,7 @@ class CodableDocumentTests: XCTestCase {
assertRoundTrip(model: model, encoded: dict)
}

func testEcodingEncodableArrayNotSupported() {
func testEncodingEncodableArrayNotSupported() {
struct Model: Codable, Equatable {
var name: String
}
Expand Down

0 comments on commit 3676ff4

Please sign in to comment.