Skip to content

Commit

Permalink
Fix support for struct with let properties
Browse files Browse the repository at this point in the history
  • Loading branch information
acecilia committed Apr 25, 2020
1 parent f9710e1 commit 2ef624d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Sources/Compose/Compose.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public struct Compose<Element1, Element2> {
set { element2[keyPath: keyPath] = newValue }
}

public subscript<T>(dynamicMember keyPath: KeyPath<Element1, T>) -> T {
element1[keyPath: keyPath]
}

public subscript<T>(dynamicMember keyPath: KeyPath<Element2, T>) -> T {
element2[keyPath: keyPath]
}

public init(_ element1: Element1, _ element2: Element2) {
self.element1 = element1
self.element2 = element2
Expand Down
9 changes: 8 additions & 1 deletion Tests/ComposeTests/ComposeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import Foundation
import Compose

final class ComposeTests: XCTestCase {
func testGetSet() {
func testGet() {
XCTAssertEqual(remoteDeveloper.name, "Andres")
XCTAssertEqual(remoteDeveloper.age, 26)
XCTAssertEqual(remoteDeveloper.country, "Spain")
XCTAssertEqual(remoteDeveloper.city, "Madrid")
}

func testSet() {
let expectedName = "Kike"
let expectedCity = "Segovia"

Expand Down
4 changes: 2 additions & 2 deletions Tests/ComposeTests/TestStructs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Compose

struct Developer: Codable, Hashable {
var name: String
var age: Int
let age: Int
}

struct RemoteLocation: Codable, Hashable {
var country: String
let country: String
var city: String
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/ComposeTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ extension ComposeTests {
static let __allTests__ComposeTests = [
("testCodable", testCodable),
("testEquatable", testEquatable),
("testGetSet", testGetSet),
("testGet", testGet),
("testHashable", testHashable),
("testLocalizedError", testLocalizedError),
("testSet", testSet),
]
}

Expand Down

0 comments on commit 2ef624d

Please sign in to comment.