Skip to content

Commit

Permalink
Merge remote-tracking branch 'morten/firebaseswiftshared' into codabl…
Browse files Browse the repository at this point in the history
…e-refactor
  • Loading branch information
paulb777 committed Oct 29, 2021
2 parents aec1dd4 + 9b87b59 commit b900e9c
Show file tree
Hide file tree
Showing 10 changed files with 580 additions and 391 deletions.
23 changes: 23 additions & 0 deletions FirebaseDatabaseSwift/Sources/Codable/EncoderDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import FirebaseDatabase
import FirebaseSharedSwift

extension Database {
public typealias Encoder = StructureEncoder
public typealias Decoder = StructureDecoder
}
111 changes: 111 additions & 0 deletions FirebaseDatabaseSwift/Tests/Codable/ServerValueCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,114 @@ extension CurrencyAmount: ExpressibleByFloatLiteral {
self.value = Decimal(value)
}
}

private func assertThat(_ dictionary: [String: Any],
file: StaticString = #file,
line: UInt = #line) -> DictionarySubject {
return DictionarySubject(dictionary, file: file, line: line)
}

func assertThat<X: Equatable & Codable>(_ model: X, file: StaticString = #file,
line: UInt = #line) -> CodableSubject<X> {
return CodableSubject(model, file: file, line: line)
}

func assertThat<X: Equatable & Encodable>(_ model: X, file: StaticString = #file,
line: UInt = #line) -> EncodableSubject<X> {
return EncodableSubject(model, file: file, line: line)
}

class EncodableSubject<X: Equatable & Encodable> {
var subject: X
var file: StaticString
var line: UInt

init(_ subject: X, file: StaticString, line: UInt) {
self.subject = subject
self.file = file
self.line = line
}

@discardableResult
func encodes(to expected: [String: Any],
using encoder: Database.Encoder = .init()) -> DictionarySubject {
let encoded = assertEncodes(to: expected, using: encoder)
return DictionarySubject(encoded, file: file, line: line)
}

func failsToEncode() {
do {
let encoder = Database.Encoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
_ = try encoder.encode(subject)
} catch {
return
}
XCTFail("Failed to throw")
}

func failsEncodingAtTopLevel() {
do {
let encoder = Database.Encoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
_ = try encoder.encode(subject)
XCTFail("Failed to throw", file: file, line: line)
} catch EncodingError.invalidValue(_, _) {
return
} catch {
XCTFail("Unrecognized error: \(error)", file: file, line: line)
}
}

private func assertEncodes(to expected: [String: Any],
using encoder: Database.Encoder = .init()) -> [String: Any] {
do {
let enc = try encoder.encode(subject)
XCTAssertEqual(enc as? NSDictionary, expected as NSDictionary, file: file, line: line)
return (enc as! NSDictionary) as! [String: Any]
} catch {
XCTFail("Failed to encode \(X.self): error: \(error)")
return ["": -1]
}
}
}

class CodableSubject<X: Equatable & Codable>: EncodableSubject<X> {
func roundTrips(to expected: [String: Any],
using encoder: Database.Encoder = .init(),
decoder: Database.Decoder = .init()) {
let reverseSubject = encodes(to: expected, using: encoder)
reverseSubject.decodes(to: subject, using: decoder)
}
}

class DictionarySubject {
var subject: [String: Any]
var file: StaticString
var line: UInt

init(_ subject: [String: Any], file: StaticString, line: UInt) {
self.subject = subject
self.file = file
self.line = line
}

func decodes<X: Equatable & Codable>(to expected: X,
using decoder: Database.Decoder = .init()) -> Void {
do {
let decoded = try decoder.decode(X.self, from: subject)
XCTAssertEqual(decoded, expected)
} catch {
XCTFail("Failed to decode \(X.self): \(error)", file: file, line: line)
}
}

func failsDecoding<X: Equatable & Codable>(to _: X.Type,
using decoder: Database.Decoder = .init()) -> Void {
XCTAssertThrowsError(
try decoder.decode(X.self, from: subject),
file: file,
line: line
)
}
}
1 change: 1 addition & 0 deletions FirebaseSharedSwift/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit b900e9c

Please sign in to comment.