Skip to content

Commit 805ee38

Browse files
authored
Added testing and travis/codecov integrations (#19)
* Added testing and travis/codecov integrations * Small fixes * Fixed travis yml
1 parent 80b1389 commit 805ee38

File tree

9 files changed

+140
-50
lines changed

9 files changed

+140
-50
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.DS_Store
22
build/
33
DerivedData/
4-
xcuserdata
4+
xcuserdata

.travis.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,23 @@ branches:
77
master
88

99
before_install:
10+
- gem install xcpretty
11+
- gem install xcpretty-json-formatter
1012
- git clone https://github.com/dingwilson/devops-ci.git
1113

14+
env:
15+
matrix:
16+
- UDID="7A5E86EB-E32C-475F-8FF5-10AA330C1D34", IOS_DEVICE="iPhone 7 Plus (11.1)"
17+
- UDID="80BE80F4-C243-43F0-BD95-5CB085DAD04F", IOS_DEVICE="iPhone 6 (8.1)"
18+
1219
script:
1320
- set -o pipefail
14-
15-
- pod spec lint
16-
21+
- open -a "simulator" --args -CurrentDeviceUDID $UDID
22+
- xcodebuild clean test -project RandomUserSwift.xcodeproj -scheme RandomUserSwift -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,id=$UDID" CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO ONLY_ACTIVE_ARCH=NO -enableCodeCoverage yes | XCPRETTY_JSON_FILE_OUTPUT="xcodebuild.json" xcpretty -f `xcpretty-json-formatter`
23+
- bash <(curl -s https://codecov.io/bash)
1724
- bundle exec danger --fail-on-errors=true
1825

19-
after_success:
20-
- source ./devops-ci/gen_jazzy_docs.sh
26+
jobs:
27+
include:
28+
- stage: jazzy
29+
script: source ./devops-ci/gen_jazzy_docs.sh

Dangerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ warn('PR is classed as Work in Progress', sticky: false) if github.pr_title.incl
33

44
# Run SwiftLint
55
swiftlint.lint_files
6+
7+
# Report any xcodebuild warnings/errors
8+
xcode_summary.report 'xcodebuild.json'

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ source "https://rubygems.org"
44

55
gem "danger"
66
gem "danger-swiftlint"
7+
gem "danger-xcode_summary"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
</p>
44

55
[![Build Status](https://travis-ci.org/dingwilson/RandomUserSwift.svg?branch=master)](https://travis-ci.org/dingwilson/RandomUserSwift)
6+
[![codecov](https://codecov.io/gh/dingwilson/RandomUserSwift/branch/master/graph/badge.svg)](https://codecov.io/gh/dingwilson/RandomUserSwift)
7+
[![doccov](https://wilsonding.com/RandomUserSwift/badge.svg)](https://wilsonding.com/RandomUserSwift)
68
[![CocoaPods Version Status](https://img.shields.io/cocoapods/v/RandomUserSwift.svg)](https://cocoapods.org/pods/RandomUserSwift)
79
[![Carthage compatible](https://img.shields.io/badge/Carthage-Compatible-brightgreen.svg?style=flat)](https://github.com/Carthage/Carthage)
810
[![CocoaPods](https://img.shields.io/cocoapods/dt/RandomUserSwift.svg)](https://cocoapods.org/pods/RandomUserSwift)
@@ -50,7 +52,7 @@ import RandomUserSwift
5052
Then, use the `getUsers` function via the `shared` singleton with a completion handler.
5153

5254
```swift
53-
RandomUser.shared.getUsers() { data, error in
55+
RandomUser.shared.getUsers { data, error in
5456
guard let data = data else { return }
5557

5658
print(data)

RandomUserSwift.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
DYLIB_INSTALL_NAME_BASE = "@rpath";
377377
INFOPLIST_FILE = RandomUserSwift/Info.plist;
378378
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
379+
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
379380
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
380381
PRODUCT_BUNDLE_IDENTIFIER = com.wilsonding.RandomUserSwift;
381382
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -398,6 +399,7 @@
398399
DYLIB_INSTALL_NAME_BASE = "@rpath";
399400
INFOPLIST_FILE = RandomUserSwift/Info.plist;
400401
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
402+
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
401403
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
402404
PRODUCT_BUNDLE_IDENTIFIER = com.wilsonding.RandomUserSwift;
403405
PRODUCT_NAME = "$(TARGET_NAME)";

RandomUserSwift/RandomUser.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RandomUser {
1515
public static let shared = RandomUser()
1616

1717
/// API Version Number
18-
public static var apiVersion = 1.2
18+
private static var apiVersion = 1.2
1919

2020
/**
2121
Get one or more users from RandomUser API (https://www.randomuser.me/documentation)
@@ -32,7 +32,7 @@ public class RandomUser {
3232
public func getUsers(results: Int = 1,
3333
gender: String = "both",
3434
nationality: String = "",
35-
completionHandler: @escaping (_ data: Users?, _ error: Error?) -> Void) {
35+
_ completionHandler: @escaping (_ data: Users?, _ error: Error?) -> Void) {
3636

3737
guard let url = createUrl(results: results, gender: gender, nationality: nationality) else { return }
3838

@@ -46,7 +46,6 @@ public class RandomUser {
4646
}
4747

4848
let users = try? JSONDecoder().decode(Users.self, from: data)
49-
5049
completionHandler(users, nil)
5150
}
5251

RandomUserSwift/Users.swift

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,33 @@ import Foundation
1010

1111
/// Codable Struct conforming to return payload from RandomUser API call
1212
public struct Users: Codable {
13-
let results: [Result]
14-
let info: Info
13+
let results: [Result]?
14+
let info: Info?
1515
}
1616

1717
struct Info: Codable {
18-
let seed: String
19-
let results, page: Int
20-
let version: String
18+
let seed: String?
19+
let results, page: Int?
20+
let version: String?
2121
}
2222

2323
struct Result: Codable {
24-
let gender: Gender
25-
let name: Name
26-
let location: Location
27-
let email: String
28-
let login: Login
29-
let dob, registered: Dob
30-
let phone, cell: String
24+
let gender: Gender?
25+
let name: NameClass?
26+
let location: Location?
27+
let email: String?
28+
let login: Login?
29+
let dob, registered: Dob?
30+
let phone, cell: String?
3131
// swiftlint:disable identifier_name
32-
let id: ID
33-
let picture: Picture
34-
let nat: String
32+
let id: ID?
33+
let picture: Picture?
34+
let nat: String?
3535
}
3636

3737
struct Dob: Codable {
38-
let date: Date
39-
let age: Int
38+
let date: String?
39+
let age: Int?
4040
}
4141

4242
enum Gender: String, Codable {
@@ -46,19 +46,35 @@ enum Gender: String, Codable {
4646

4747
// swiftlint:disable type_name
4848
struct ID: Codable {
49-
let name: String
49+
let name: NameEnum?
5050
let value: String?
5151
}
5252

53+
enum NameEnum: String, Codable {
54+
case avs = "AVS"
55+
case bsn = "BSN"
56+
case cpr = "CPR"
57+
case dni = "DNI"
58+
case empty = ""
59+
// swiftlint:disable identifier_name
60+
case fn = "FN"
61+
case hetu = "HETU"
62+
case insee = "INSEE"
63+
case nino = "NINO"
64+
case pps = "PPS"
65+
case ssn = "SSN"
66+
case tfn = "TFN"
67+
}
68+
5369
struct Location: Codable {
54-
let street, city, state: String
55-
let postcode: Postcode
56-
let coordinates: Coordinates
57-
let timezone: Timezone
70+
let street, city, state: String?
71+
let postcode: Postcode?
72+
let coordinates: Coordinates?
73+
let timezone: Timezone?
5874
}
5975

6076
struct Coordinates: Codable {
61-
let latitude, longitude: String
77+
let latitude, longitude: String?
6278
}
6379

6480
enum Postcode: Codable {
@@ -75,7 +91,6 @@ enum Postcode: Codable {
7591
self = .string(value)
7692
return
7793
}
78-
7994
throw DecodingError.typeMismatch(Postcode.self,
8095
DecodingError.Context(codingPath: decoder.codingPath,
8196
debugDescription: "Wrong type for Postcode"))
@@ -93,21 +108,22 @@ enum Postcode: Codable {
93108
}
94109

95110
struct Timezone: Codable {
96-
let offset, description: String
111+
let offset, description: String?
97112
}
98113

99114
struct Login: Codable {
100-
let uuid, username, password, salt: String
101-
let md5, sha1, sha256: String
115+
let uuid, username, password, salt: String?
116+
let md5, sha1, sha256: String?
102117
}
103118

104-
struct Name: Codable {
105-
let title: Title
106-
let first, last: String
119+
struct NameClass: Codable {
120+
let title: Title?
121+
let first, last: String?
107122
}
108123

109124
enum Title: String, Codable {
110125
case madame
126+
case mademoiselle
111127
case miss
112128
case monsieur
113129
// swiftlint:disable identifier_name
@@ -118,5 +134,5 @@ enum Title: String, Codable {
118134
}
119135

120136
struct Picture: Codable {
121-
let large, medium, thumbnail: String
137+
let large, medium, thumbnail: String?
122138
}

RandomUserSwiftTests/RandomUserSwiftTests.swift

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,84 @@ import XCTest
1111

1212
class RandomUserSwiftTests: XCTestCase {
1313

14+
var data: Users?
15+
var error: Error?
16+
17+
var expectation: XCTestExpectation?
18+
1419
override func setUp() {
1520
super.setUp()
16-
// Put setup code here. This method is called before the invocation of each test method in the class.
21+
22+
self.error = nil
23+
self.data = nil
24+
self.expectation = self.expectation(description: "api")
1725
}
1826

1927
override func tearDown() {
20-
// Put teardown code here. This method is called after the invocation of each test method in the class.
28+
XCTAssertNil(self.error, "Running getUsers returns non-nil error.")
29+
XCTAssertNotNil(self.data, "Running getUsers returns nil value for data.")
30+
31+
self.expectation = nil
2132
super.tearDown()
2233
}
2334

24-
func testExample() {
25-
// This is an example of a functional test case.
26-
// Use XCTAssert and related functions to verify your tests produce the correct results.
35+
func testGetUsersWithMultipleResults() {
36+
RandomUser.shared.getUsers(results: 5) {
37+
self.data = $0
38+
self.error = $1
39+
40+
self.expectation?.fulfill()
41+
}
42+
43+
waitForExpectations(timeout: 5, handler: nil)
44+
45+
XCTAssertEqual(data?.results?.count,
46+
5,
47+
"Setting multiple results does not generate correct number of users.")
48+
}
49+
50+
func testGetUsersByFemaleGender() {
51+
RandomUser.shared.getUsers(gender: "female") {
52+
self.data = $0
53+
self.error = $1
54+
55+
self.expectation?.fulfill()
56+
}
57+
58+
waitForExpectations(timeout: 5, handler: nil)
59+
60+
XCTAssertEqual(data?.results?[0].gender?.rawValue,
61+
"female",
62+
"Setting gender as female does not generate a female user.")
2763
}
2864

29-
func testPerformanceExample() {
30-
// This is an example of a performance test case.
31-
self.measure {
32-
// Put the code you want to measure the time of here.
65+
func testGetUsersByMaleGender() {
66+
RandomUser.shared.getUsers(gender: "male") {
67+
self.data = $0
68+
self.error = $1
69+
70+
self.expectation?.fulfill()
3371
}
72+
73+
waitForExpectations(timeout: 5, handler: nil)
74+
75+
XCTAssertEqual(data?.results?[0].gender?.rawValue,
76+
"male",
77+
"Setting gender as male does not generate a male user.")
3478
}
3579

80+
func testGetUsersByNationality() {
81+
RandomUser.shared.getUsers(nationality: "us") {
82+
self.data = $0
83+
self.error = $1
84+
85+
self.expectation?.fulfill()
86+
}
87+
88+
waitForExpectations(timeout: 5, handler: nil)
89+
90+
XCTAssertEqual(data?.results?[0].nat,
91+
"US",
92+
"Setting nationality as US does not generate a user with US nat.")
93+
}
3694
}

0 commit comments

Comments
 (0)