1use std::borrow::Cow;
4use std::time::Duration;
5
6use icedwright_types::{Point, VirtualDuration};
7use serde::{Deserialize, Serialize};
8
9use crate::blob::{BlobRef, Png, Yaml};
10use crate::ids::{StepIndex, SubscriptionOrdinal, TaskOrdinal, WindowOrdinal};
11
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15pub struct Step {
16 pub index: StepIndex,
18 pub kind: StepKind,
20 pub location: SourceLocation,
22 pub virtual_start: VirtualDuration,
24 pub virtual_end: VirtualDuration,
26 pub events: Vec<TraceEvent>,
28 pub messages: Vec<TraceMessage>,
30 pub tasks: Vec<TraceTask>,
32 pub state_diff: Option<String>,
34 pub attempts: Vec<Attempt>,
36 pub tree_before: BlobRef<Yaml>,
38 pub tree_after: BlobRef<Yaml>,
40 pub frame_before: Option<BlobRef<Png>>,
42 pub frame_after: Option<BlobRef<Png>>,
44 pub outcome: StepOutcome,
46}
47
48#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
51#[serde(rename_all = "snake_case")]
52#[non_exhaustive]
53pub enum StepKind {
54 Action {
56 action: String,
58 locator: Option<SelectorText>,
60 resolved: Option<String>,
62 point: Option<Point>,
64 },
65 Expect {
67 subject: SelectorText,
69 assertion: String,
71 negated: bool,
73 },
74 Advance {
76 by: VirtualDuration,
78 },
79 Task {
81 name: String,
83 control: String,
85 },
86 Window {
88 window: WindowOrdinal,
90 change: String,
92 },
93}
94
95impl StepKind {
96 #[must_use]
99 pub fn is_action(&self) -> bool {
100 matches!(self, StepKind::Action { .. })
101 }
102}
103
104pub trait CanonicalSelector {
106 fn canonical(&self) -> String;
108}
109
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(transparent)]
114pub struct SelectorText(String);
115
116impl SelectorText {
117 #[must_use]
119 pub fn of(selector: &impl CanonicalSelector) -> Self {
120 Self(selector.canonical())
121 }
122
123 #[must_use]
125 pub fn as_str(&self) -> &str {
126 &self.0
127 }
128}
129
130#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
132pub struct SourceLocation {
133 pub file: String,
135 pub line: u32,
137 pub column: u32,
139}
140
141impl From<&std::panic::Location<'_>> for SourceLocation {
142 fn from(location: &std::panic::Location<'_>) -> Self {
143 Self {
144 file: location.file().to_owned(),
145 line: location.line(),
146 column: location.column(),
147 }
148 }
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
153#[serde(rename_all = "snake_case")]
154pub enum Captured {
155 Ignored,
157 Captured,
159}
160
161#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
163pub struct TraceEvent {
164 pub at: VirtualDuration,
166 pub window: WindowOrdinal,
168 pub event: String,
170 pub status: Captured,
172}
173
174#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
176pub struct TraceMessage {
177 pub at: VirtualDuration,
179 pub debug: String,
181 pub full_len: Option<u64>,
183 pub variant: Option<Cow<'static, str>>,
185 pub origin: TraceOrigin,
187}
188
189#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
191#[serde(rename_all = "snake_case")]
192pub enum TraceOrigin {
193 Widget {
195 event: u32,
197 },
198 Task(TaskOrdinal),
200 Subscription(SubscriptionOrdinal),
202}
203
204#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
206pub struct TraceTask {
207 pub id: TaskOrdinal,
209 pub name: Option<String>,
211 pub at: VirtualDuration,
213 pub change: TraceTaskChange,
215}
216
217#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
219#[serde(rename_all = "snake_case")]
220#[non_exhaustive]
221pub enum TraceTaskChange {
222 Spawned {
224 by_message: Option<u32>,
226 },
227 Held,
229 Released,
231 Resolved,
233 Replaced,
235 Finished,
237 Failed {
239 error: String,
241 },
242 Aborted,
244}
245
246#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
248pub struct Attempt {
249 pub at: VirtualDuration,
251 pub matched: u32,
253 pub detail: String,
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
259#[serde(rename_all = "snake_case")]
260pub enum StepOutcome {
261 Passed,
263 Failed {
265 message: String,
267 },
268 Panicked {
270 message: String,
272 },
273}
274
275impl StepOutcome {
276 #[must_use]
278 pub fn is_failure(&self) -> bool {
279 !matches!(self, StepOutcome::Passed)
280 }
281}
282
283#[derive(Debug, Clone, Copy, PartialEq, Eq)]
285pub struct RealSpan {
286 pub start: Duration,
288 pub end: Duration,
290}
291
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
294pub struct RealTiming {
295 pub step: StepIndex,
297 pub real_start: Duration,
299 pub real_end: Duration,
301}
302
303#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
305pub struct RealTimings {
306 pub wall_clock_start: Option<Duration>,
308 pub steps: Vec<RealTiming>,
310}
311
312#[cfg(test)]
313mod tests {
314 use super::*;
315
316 struct Button;
317
318 impl CanonicalSelector for Button {
319 fn canonical(&self) -> String {
320 "role=button[name=\"Save\"]".to_owned()
321 }
322 }
323
324 #[test]
325 fn selector_text_comes_from_a_selector() {
326 assert_eq!(
327 SelectorText::of(&Button).as_str(),
328 "role=button[name=\"Save\"]"
329 );
330 }
331
332 #[test]
333 fn locations_come_from_the_caller() {
334 let location = SourceLocation::from(std::panic::Location::caller());
335 assert!(location.file.ends_with("step.rs"));
336 assert!(location.line > 0);
337 }
338
339 #[test]
340 fn only_passed_is_not_a_failure() {
341 assert!(!StepOutcome::Passed.is_failure());
342 let message = String::new();
343 assert!(
344 StepOutcome::Failed {
345 message: message.clone()
346 }
347 .is_failure()
348 );
349 assert!(StepOutcome::Panicked { message }.is_failure());
350 }
351}