-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: feature assertion output + simple duration execution (#67)
- Loading branch information
Showing
4 changed files
with
145 additions
and
57 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
Sources/FeaturevisorTestRunner/Extensions/UInt64+Time.swift
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,14 @@ | ||
import Foundation | ||
|
||
extension UInt64 { | ||
|
||
var milliseconds: Double { | ||
let elapsedTimeInMilliSeconds = Double(self) / 1_000_000.0 | ||
return Double(elapsedTimeInMilliSeconds) | ||
} | ||
|
||
var seconds: Double { | ||
let elapsedTimeInMilliSeconds = Double(self) / 1_000_000_000.0 | ||
return Double(elapsedTimeInMilliSeconds) | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
Sources/FeaturevisorTestRunner/FeaturevisorTestRunner+Print.swift
This file was deleted.
Oops, something went wrong.
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
99 changes: 99 additions & 0 deletions
99
Sources/FeaturevisorTestRunner/Utils/FeatureResultOutputBuilder.swift
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,99 @@ | ||
import FeaturevisorTypes | ||
import Foundation | ||
|
||
class FeatureResultOutputBuilder { | ||
|
||
enum ResultMark: String { | ||
case success = "✔" | ||
case failure = "✘" | ||
|
||
init(result: Bool) { | ||
self = result ? .success : .failure | ||
} | ||
} | ||
|
||
struct Assertion { | ||
let environment: Environment | ||
let index: Int | ||
let assertionResult: Bool | ||
let expectedValueFailures: [VariableKey: (expected: VariableValue, got: VariableValue?)] | ||
let description: String | ||
let elapsedTime: UInt64 | ||
} | ||
|
||
private let onlyFailures: Bool | ||
private let feature: String | ||
private var assertions: [Assertion] = [] | ||
|
||
init(feature: String, onlyFailures: Bool) { | ||
self.onlyFailures = onlyFailures | ||
self.feature = feature | ||
} | ||
|
||
@discardableResult func addAssertion( | ||
environment: Environment, | ||
index: Int, | ||
assertionResult: Bool, | ||
expectedValueFailures: [VariableKey: (expected: VariableValue, got: VariableValue?)], | ||
description: String, | ||
elapsedTime: UInt64 | ||
) -> Self { | ||
let assertion: Assertion = .init( | ||
environment: environment, | ||
index: index, | ||
assertionResult: assertionResult, | ||
expectedValueFailures: expectedValueFailures, | ||
description: description, | ||
elapsedTime: elapsedTime | ||
) | ||
|
||
assertions.append(assertion) | ||
return self | ||
} | ||
|
||
func build() -> String? { | ||
|
||
let hasFailedAssertions = !assertions.filter({ !$0.assertionResult }).isEmpty | ||
|
||
guard onlyFailures && hasFailedAssertions || !onlyFailures else { | ||
return nil | ||
} | ||
|
||
let totalElapsedTimeInMilliSeconds = assertions.reduce( | ||
0.0, | ||
{ $0 + $1.elapsedTime.milliseconds } | ||
) | ||
var output: String = "" | ||
|
||
output.append("\nTesting: \(feature).feature.yml (\(totalElapsedTimeInMilliSeconds)ms)") | ||
output.append("\n feature \"\(feature)\"") | ||
|
||
assertions.forEach({ assertion in | ||
|
||
let mark = ResultMark(result: assertion.assertionResult) | ||
let index = assertion.index | ||
let env = assertion.environment.rawValue | ||
let description = assertion.description | ||
let expectedValueFailures = assertion.expectedValueFailures | ||
let elapsedTimeInMilliSeconds = assertion.elapsedTime.milliseconds | ||
|
||
output.append( | ||
"\n \(mark.rawValue) Assertion #\(index): \(env) \(description) (\(elapsedTimeInMilliSeconds)ms)" | ||
) | ||
|
||
expectedValueFailures.forEach({ (key, value) in | ||
output.append("\n => variable key: \(key)") | ||
output.append("\n => expected: \(value.expected)") | ||
|
||
if let got = value.got { | ||
output.append("\n => received: \(got)") | ||
} | ||
else { | ||
output.append("\n => received: nil") | ||
} | ||
}) | ||
}) | ||
|
||
return output | ||
} | ||
} |