Skip to content

Commit 21a762e

Browse files
author
Saul Urias
committed
Fixed ForecastDateTime Tests Adding TimeZone
1 parent 5f88e78 commit 21a762e

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

SwiftWeather/ForecastDateTime.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
import Foundation
77

88
struct ForecastDateTime {
9-
let rawDate: Double
10-
11-
init(_ date: Double) {
12-
rawDate = date
13-
}
14-
15-
var shortTime: String {
16-
let dateFormatter = DateFormatter()
17-
dateFormatter.dateFormat = "HH:mm"
18-
let date = Date(timeIntervalSince1970: rawDate)
19-
return dateFormatter.string(from: date)
20-
}
9+
let rawDate: Double
10+
let timeZone: TimeZone
11+
12+
init(date: Double, timeZone: TimeZone) {
13+
self.timeZone = timeZone
14+
self.rawDate = date
15+
}
16+
17+
var shortTime: String {
18+
let dateFormatter = DateFormatter()
19+
dateFormatter.timeZone = timeZone
20+
dateFormatter.dateFormat = "HH:mm"
21+
let date = Date(timeIntervalSince1970: rawDate)
22+
return dateFormatter.string(from: date)
23+
}
2124
}

SwiftWeatherTests/UnitTests/ForecastDateTimeSpec.swift

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ import Nimble
88
@testable import SwiftWeather
99

1010
class ForecastDateTimeSpec: QuickSpec {
11-
override func spec() {
12-
describe("#init") {
13-
it("should init with the rawDate correctly assigned") {
14-
var forecastDateTime = ForecastDateTime(1234)
15-
expect(forecastDateTime.rawDate).to(beCloseTo(1234))
16-
forecastDateTime = ForecastDateTime(0)
17-
expect(forecastDateTime.rawDate).to(beCloseTo(0))
18-
}
11+
12+
private let testTimeZone = TimeZone(abbreviation: "UTC+11:00")!
13+
14+
override func spec() {
15+
describe("#init") {
16+
it("should init with the rawDate correctly assigned") {
17+
var forecastDateTime = ForecastDateTime(date: 1488096060, timeZone: self.testTimeZone)
18+
expect(forecastDateTime.rawDate).to(beCloseTo(1488096060))
19+
forecastDateTime = ForecastDateTime(date: 0, timeZone: self.testTimeZone)
20+
expect(forecastDateTime.rawDate).to(beCloseTo(0))
21+
}
22+
}
23+
24+
describe("#shortTime") {
25+
it("should return the correct shortTime string with format HH:mm") {
26+
var forecastDateTime = ForecastDateTime(date: 1488096060, timeZone: self.testTimeZone)
27+
expect(forecastDateTime.shortTime).to(equal("7:01 PM"))
28+
forecastDateTime = ForecastDateTime(date: 1488103200, timeZone: self.testTimeZone)
29+
expect(forecastDateTime.shortTime).to(equal("9:00 PM"))
30+
}
31+
}
1932
}
20-
21-
describe("#shortTime") {
22-
it("should return the correct shortTime string with format HH:mm") {
23-
var forecastDateTime = ForecastDateTime(1488096060)
24-
expect(forecastDateTime.shortTime).to(equal("01:01"))
25-
forecastDateTime = ForecastDateTime(1488103200)
26-
expect(forecastDateTime.shortTime).to(equal("03:00"))
27-
}
28-
}
29-
}
3033
}

0 commit comments

Comments
 (0)