Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
adding AnyEnum.init
renaming flatten -> unwrapped
adding minimum test cases of unwrapped and map
  • Loading branch information
tarunon committed Jul 19, 2017
1 parent f3bb338 commit cc81050
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 19 deletions.
42 changes: 38 additions & 4 deletions Sources/EnumConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
public enum AnyEnum2<T0, T1> {
case case0(T0)
case case1(T1)

public init<E: Enum2Convertible>(_ source: E)
where T0 == E.T0
, T1 == E.T1
{
self = source.asEnum
}
}

public protocol Enum2Convertible {
Expand All @@ -19,7 +26,7 @@ extension AnyEnum2: Enum2Convertible {
extension Enum2Convertible
where T0 == T1
{
public func flatten() -> T0 {
public func unwrapped() -> T0 {
switch self.asEnum {
case .case0(let x): return x
case .case1(let x): return x
Expand Down Expand Up @@ -48,6 +55,14 @@ public enum AnyEnum3<T0, T1, T2> {
case case0(T0)
case case1(T1)
case case2(T2)

public init<E: Enum3Convertible>(_ source: E)
where T0 == E.T0
, T1 == E.T1
, T2 == E.T2
{
self = source.asEnum
}
}

public protocol Enum3Convertible {
Expand All @@ -67,7 +82,7 @@ extension Enum3Convertible
where T0 == T1
, T0 == T2
{
public func flatten() -> T0 {
public func unwrapped() -> T0 {
switch self.asEnum {
case .case0(let x): return x
case .case1(let x): return x
Expand Down Expand Up @@ -108,6 +123,15 @@ public enum AnyEnum4<T0, T1, T2, T3> {
case case1(T1)
case case2(T2)
case case3(T3)

public init<E: Enum4Convertible>(_ source: E)
where T0 == E.T0
, T1 == E.T1
, T2 == E.T2
, T3 == E.T3
{
self = source.asEnum
}
}

public protocol Enum4Convertible {
Expand All @@ -129,7 +153,7 @@ extension Enum4Convertible
, T0 == T2
, T0 == T3
{
public func flatten() -> T0 {
public func unwrapped() -> T0 {
switch self.asEnum {
case .case0(let x): return x
case .case1(let x): return x
Expand Down Expand Up @@ -184,6 +208,16 @@ public enum AnyEnum5<T0, T1, T2, T3, T4> {
case case2(T2)
case case3(T3)
case case4(T4)

public init<E: Enum5Convertible>(_ source: E)
where T0 == E.T0
, T1 == E.T1
, T2 == E.T2
, T3 == E.T3
, T4 == E.T4
{
self = source.asEnum
}
}

public protocol Enum5Convertible {
Expand All @@ -207,7 +241,7 @@ extension Enum5Convertible
, T0 == T3
, T0 == T4
{
public func flatten() -> T0 {
public func unwrapped() -> T0 {
switch self.asEnum {
case .case0(let x): return x
case .case1(let x): return x
Expand Down
14 changes: 13 additions & 1 deletion Sources/EnumConvertible.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
return ["where " + res[0]] + [", " + x for x in res[1:]]
def mapReturnType(enumNumber, caseNumber):
return "AnyEnum{}<{}>".format(enumNumber, ", ".join(["T{}".format(i) if i != caseNumber else "T" for i in range(enumNumber)]))
def initConstraintN(n):
all = range(n)
res = ["T{} == E.T{}".format(i, i) for i in all]
return ["where " + res[0]] + [", " + x for x in res[1:]]

enumNumbers = range(2, int(MAX_SERIAL_NUMBER) + 1)
if len(SPECIFIC_NUMBERS) > 0:
Expand All @@ -20,6 +24,14 @@ public enum AnyEnum${enumNumber}<${enumerateT(enumNumber)}> {
% for caseNumber in range(enumNumber):
case case${caseNumber}(T${caseNumber})
% end

public init<E: Enum${enumNumber}Convertible>(_ source: E)
% for constraint in initConstraintN(enumNumber):
${constraint}
% end
{
self = source.asEnum
}
}

public protocol Enum${enumNumber}Convertible {
Expand All @@ -40,7 +52,7 @@ extension Enum${enumNumber}Convertible
${constraints}
% end
{
public func flatten() -> T0 {
public func unwrapped() -> T0 {
switch self.asEnum {
% for caseNumber in range(enumNumber):
case .case${caseNumber}(let x): return x
Expand Down
71 changes: 57 additions & 14 deletions Tests/EnumConvertibleTests/EnumConvertibleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ class EnumConvertibleTests: XCTestCase {
func testEnum2() {
let int = 100
let str = "abc"
switch (MyEnum2.int(int).asEnum, MyEnum2.int(int).asEnum.asEnum) {
switch (AnyEnum2(MyEnum2.int(int)), AnyEnum2(AnyEnum2(MyEnum2.int(int)))) {
case (.case0(let x), .case0(let y)) where x == int && x == y:
break
default:
XCTFail()
}
switch (MyEnum2.str(str).asEnum, MyEnum2.str(str).asEnum.asEnum) {
switch (AnyEnum2(MyEnum2.str(str)), AnyEnum2(MyEnum2.str(str))) {
case (.case1(let x), .case1(let y)) where x == str && x == y:
break
default:
Expand All @@ -92,19 +92,19 @@ class EnumConvertibleTests: XCTestCase {
let int = 100
let str = "abc"
let double = 3.14
switch (MyEnum3.int(int).asEnum, MyEnum3.int(int).asEnum.asEnum) {
switch (AnyEnum3(MyEnum3.int(int)), AnyEnum3(AnyEnum3(MyEnum3.int(int)))) {
case (.case0(let x), .case0(let y)) where x == int && x == y:
break
default:
XCTFail()
}
switch (MyEnum3.str(str).asEnum, MyEnum3.str(str).asEnum.asEnum) {
switch (AnyEnum3(MyEnum3.str(str)), AnyEnum3(AnyEnum3(MyEnum3.str(str)))) {
case (.case1(let x), .case1(let y)) where x == str && x == y:
break
default:
XCTFail()
}
switch (MyEnum3.double(double).asEnum, MyEnum3.double(double).asEnum.asEnum) {
switch (AnyEnum3(MyEnum3.double(double)), AnyEnum3(AnyEnum3(MyEnum3.double(double)))) {
case (.case2(let x), .case2(let y)) where x == double && x == y:
break
default:
Expand All @@ -117,25 +117,25 @@ class EnumConvertibleTests: XCTestCase {
let str = "abc"
let double = 3.14
let array = [1, 2, 3]
switch (MyEnum4.int(int).asEnum, MyEnum4.int(int).asEnum.asEnum) {
switch (AnyEnum4(MyEnum4.int(int)), AnyEnum4(AnyEnum4(MyEnum4.int(int)))) {
case (.case0(let x), .case0(let y)) where x == int && x == y:
break
default:
XCTFail()
}
switch (MyEnum4.str(str).asEnum, MyEnum4.str(str).asEnum.asEnum) {
switch (AnyEnum4(MyEnum4.str(str)), AnyEnum4(AnyEnum4(MyEnum4.str(str)))) {
case (.case1(let x), .case1(let y)) where x == str && x == y:
break
default:
XCTFail()
}
switch (MyEnum4.double(double).asEnum, MyEnum4.double(double).asEnum.asEnum) {
switch (AnyEnum4(MyEnum4.double(double)), AnyEnum4(AnyEnum4(MyEnum4.double(double)))) {
case (.case2(let x), .case2(let y)) where x == double && x == y:
break
default:
XCTFail()
}
switch (MyEnum4.array(array).asEnum, MyEnum4.array(array).asEnum.asEnum) {
switch (AnyEnum4(MyEnum4.array(array)), AnyEnum4(AnyEnum4(MyEnum4.array(array)))) {
case (.case3(let x), .case3(let y)) where x == array && x == y:
break
default:
Expand All @@ -149,38 +149,81 @@ class EnumConvertibleTests: XCTestCase {
let double = 3.14
let array = [1, 2, 3]
let dictionary = ["a": 0, "b": 200]
switch (MyEnum5.int(int).asEnum, MyEnum5.int(int).asEnum.asEnum) {
switch (AnyEnum5(MyEnum5.int(int)), AnyEnum5(AnyEnum5(MyEnum5.int(int)))) {
case (.case0(let x), .case0(let y)) where x == int && x == y:
break
default:
XCTFail()
}
switch (MyEnum5.str(str).asEnum, MyEnum5.str(str).asEnum.asEnum) {
switch (AnyEnum5(MyEnum5.str(str)), AnyEnum5(AnyEnum5(MyEnum5.str(str)))) {
case (.case1(let x), .case1(let y)) where x == str && x == y:
break
default:
XCTFail()
}
switch (MyEnum5.double(double).asEnum, MyEnum5.double(double).asEnum.asEnum) {
switch (AnyEnum5(MyEnum5.double(double)), AnyEnum5(AnyEnum5(MyEnum5.double(double)))) {
case (.case2(let x), .case2(let y)) where x == double && x == y:
break
default:
XCTFail()
}
switch (MyEnum5.array(array).asEnum, MyEnum5.array(array).asEnum.asEnum) {
switch (AnyEnum5(MyEnum5.array(array)), AnyEnum5(AnyEnum5(MyEnum5.array(array)))) {
case (.case3(let x), .case3(let y)) where x == array && x == y:
break
default:
XCTFail()
}
switch (MyEnum5.dictionary(dictionary).asEnum, MyEnum5.dictionary(dictionary).asEnum.asEnum) {
switch (AnyEnum5(MyEnum5.dictionary(dictionary)), AnyEnum5(AnyEnum5(MyEnum5.dictionary(dictionary)))) {
case (.case4(let x), .case4(let y)) where x == dictionary && x == y:
break
default:
XCTFail()
}
}

func testUnwrapped() {
let enum2_1 = AnyEnum2<Int, Int>.case0(200)
switch (enum2_1) {
case .case0(let x) where x == enum2_1.unwrapped():
break
default:
XCTFail()
}

let enum2_2 = AnyEnum2<Int, Int>.case1(200)
switch (enum2_2) {
case .case1(let x) where x == enum2_2.unwrapped():
break
default:
XCTFail()
}
}

func testMap() {
let enum3 = AnyEnum3<Int, Float, Double>.case0(200)
let mappedEnum3_1 = enum3.map0 { "\($0)" }
let mappedEnum3_2 = enum3.map1 { "\($0)" }
let mappedEnum3_3 = enum3.map2 { "\($0)" }
switch mappedEnum3_1 {
case .case0(let x) where x == "200":
break
default:
XCTFail()
}
switch mappedEnum3_2 {
case .case0(let x) where x == 200:
break
default:
XCTFail()
}
switch mappedEnum3_3 {
case .case0(let x) where x == 200:
break
default:
XCTFail()
}
}

static var allTests = [
("testEnum2", testEnum2),
("testEnum3", testEnum3),
Expand Down

0 comments on commit cc81050

Please sign in to comment.