From fee904ba1008a3f693263149b559c181ec284d6c Mon Sep 17 00:00:00 2001 From: Kevin Perry Date: Tue, 8 Mar 2022 16:54:53 -0800 Subject: [PATCH] =?UTF-8?q?Add=20delayNext=20=E2=80=9C,=E2=80=9D=20operato?= =?UTF-8?q?r=20to=20validation=20diagram?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Guides/Validation.md | 6 ++ Sources/AsyncSequenceValidation/Event.swift | 12 ++++ Sources/AsyncSequenceValidation/Test.swift | 28 ++++++-- Sources/AsyncSequenceValidation/Theme.swift | 2 + .../TestValidationTests.swift | 64 +++++++++++++++++++ 5 files changed, 107 insertions(+), 5 deletions(-) diff --git a/Guides/Validation.md b/Guides/Validation.md index c2b2875..573a215 100644 --- a/Guides/Validation.md +++ b/Guides/Validation.md @@ -47,6 +47,7 @@ The syntax is trivially parsable (and consequently customizable). By default, th | `[` | Begin group | `"[ab]-"` | | `]` | End group | `"[ab]-"` | | `'` | Begin/End Value | `"'foo'-"` | +| `,` | Delay next | `",[a,]b"` | Because some events may take up more than one character and the alignment is important to the visual progression of events, spaces are not counted as part of the parsed events. A space means that no time is advanced and not event is produced or expected. This means that the string `"a - -b- -"` is equivalent to `"a--b--"`. @@ -59,6 +60,9 @@ struct EmojiTokens: AsyncSequenceValidationTheme { case "➖": return .step case "❗️": return .error case "❌": return .finish + case "➡️": return .beginValue + case "⬅️": return .endValue + case "⏳": return .delayNext case " ": return .skip default: return .value(String(character)) } @@ -214,6 +218,7 @@ Access to the validation diagram input list is done through calls such as `$0.in | `[` | `.beginGroup` | Begin group | `"[ab]-"` | | `]` | `.endGroup` | End group | `"[ab]-"` | | `'` | `.beginValue` `.endValue` | Begin/End Value | `"'foo'-"` | +| `,` | `.delayNext` | Delay next | `",[a,]b"` | | ` ` | `.skip` | Skip/Ignore | `"a b- |"` | | | `.value` | Values. | `"ab-|"` | @@ -234,6 +239,7 @@ extension AsyncSequenceValidationDiagram { case error case finish case cancel + case delayNext case beginValue case endValue case beginGroup diff --git a/Sources/AsyncSequenceValidation/Event.swift b/Sources/AsyncSequenceValidation/Event.swift index 0e92346..45e5d9d 100644 --- a/Sources/AsyncSequenceValidation/Event.swift +++ b/Sources/AsyncSequenceValidation/Event.swift @@ -33,6 +33,7 @@ extension AsyncSequenceValidationDiagram { case value(String, String.Index) case failure(Error, String.Index) case finish(String.Index) + case delayNext(String.Index) case cancel(String.Index) var results: [Result] { @@ -40,6 +41,7 @@ extension AsyncSequenceValidationDiagram { case .value(let value, _): return [.success(value)] case .failure(let failure, _): return [.failure(failure)] case .finish: return [.success(nil)] + case .delayNext: return [] case .cancel: return [] } } @@ -49,6 +51,7 @@ extension AsyncSequenceValidationDiagram { case .value(_, let index): return index case .failure(_, let index): return index case .finish(let index): return index + case .delayNext(let index): return index case .cancel(let index): return index } } @@ -99,6 +102,15 @@ extension AsyncSequenceValidationDiagram { } else { string?.append(ch) } + case .delayNext: + if string == nil { + if grouping == 0 { + when = when.advanced(by: .steps(1)) + } + emissions.append((when, .delayNext(index))) + } else { + string?.append(ch) + } case .beginValue: string = "" case .endValue: diff --git a/Sources/AsyncSequenceValidation/Test.swift b/Sources/AsyncSequenceValidation/Test.swift index 83ec81a..7b64e0b 100644 --- a/Sources/AsyncSequenceValidation/Test.swift +++ b/Sources/AsyncSequenceValidation/Test.swift @@ -10,6 +10,7 @@ //===----------------------------------------------------------------------===// import _CAsyncSequenceValidationSupport +import ClockStub @_silgen_name("swift_job_run") @usableFromInline @@ -22,7 +23,7 @@ public protocol AsyncSequenceValidationTest: Sendable { var inputs: [String] { get } var output: String { get } - func test(_ event: (String) -> Void) async throws + func test(with clock: C, activeTicks: [C.Instant], _ event: (String) -> Void) async throws } extension AsyncSequenceValidationDiagram { @@ -31,11 +32,18 @@ extension AsyncSequenceValidationDiagram { let sequence: Operation let output: String - func test(_ event: (String) -> Void) async throws { + func test(with clock: C, activeTicks: [C.Instant], _ event: (String) -> Void) async throws { var iterator = sequence.makeAsyncIterator() do { - while let item = try await iterator.next() { - event(item) + for tick in activeTicks { + if tick != clock.now { + try await clock.sleep(until: tick, tolerance: nil) + } + if let item = try await iterator.next() { + event(item) + } else { + break + } } do { if let pastEnd = try await iterator.next(){ @@ -238,6 +246,16 @@ extension AsyncSequenceValidationDiagram { default: return false } }.map { when, _ in return when }) + + let activeTicks = parsedOutput.reduce(into: [Clock.Instant.init(when: .zero)]) { events, thisEvent in + switch thisEvent { + case (let when, .delayNext(_)): + events.removeLast() + events.append(when.advanced(by: .steps(1))) + case (let when, _): + events.append(when) + } + } var expected = [(Clock.Instant, Result)]() for (when, event) in parsedOutput { @@ -262,7 +280,7 @@ extension AsyncSequenceValidationDiagram { let runner = Task { do { - try await test.test { event in + try await test.test(with: clock, activeTicks: activeTicks) { event in actual.withCriticalRegion { values in values.append((clock.now, .success(event))) } diff --git a/Sources/AsyncSequenceValidation/Theme.swift b/Sources/AsyncSequenceValidation/Theme.swift index 3d67645..2fe7ce0 100644 --- a/Sources/AsyncSequenceValidation/Theme.swift +++ b/Sources/AsyncSequenceValidation/Theme.swift @@ -25,6 +25,7 @@ extension AsyncSequenceValidationDiagram { case error case finish case cancel + case delayNext case beginValue case endValue case beginGroup @@ -40,6 +41,7 @@ extension AsyncSequenceValidationDiagram { case "^": return .error case "|": return .finish case ";": return .cancel + case ",": return .delayNext case "'": return inValue ? .endValue : .beginValue case "[": return .beginGroup case "]": return .endGroup diff --git a/Tests/AsyncAlgorithmsTests/TestValidationTests.swift b/Tests/AsyncAlgorithmsTests/TestValidationTests.swift index 5e486bb..4f49e69 100644 --- a/Tests/AsyncAlgorithmsTests/TestValidationTests.swift +++ b/Tests/AsyncAlgorithmsTests/TestValidationTests.swift @@ -79,6 +79,7 @@ final class TestValidationDiagram: XCTestCase { case "❌": return .finish case "➡️": return .beginValue case "⬅️": return .endValue + case "⏳": return .delayNext case " ": return .skip default: return .value(String(character)) } @@ -250,4 +251,67 @@ final class TestValidationDiagram: XCTestCase { "a--b--c--|" } } + + func test_delayNext() { + validate { + "xxx--- |" + $0.inputs[0] + "x,,,,[xx]|" + } + } + + func test_delayNext_initialDelay() { + validate { + "xxx |" + $0.inputs[0] + ",,[xxx]|" + } + } + + func test_delayNext_into_emptyTick() { + validate { + "xx|" + LaggingAsyncSequence($0.inputs[0], delayBy: .steps(3), using: $0.clock) + ",,,---x--[x|]" + } + } + + func test_values_one_at_a_time_after_delay() { + validate { + "xxx|" + $0.inputs[0] + ",,,[x,][x,][x,]|" + } + } +} + +struct LaggingAsyncSequence : AsyncSequence { + typealias Element = Base.Element + + struct Iterator : AsyncIteratorProtocol { + var base: Base.AsyncIterator + let delay: C.Instant.Duration + let clock: C + mutating func next() async throws -> Element? { + if let value = try await base.next() { + try await clock.sleep(until: clock.now.advanced(by: delay), tolerance: nil) + return value + } else { + return nil + } + } + } + + func makeAsyncIterator() -> Iterator { + return Iterator(base: base.makeAsyncIterator(), delay: delay, clock: clock) + } + + let base: Base + let delay: C.Instant.Duration + let clock: C + init(_ base: Base, delayBy delay: C.Instant.Duration, using clock: C) { + self.base = base + self.delay = delay + self.clock = clock + } }