-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from RougeWare/feature/Autoconformance
Added auto-conformance to the 4 common synthesized protocols
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// LazyContainer + Equatable.swift | ||
// | ||
// | ||
// Created by Ky on 2022-06-03. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
|
||
// MARK: - Encodable | ||
|
||
public extension LazyContainer where Self: Encodable, Value: Encodable { | ||
func encode(to encoder: Encoder) throws { | ||
try wrappedValue.encode(to: encoder) | ||
} | ||
} | ||
|
||
|
||
|
||
extension Lazy: Encodable where Value: Encodable {} | ||
extension ResettableLazy: Encodable where Value: Encodable {} | ||
extension FunctionalLazy: Encodable where Value: Encodable {} | ||
|
||
|
||
|
||
// MARK: - Decodable | ||
|
||
public extension LazyContainer where Self: Decodable, Value: Decodable { | ||
init(from decoder: Decoder) throws { | ||
self = .preinitialized(try Value(from: decoder)) | ||
} | ||
} | ||
|
||
|
||
|
||
extension Lazy: Decodable where Value: Decodable {} | ||
extension ResettableLazy: Decodable where Value: Decodable {} | ||
extension FunctionalLazy: Decodable where Value: Decodable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// LazyContainer + Equatable.swift | ||
// | ||
// | ||
// Created by Ky on 2022-06-03. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
|
||
public extension LazyContainer where Self: Equatable, Value: Equatable { | ||
static func == (lhs: Self, rhs: Self) -> Bool { | ||
lhs.wrappedValue == rhs.wrappedValue | ||
} | ||
|
||
|
||
static func == (lhs: Self, rhs: Value) -> Bool { | ||
lhs.wrappedValue == rhs | ||
} | ||
|
||
|
||
static func == (lhs: Value, rhs: Self) -> Bool { | ||
lhs == rhs.wrappedValue | ||
} | ||
} | ||
|
||
|
||
|
||
extension Lazy: Equatable where Value: Equatable {} | ||
extension ResettableLazy: Equatable where Value: Equatable {} | ||
extension FunctionalLazy: Equatable where Value: Equatable {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// LazyContainer + Hashable.swift | ||
// | ||
// | ||
// Created by Ky on 2022-06-03. | ||
// | ||
|
||
import Foundation | ||
|
||
|
||
|
||
public extension LazyContainer where Self: Hashable, Value: Hashable { | ||
func hash(into hasher: inout Hasher) { | ||
wrappedValue.hash(into: &hasher) | ||
} | ||
} | ||
|
||
|
||
|
||
extension Lazy: Hashable where Value: Hashable {} | ||
extension ResettableLazy: Hashable where Value: Hashable {} | ||
extension FunctionalLazy: Hashable where Value: Hashable {} |
38 changes: 38 additions & 0 deletions
38
Tests/LazyContainersTests/LazyContainer + Codable tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// LazyContainer + Hashable tests.swift | ||
// | ||
// | ||
// Created by S🌟System on 2022-06-03. | ||
// | ||
|
||
import XCTest | ||
|
||
import LazyContainers | ||
|
||
|
||
|
||
final class LazyContainer_Codable_tests: XCTestCase { | ||
|
||
func testHashableConformance() { | ||
|
||
struct Test: Codable { | ||
|
||
@Lazy(initializer: { 42 }) | ||
var lazyInt | ||
|
||
@FunctionalLazy(initializer: { CGFloat.pi }) | ||
var lazyFloat | ||
|
||
@ResettableLazy(initializer: { "foobar" }) | ||
var lazyString | ||
} | ||
|
||
|
||
|
||
let encoder = JSONEncoder() | ||
encoder.outputFormatting = .sortedKeys | ||
|
||
XCTAssertEqual(String(data: try encoder.encode(Test()), encoding: .utf8), | ||
#"{"lazyFloat":3.1415926535897931,"lazyInt":42,"lazyString":"foobar"}"#) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Tests/LazyContainersTests/LazyContainer + Equatable tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// LazyContainer + Equatable tests.swift | ||
// | ||
// | ||
// Created by S🌟System on 2022-06-03. | ||
// | ||
|
||
import XCTest | ||
|
||
import LazyContainers | ||
|
||
|
||
|
||
final class LazyContainer_Equatable_tests: XCTestCase { | ||
|
||
func testEquatableConformance() { | ||
|
||
struct Test: Equatable { | ||
|
||
@Lazy(initializer: { 42 }) | ||
var lazyInt | ||
|
||
@FunctionalLazy(initializer: { CGFloat.pi }) | ||
var lazyFloat | ||
|
||
@ResettableLazy(initializer: { "foobar" }) | ||
var lazyString | ||
} | ||
|
||
XCTAssertEqual(Test(), Test()) | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Tests/LazyContainersTests/LazyContainer + Hashable tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// LazyContainer + Hashable tests.swift | ||
// | ||
// | ||
// Created by S🌟System on 2022-06-03. | ||
// | ||
|
||
import XCTest | ||
|
||
import LazyContainers | ||
|
||
|
||
|
||
final class LazyContainer_Hashable_tests: XCTestCase { | ||
|
||
func testHashableConformance() { | ||
|
||
struct Test: Hashable { | ||
|
||
@Lazy(initializer: { 42 }) | ||
var lazyInt | ||
|
||
@FunctionalLazy(initializer: { CGFloat.pi }) | ||
var lazyFloat | ||
|
||
@ResettableLazy(initializer: { "foobar" }) | ||
var lazyString | ||
} | ||
|
||
XCTAssertEqual(Test().hashValue, Test().hashValue) | ||
} | ||
} |