Skip to content
This repository has been archived by the owner on Apr 2, 2023. It is now read-only.

Commit

Permalink
Add Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanDowning committed Apr 25, 2018
1 parent d1aa8ba commit d71e1c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:4.1
import PackageDescription

let package = Package(
Expand Down
79 changes: 5 additions & 74 deletions Sources/METAR/METAR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

public struct METAR: Codable {
public struct METAR: Codable, Equatable {

public let identifier: String
public let date: Date
Expand All @@ -32,30 +32,6 @@ public struct METAR: Codable {

}

extension METAR: Equatable {

public static func == (lhs: METAR, rhs: METAR) -> Bool {
return lhs.identifier == rhs.identifier &&
lhs.date == rhs.date &&
lhs.metarString == rhs.metarString &&
lhs.wind == rhs.wind &&
lhs.qnh == rhs.qnh &&
lhs.skyCondition == rhs.skyCondition &&
lhs.visibility == rhs.visibility &&
lhs.weather == rhs.weather &&
lhs.trends == rhs.trends &&
lhs.militaryColourCode == rhs.militaryColourCode &&
lhs.temperature == rhs.temperature &&
lhs.dewPoint == rhs.dewPoint &&
lhs.automaticStation == rhs.automaticStation &&
lhs.noSignificantChangesExpected == rhs.noSignificantChangesExpected &&
lhs.correction == rhs.correction &&
lhs.remarks == rhs.remarks &&
lhs.flightRules == rhs.flightRules
}

}

extension METAR {

static func noaaFlightRules(ceilingAndVisibilityOK: Bool, cloudLayers: [CloudLayer], visibility: Measurement<UnitLength>?) -> NOAAFlightRules? {
Expand Down Expand Up @@ -115,7 +91,7 @@ extension METAR {
guard let correctionRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)COR\\b") else { return nil }
guard let windRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)([0-9]{3}|VRB)([0-9]{2})(?:G([0-9]{2}))?(KT|MPS|KPH)(?: ([0-9]{3})V([0-9]{3}))?\\b") else { return nil }
guard let cloudsRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(SKC|CLR|NSC|NCD)\\b") else { return nil }
guard let cloudLayerRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(FEW|SCT|BKN|OVC|VV|///)([0-9]{3}|///)(CB|TCU|///)?") else { return nil }
guard let cloudLayerRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(FEW|SCT|BKN|OVC|VV|///)([0-9]{3}|///)(?:///)?(CB|TCU|///)?") else { return nil }
guard let temperatureRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(M)?([0-9]{2})/(M)?([0-9]{2})\\b") else { return nil }
guard let malformedTemperatureRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(M)?([0-9]{2})/ ") else { return nil }
guard let visibilityRegularExpression = try? NSRegularExpression(pattern: "(?<!\\S)(CAVOK|[0-9]{4})(NDV)?\\b") else { return nil }
Expand Down Expand Up @@ -558,10 +534,6 @@ public struct QNH: Equatable, Codable {
}
}

public static func == (lhs: QNH, rhs: QNH) -> Bool {
return lhs.measurement == rhs.measurement
}

}

public struct Temperature: Equatable, Codable {
Expand All @@ -580,10 +552,6 @@ public struct Temperature: Equatable, Codable {
}
}

public static func == (lhs: Temperature, rhs: Temperature) -> Bool {
return lhs.measurement == rhs.measurement
}

}

public struct Visibility: Equatable, Codable {
Expand All @@ -598,10 +566,6 @@ public struct Visibility: Equatable, Codable {
public let unit: Unit
public let greaterThanOrEqual: Bool

public static func == (lhs: Visibility, rhs: Visibility) -> Bool {
return lhs.measurement == rhs.measurement && lhs.greaterThanOrEqual == rhs.greaterThanOrEqual
}

public var measurement: Measurement<UnitLength> {
switch unit {
case .kilometers:
Expand All @@ -615,9 +579,9 @@ public struct Visibility: Equatable, Codable {

}

public struct Wind: Equatable, Codable {
public struct Wind: Codable, Equatable {

public struct Speed: Codable {
public struct Speed: Codable, Equatable {

public enum Unit: String, Codable {
case knots
Expand All @@ -628,10 +592,6 @@ public struct Wind: Equatable, Codable {
public let value: Double
public let unit: Unit

public static func == (lhs: Speed, rhs: Speed) -> Bool {
return lhs.measurement == rhs.measurement
}

public var measurement: Measurement<UnitSpeed> {
switch unit {
case .knots:
Expand All @@ -653,21 +613,8 @@ public struct Wind: Equatable, Codable {
public let variation: Variation?

public struct Variation: Equatable, Codable {

public let from: Degrees
public let to: Degrees

public static func == (lhs: Wind.Variation, rhs: Wind.Variation) -> Bool {
return lhs.from == rhs.from && lhs.to == rhs.to
}

}

public static func == (lhs: Wind, rhs: Wind) -> Bool {
return lhs.direction == rhs.direction &&
lhs.speed == rhs.speed &&
lhs.gustSpeed?.measurement == rhs.gustSpeed?.measurement &&
lhs.variation == rhs.variation
}

}
Expand Down Expand Up @@ -697,10 +644,6 @@ public struct CloudLayer: Equatable, Codable {
}
}

public static func == (lhs: Height, rhs: Height) -> Bool {
return lhs.measurement == rhs.measurement
}

}

public let coverage: Coverage
Expand All @@ -715,10 +658,6 @@ public struct CloudLayer: Equatable, Codable {
case cumulonimbus, toweringCumulus
}

public static func == (lhs: CloudLayer, rhs: CloudLayer) -> Bool {
return lhs.coverage == rhs.coverage && lhs.height?.measurement == rhs.height?.measurement && lhs.significantCloudType == rhs.significantCloudType
}

}

public struct Weather: Equatable, Codable {
Expand Down Expand Up @@ -767,10 +706,6 @@ public struct Weather: Equatable, Codable {
case wellDevelopedDustWhirls = "PO"
}

public static func == (lhs: Weather, rhs: Weather) -> Bool {
return lhs.modifier == rhs.modifier && lhs.phenomena == rhs.phenomena
}

}

public enum MilitaryColourCode: String, Codable {
Expand All @@ -783,7 +718,7 @@ public enum MilitaryColourCode: String, Codable {
case red
}

public struct Forecast: Equatable, Codable {
public struct Forecast: Codable, Equatable {

public enum `Type`: String, Codable {
case becoming = "BECMG", temporaryForecast = "TEMPO"
Expand All @@ -792,10 +727,6 @@ public struct Forecast: Equatable, Codable {
public let metarRepresentation: METAR
public let type: Type

public static func == (lhs: Forecast, rhs: Forecast) -> Bool {
return lhs.metarRepresentation == rhs.metarRepresentation && lhs.type == rhs.type
}

}

public enum NOAAFlightRules: String, Codable {
Expand Down
31 changes: 30 additions & 1 deletion Tests/METARTests/METARTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class METARTests: XCTestCase {
militaryColourCode: .blue,
temperature: Temperature(value: 22, unit: .celsius),
dewPoint: Temperature(value: 20, unit: .celsius),
relativeHumidity: 1,
relativeHumidity: 0.88436416127981798,
ceilingAndVisibilityOK: false,
automaticStation: true,
correction: true,
Expand All @@ -38,6 +38,35 @@ class METARTests: XCTestCase {
XCTAssertEqual(metar, testMETAR)
}

func testSlashesInBetweenCloudTypeAndHeight() {
let metarString = "EGGD 251250Z AUTO 25016G27KT 220V280 9999 BKN019///TCU 11/11 Q1013"
let metar = METAR(rawMETAR: metarString)
let testMETAR = METAR(
identifier: "EGGD",
date: date(day: 25, hour: 12, minute: 50),
wind: Wind(direction: 250, speed: Wind.Speed(value: 16, unit: .knots), gustSpeed: Wind.Speed(value: 27, unit: .knots), variation: Wind.Variation(from: 220, to: 280)),
qnh: QNH(value: 1013, unit: .hectopascals),
skyCondition: nil,
cloudLayers: [CloudLayer(coverage: .broken, height: CloudLayer.Height(value: 1900, unit: .feet), significantCloudType: .toweringCumulus)],
visibility: Visibility(value: 10, unit: .kilometers, greaterThanOrEqual: true),
weather: [],
trends: [],
militaryColourCode: nil,
temperature: Temperature(value: 11, unit: .celsius),
dewPoint: Temperature(value: 11, unit: .celsius),
relativeHumidity: 1,
ceilingAndVisibilityOK: false,
automaticStation: true,
correction: false,
noSignificantChangesExpected: false,
remarks: nil,
metarString: metarString,
flightRules: .mvfr
)

XCTAssertEqual(metar, testMETAR)
}

func testLIFRVisibility() {
XCTAssertEqual(METAR(rawMETAR: "EGGD 121212Z FEW010 1/8SM")?.flightRules, .lifr)
XCTAssertEqual(METAR(rawMETAR: "EGGD 121212Z FEW010 1/4SM")?.flightRules, .lifr)
Expand Down

0 comments on commit d71e1c4

Please sign in to comment.