-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/edit_camel_case
- Loading branch information
Showing
6 changed files
with
206 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// YumemiWeather.APIQuality.swift | ||
// | ||
// | ||
// Created by Tomohiro Kumagai on 2024/05/02 | ||
// | ||
// | ||
|
||
extension YumemiWeather { | ||
|
||
/// API の品質を表現します。 | ||
enum APIQuality { | ||
case sometimesFails(probability: Double) | ||
case alwaysFails | ||
case neverFails | ||
} | ||
} | ||
|
||
extension YumemiWeather { | ||
|
||
/// API の想定品質です。 | ||
static var apiQuality: APIQuality = .sometimesFails(probability: 0.25) | ||
|
||
static func whetherHit(with probability: Double) -> Bool { | ||
return (0 ..< probability).contains(.random(in: 0 ..< 1)) | ||
} | ||
|
||
/// この場所に不安定要素を埋め込みます。 | ||
/// | ||
/// 不安定さの度合いは `apiQuality` に依存します。 | ||
/// - Throws: YumemiError ここで失敗が生成されると YumemiWeatherError.unknownError が送出されます。 | ||
static func introduceInstability() throws { | ||
|
||
switch apiQuality { | ||
|
||
case .neverFails: | ||
return | ||
|
||
case .sometimesFails(let probability) where !whetherHit(with: probability): | ||
return | ||
|
||
case .sometimesFails, .alwaysFails: | ||
throw YumemiWeatherError.unknownError | ||
} | ||
} | ||
|
||
/// この場所に不安定要素を埋め込みます。 | ||
/// | ||
/// 不安定さの度合いは `apiQuality` に依存します。 | ||
/// - Throws: YumemiError ここで失敗が生成されると YumemiWeatherError.unknownError が送出されます。 | ||
func introduceInstability() throws { | ||
try Self.introduceInstability() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// TestMethods.swift | ||
// | ||
// | ||
// Created by Tomohiro Kumagai on 2024/05/02 | ||
// | ||
// | ||
|
||
import XCTest | ||
@testable import YumemiWeather | ||
|
||
/// 失敗数が想定内かを判定します。 | ||
/// - Parameters: | ||
/// - probability: 失敗率です。 | ||
/// - tryingCount: 総試行回数です。 | ||
/// - failureCount: うち、失敗した回数です。 | ||
/// - file: 判定コードが記載されたファイルです。 | ||
/// - line: 判定コードが記載された行です。 | ||
func XCTAssertFailureCount(probability: Double, tryingCount: Int, failureCount: Int, file: StaticString = #filePath, line: UInt = #line) { | ||
|
||
let estimatedFailureCount = Double(tryingCount) * probability | ||
let expectedFailureMargin = Double(tryingCount) * 0.01 | ||
|
||
let expectedFailureRange = estimatedFailureCount - expectedFailureMargin ..< estimatedFailureCount + expectedFailureMargin | ||
|
||
XCTAssertTrue(expectedFailureRange.contains(Double(failureCount)), "Failures: \(failureCount), Estimated failures = \(estimatedFailureCount)±\(expectedFailureMargin)", file: file, line: line) | ||
} | ||
|
||
/// YumemiWether API の品質をテストします。 | ||
/// - Parameters: | ||
/// - tryingCount: 総試行回数です。 | ||
/// - quality: API の想定品質です。 | ||
/// - file: 判定コードが記載されたファイルです。 | ||
/// - line: 判定コードが記載された行です。 | ||
func XCTAssertAPIQuality(_ predicate: () throws -> Void, tryingCount: Int, quality: YumemiWeather.APIQuality, file: StaticString = #filePath, line: UInt = #line) { | ||
|
||
var succeededCount = 0 | ||
var failedCount = 0 | ||
|
||
YumemiWeather.apiQuality = quality | ||
|
||
for _ in 0 ..< tryingCount { | ||
|
||
switch Result(catching: predicate) { | ||
|
||
case .success(): | ||
succeededCount += 1 | ||
|
||
case .failure(_): | ||
failedCount += 1 | ||
} | ||
} | ||
|
||
switch quality { | ||
|
||
case .neverFails: | ||
XCTAssertEqual(succeededCount, tryingCount, file: file, line: line) | ||
XCTAssertEqual(failedCount, 0, file: file, line: line) | ||
|
||
case .alwaysFails: | ||
XCTAssertEqual(succeededCount, 0, file: file, line: line) | ||
XCTAssertEqual(failedCount, tryingCount, file: file, line: line) | ||
|
||
case .sometimesFails(let probability): | ||
XCTAssertFailureCount(probability: probability, tryingCount: tryingCount, failureCount: failedCount, file: file, line: line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters