Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #29 from hkellaway/feature/nonoptional_properties
Browse files Browse the repository at this point in the history
Added force decoding
  • Loading branch information
Harlan Kellaway committed Aug 21, 2015
2 parents bf18602 + 8f70fe5 commit a9999ff
Show file tree
Hide file tree
Showing 31 changed files with 955 additions and 344 deletions.
8 changes: 8 additions & 0 deletions GlossExample/GlossExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
DC9E42D51B8066D600D1792A /* DecoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC9E42D41B8066D600D1792A /* DecoderTests.swift */; };
DCEA3C201B80FD960022F666 /* TestModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCEA3C1F1B80FD960022F666 /* TestModel.swift */; };
DCEA3C221B80FDE60022F666 /* TestNestedModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCEA3C211B80FDE60022F666 /* TestNestedModel.swift */; };
DCF891401B86AF6B00888FB1 /* Pods_GlossExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DCF8913F1B86AF6B00888FB1 /* Pods_GlossExample.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
F0157B7C46CEF886BC8CFC43 /* Pods_GlossExample.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 371978450C13B8BC0B320144 /* Pods_GlossExample.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -59,6 +60,7 @@
DC9E42D41B8066D600D1792A /* DecoderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DecoderTests.swift; sourceTree = "<group>"; };
DCEA3C1F1B80FD960022F666 /* TestModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestModel.swift; sourceTree = "<group>"; };
DCEA3C211B80FDE60022F666 /* TestNestedModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNestedModel.swift; sourceTree = "<group>"; };
DCF8913F1B86AF6B00888FB1 /* Pods_GlossExample.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Pods_GlossExample.framework; path = "Pods/../build/Debug-iphoneos/Pods_GlossExample.framework"; sourceTree = "<group>"; };
ED8E6DA407D0AE26300C907D /* Pods-GlossExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GlossExample.release.xcconfig"; path = "Pods/Target Support Files/Pods-GlossExample/Pods-GlossExample.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -75,6 +77,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
DCF891401B86AF6B00888FB1 /* Pods_GlossExample.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -93,6 +96,7 @@
63509F10EB2766219388D381 /* Frameworks */ = {
isa = PBXGroup;
children = (
DCF8913F1B86AF6B00888FB1 /* Pods_GlossExample.framework */,
371978450C13B8BC0B320144 /* Pods_GlossExample.framework */,
);
name = Frameworks;
Expand Down Expand Up @@ -505,10 +509,12 @@
};
DC08B5CF1B76B3ED0098D6C8 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 1CF68443A5AEBB8235A16A54 /* Pods-GlossExample.debug.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
"$(PROJECT_DIR)/build/Debug-iphoneos",
);
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand All @@ -524,10 +530,12 @@
};
DC08B5D01B76B3ED0098D6C8 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 1CF68443A5AEBB8235A16A54 /* Pods-GlossExample.debug.xcconfig */;
buildSettings = {
FRAMEWORK_SEARCH_PATHS = (
"$(SDKROOT)/Developer/Library/Frameworks",
"$(inherited)",
"$(PROJECT_DIR)/build/Debug-iphoneos",
);
INFOPLIST_FILE = GlossExampleTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
Expand Down
12 changes: 6 additions & 6 deletions GlossExample/GlossExample/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import Gloss

struct Repo: Glossy {

let repoId: Int?
let repoId: Int
let name: String?
let desc: String?
let desc: String
let url: NSURL?
let owner: RepoOwner?
let owner: RepoOwner
let primaryLanguage: Language?

enum Language: String {
Expand All @@ -43,11 +43,11 @@ struct Repo: Glossy {

static func fromJSON(json: JSON) -> Repo {
return Repo (
repoId: "id" <~~ json,
repoId: "id" <~~! json,
name: "name" <~~ json,
desc: "description" <~~ json,
desc: "description" <~~! json,
url: "html_url" <~~ json,
owner: "owner" <~~ json,
owner: "owner" <~~! json,
primaryLanguage: "language" <~~ json
)
}
Expand Down
4 changes: 2 additions & 2 deletions GlossExample/GlossExample/RepoOwner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import Gloss

struct RepoOwner: Glossy {

let ownerId: Int?
let ownerId: Int
let username: String?

// MARK: - Deserialization

static func fromJSON(json: JSON) -> RepoOwner {
return RepoOwner(
ownerId: "id" <~~ json,
ownerId: "id" <~~! json,
username: "login" <~~ json
)
}
Expand Down
177 changes: 166 additions & 11 deletions GlossExample/GlossExampleTests/DecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ class DecoderTests: XCTestCase {

func testDecodeBool() {
let result: Bool? = Decoder.decode("bool")(testJSON!)
let force: Bool = Decoder.forceDecode("bool")(testJSON!)

XCTAssertTrue((result == true), "Decode Bool should return correct value")
XCTAssertTrue((force == true), "Force decode Bool should return correct value")
}

func testDecodeBoolArray() {
Expand Down Expand Up @@ -152,17 +154,6 @@ class DecoderTests: XCTestCase {
XCTAssertTrue((result?.name == "nestedModel1"), "Decode nested model should return correct value")
}

func testDecodeNestedModelArray() {
let result: [TestNestedModel]? = Decoder.decodeArray("nestedModelArray")(testJSON!)
let element1: TestNestedModel = result![0]
let element2: TestNestedModel = result![1]

XCTAssertTrue((element1.id == 456), "Decode nested model array should return correct value")
XCTAssertTrue((element1.name == "nestedModel2"), "Decode nested model array should return correct value")
XCTAssertTrue((element2.id == 789), "Decode nested model array should return correct value")
XCTAssertTrue((element2.name == "nestedModel3"), "Decode nested model array should return correct value")
}

func testDecodeEnumValue() {
let result: TestModel.EnumValue? = Decoder.decodeEnum("enumValue")(testJSON!)

Expand Down Expand Up @@ -224,4 +215,168 @@ class DecoderTests: XCTestCase {
XCTAssertTrue((result?.absoluteString == "http://github.com"), "Decode NSURL should return correct value")
}

func testForceDecodeBool() {
let result: Bool = Decoder.forceDecode("bool")(testJSON!)

XCTAssertTrue((result == true), "Force decode Bool should return correct value")
}

func testForceDecodeBoolArray() {
let result: [Bool] = Decoder.forceDecode("boolArray")(testJSON!)
let element1: Bool = result[0]
let element2: Bool = result[1]
let element3: Bool = result[2]

XCTAssertTrue((element1 == true), "Decode Bool array should return correct value")
XCTAssertTrue((element2 == false), "Decode Bool array should return correct value")
XCTAssertTrue((element3 == true), "Decode Bool array should return correct value")
}

func testForceDecodeInt() {
let result: Int = Decoder.forceDecode("integer")(testJSON!)

XCTAssertTrue((result == 1), "Decode Int should return correct value")
}

func testForceDecodeIntArray() {
let result: [Int] = Decoder.forceDecode("integerArray")(testJSON!)
let element1: Int = result[0]
let element2: Int = result[1]
let element3: Int = result[2]

XCTAssertTrue((element1 == 1), "Decode Int array should return correct value")
XCTAssertTrue((element2 == 2), "Decode Int array should return correct value")
XCTAssertTrue((element3 == 3), "Decode Int array should return correct value")
}

func testForceDecodeFloat() {
let result: Float = Decoder.forceDecode("float")(testJSON!)

XCTAssertTrue((result == 2.0), "Decode Float should return correct value")
}

func testForceDecodeFloatArray() {
let result: [Float] = Decoder.forceDecode("floatArray")(testJSON!)
let element1: Float = result[0]
let element2: Float = result[1]
let element3: Float = result[2]

XCTAssertTrue((element1 == 1.0), "Decode Float array should return correct value")
XCTAssertTrue((element2 == 2.0), "Decode Float array should return correct value")
XCTAssertTrue((element3 == 3.0), "Decode Float array should return correct value")
}

func testForceDecodeDouble() {
let result: Double = Decoder.forceDecode("double")(testJSON!)

XCTAssertTrue((result == 6.0), "Decode Double should return correct value")
}

func testForceDecodeDoubleArray() {
let result: [Double] = Decoder.forceDecode("doubleArray")(testJSON!)
let element1: Double = result[0]
let element2: Double = result[1]
let element3: Double = result[2]

XCTAssertTrue((element1 == 4.0), "Decode Double array should return correct value")
XCTAssertTrue((element2 == 5.0), "Decode Double array should return correct value")
XCTAssertTrue((element3 == 6.0), "Decode Double array should return correct value")
}

func testForceDecodeString() {
let result: String = Decoder.forceDecode("string")(testJSON!)

XCTAssertTrue((result == "abc"), "Decode String should return correct value")
}

func testForceDecodeStringArray() {
let result: [String] = Decoder.forceDecode("stringArray")(testJSON!)
let element1: String = result[0]
let element2: String = result[1]
let element3: String = result[2]

XCTAssertTrue((element1 == "def"), "Decode String array should return correct value")
XCTAssertTrue((element2 == "ghi"), "Decode String array should return correct value")
XCTAssertTrue((element3 == "jkl"), "Decode String array should return correct value")
}

func testForceDecodeNestedModel() {
let result: TestNestedModel = Decoder.forceDecode("nestedModel")(testJSON!)

XCTAssertTrue((result.id == 123), "Decode nested model should return correct value")
XCTAssertTrue((result.name == "nestedModel1"), "Decode nested model should return correct value")
}

func testForceDecodeNestedModelArray() {
let result: [TestNestedModel] = Decoder.forceDecodeArray("nestedModelArray")(testJSON!)
let element1: TestNestedModel = result[0]
let element2: TestNestedModel = result[1]

XCTAssertTrue((element1.id == 456), "Decode nested model array should return correct value")
XCTAssertTrue((element1.name == "nestedModel2"), "Decode nested model array should return correct value")
XCTAssertTrue((element2.id == 789), "Decode nested model array should return correct value")
XCTAssertTrue((element2.name == "nestedModel3"), "Decode nested model array should return correct value")
}

func testForceDecodeEnumValue() {
let result: TestModel.EnumValue = Decoder.forceDecodeEnum("enumValue")(testJSON!)

XCTAssertTrue((result == TestModel.EnumValue.A), "Decode enum value should return correct value")
}

func testForceDecodeEnumArray() {
let result: [TestModel.EnumValue] = Decoder.forceDecodeArray("enumValueArray")(testJSON!)
let element1: TestModel.EnumValue = result[0]
let element2: TestModel.EnumValue = result[1]
let element3: TestModel.EnumValue = result[2]

XCTAssertTrue((element1 == TestModel.EnumValue.A), "Decode enum value array should return correct value")
XCTAssertTrue((element2 == TestModel.EnumValue.B), "Decode enum value array should return correct value")
XCTAssertTrue((element3 == TestModel.EnumValue.C), "Decode enum value array should return correct value")
}

func testForceDecodeDate() {
let result: NSDate = Decoder.forceDecodeDate("date", dateFormatter: TestModel.dateFormatter)(testJSON!)

let year: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Year, fromDate: result).year
let month: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Month, fromDate: result).month
let day: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Day, fromDate: result).day
let hour: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Hour, fromDate: result).hour
let minute: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Minute, fromDate: result).minute
let second: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Second, fromDate: result).second
let nanosecond: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Nanosecond, fromDate: result).nanosecond

XCTAssertTrue((year == 2015), "Decode NSDate should return correct value")
XCTAssertTrue((month == 8), "Decode NSDate should return correct value")
XCTAssertTrue((day == 16), "Decode NSDate should return correct value")
XCTAssertTrue((hour == 20), "Decode NSDate should return correct value")
XCTAssertTrue((minute == 51), "Decode NSDate should return correct value")
XCTAssertTrue((second == 46), "Decode NSDate should return correct value")
XCTAssertTrue((nanosecond/1000000 == 599), "Decode NSDate should return correct value")
}

func testForceDecodeDateISO8601() {
let result: NSDate = Decoder.forceDecodeDateISO8601("dateISO8601")(testJSON!)

let year: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Year, fromDate: result).year
let month: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Month, fromDate: result).month
let day: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Day, fromDate: result).day
let hour: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Hour, fromDate: result).hour
let minute: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Minute, fromDate: result).minute
let second: Int = NSCalendar.currentCalendar().components(NSCalendarUnit.Second, fromDate: result).second

XCTAssertTrue((year == 2015), "Decode NSDate should return correct value")
XCTAssertTrue((month == 8), "Decode NSDate should return correct value")
XCTAssertTrue((day == 8), "Decode NSDate should return correct value")
XCTAssertTrue((hour == 17), "Decode NSDate should return correct value")
XCTAssertTrue((minute == 57), "Decode NSDate should return correct value")
XCTAssertTrue((second == 13), "Decode NSDate should return correct value")
}

func testForceDecodeURL() {
let result: NSURL = Decoder.forceDecodeURL("url")(testJSON!)

XCTAssertTrue((result.absoluteString == "http://github.com"), "Decode NSURL should return correct value")
}

}
Loading

0 comments on commit a9999ff

Please sign in to comment.