Skip to content

Commit

Permalink
Merge pull request #6 from mattpolzin/all-types
Browse files Browse the repository at this point in the history
Add `allTypes` accessor
  • Loading branch information
mattpolzin authored Mar 5, 2020
2 parents 0c9c082 + f1c3a51 commit 9031b84
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 9 deletions.
8 changes: 1 addition & 7 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ import PackageDescription
let package = Package(
name: "Poly",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Poly",
targets: ["Poly"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
dependencies: [],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Poly",
dependencies: []),
Expand Down
3 changes: 1 addition & 2 deletions Poly.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
#

spec.name = "Poly"
spec.version = "2.3.1"
spec.version = "2.4.0"
spec.summary = "A general-purpose library to help represent situations where the type of a value is one of a set of types."

# This description is used to generate tags and improve search results.
Expand Down Expand Up @@ -134,6 +134,5 @@ Pod::Spec.new do |spec|
# spec.requires_arc = true

# spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
# spec.dependency "Poly", "~> 1.0"

end
54 changes: 54 additions & 0 deletions Sources/Poly/Poly+AllTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// Poly+AllTypes.swift
//
//
// Created by Mathew Polzin on 3/5/20.
//

extension Poly0 {
public static var allTypes: [Any.Type] { [] }
}

extension Poly1 {
public static var allTypes: [Any.Type] { [A.self] }
}

extension Poly2 {
public static var allTypes: [Any.Type] { [A.self, B.self] }
}

extension Poly3 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self] }
}

extension Poly4 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self] }
}

extension Poly5 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self] }
}

extension Poly6 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self] }
}

extension Poly7 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self] }
}

extension Poly8 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self] }
}

extension Poly9 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self, I.self] }
}

extension Poly10 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self, I.self, J.self] }
}

extension Poly11 {
public static var allTypes: [Any.Type] { [A.self, B.self, C.self, D.self, E.self, F.self, G.self, H.self, I.self, J.self, K.self] }
}
3 changes: 3 additions & 0 deletions Sources/Poly/Poly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
public protocol Poly {
/// Get a type-erased value.
var value: Any { get }

/// Get a list of all types this `Poly` supports.
static var allTypes: [Any.Type] { get }
}

// MARK: - 0 types
Expand Down
161 changes: 161 additions & 0 deletions Tests/PolyTests/Poly+AllTypesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//
// Poly+AllTypesTests.swift
//
//
// Created by Mathew Polzin on 3/5/20.
//

import XCTest
import Poly

final class PolyAllTypesTests: XCTestCase {
func test_poly0() {
XCTAssertTrue(Poly0.allTypes.isEmpty)
}

func test_poly1() {
XCTAssert(Poly1<TestType1>.allTypes[0] == TestType1.self)
}

func test_poly2() {
XCTAssert(Poly2<TestType1, TestType2>.allTypes[0] == TestType1.self)
XCTAssert(Poly2<TestType1, TestType2>.allTypes[1] == TestType2.self)
}

func test_poly3() {
XCTAssert(Poly3<TestType1, TestType2, TestType3>.allTypes[0] == TestType1.self)
XCTAssert(Poly3<TestType1, TestType2, TestType3>.allTypes[1] == TestType2.self)
XCTAssert(Poly3<TestType1, TestType2, TestType3>.allTypes[2] == TestType3.self)
}

func test_poly4() {
XCTAssert(Poly4<TestType1, TestType2, TestType3, TestType4>.allTypes[0] == TestType1.self)
XCTAssert(Poly4<TestType1, TestType2, TestType3, TestType4>.allTypes[1] == TestType2.self)
XCTAssert(Poly4<TestType1, TestType2, TestType3, TestType4>.allTypes[2] == TestType3.self)
XCTAssert(Poly4<TestType1, TestType2, TestType3, TestType4>.allTypes[3] == TestType4.self)
}

func test_poly5() {
XCTAssert(Poly5<TestType1, TestType2, TestType3, TestType4, TestType5>.allTypes[0] == TestType1.self)
XCTAssert(Poly5<TestType1, TestType2, TestType3, TestType4, TestType5>.allTypes[1] == TestType2.self)
XCTAssert(Poly5<TestType1, TestType2, TestType3, TestType4, TestType5>.allTypes[2] == TestType3.self)
XCTAssert(Poly5<TestType1, TestType2, TestType3, TestType4, TestType5>.allTypes[3] == TestType4.self)
XCTAssert(Poly5<TestType1, TestType2, TestType3, TestType4, TestType5>.allTypes[4] == TestType5.self)
}

func test_poly6() {
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[0] == TestType1.self)
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[1] == TestType2.self)
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[2] == TestType3.self)
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[3] == TestType4.self)
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[4] == TestType5.self)
XCTAssert(Poly6<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6>.allTypes[5] == TestType6.self)
}

func test_poly7() {
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[0] == TestType1.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[1] == TestType2.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[2] == TestType3.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[3] == TestType4.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[4] == TestType5.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[5] == TestType6.self)
XCTAssert(Poly7<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7>.allTypes[6] == TestType7.self)
}

func test_poly8() {
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[0] == TestType1.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[1] == TestType2.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[2] == TestType3.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[3] == TestType4.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[4] == TestType5.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[5] == TestType6.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[6] == TestType7.self)
XCTAssert(Poly8<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8>.allTypes[7] == TestType8.self)
}

func test_poly9() {
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[0] == TestType1.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[1] == TestType2.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[2] == TestType3.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[3] == TestType4.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[4] == TestType5.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[5] == TestType6.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[6] == TestType7.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[7] == TestType8.self)
XCTAssert(Poly9<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9>.allTypes[8] == TestType9.self)
}

func test_poly10() {
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[0] == TestType1.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[1] == TestType2.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[2] == TestType3.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[3] == TestType4.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[4] == TestType5.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[5] == TestType6.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[6] == TestType7.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[7] == TestType8.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[8] == TestType9.self)
XCTAssert(Poly10<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10>.allTypes[9] == TestType10.self)
}

func test_poly11() {
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[0] == TestType1.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[1] == TestType2.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[2] == TestType3.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[3] == TestType4.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[4] == TestType5.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[5] == TestType6.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[6] == TestType7.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[7] == TestType8.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[8] == TestType9.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[9] == TestType10.self)
XCTAssert(Poly11<TestType1, TestType2, TestType3, TestType4, TestType5, TestType6, TestType7, TestType8, TestType9, TestType10, TestType11>.allTypes[10] == TestType11.self)
}
}

// MARK: - Test types
extension PolyAllTypesTests {
struct TestType1: Codable, Equatable {
let a: Int
}

struct TestType2: Codable, Equatable {
let b: Int
}

struct TestType3: Codable, Equatable {
let c: Int
}

struct TestType4: Codable, Equatable {
let d: Int
}

struct TestType5: Codable, Equatable {
let e: Int
}

struct TestType6: Codable, Equatable {
let f: Int
}

struct TestType7: Codable, Equatable {
let g: Int
}

struct TestType8: Codable, Equatable {
let h: Int
}

struct TestType9: Codable, Equatable {
let i: Int
}

struct TestType10: Codable, Equatable {
let j: Int
}

struct TestType11: Codable, Equatable {
let k: Int
}
}

0 comments on commit 9031b84

Please sign in to comment.