Skip to content

Commit

Permalink
Test event emission origin
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed May 20, 2024
1 parent 2865511 commit b5dd972
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crates/events/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl<'a> EventAttributeEntry<'a> for EventOrigin<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Event, EventLevel, EventTypeBuilder};

const fn dummy_trace() -> EventTrace<'static> {
EventTrace {
Expand All @@ -233,6 +234,30 @@ mod tests {
}
}

#[test]
fn test_event_trace_emit_event() {
let (event, start_line, end_line) = {
let ev = Event::new(
EventTypeBuilder::new_with_type("test").build(),
EventLevel::Tx,
);
let mut events = Vec::with_capacity(1);

const START_LINE: u32 = line!();
emit_event(ev, &mut events);
const END_LINE: u32 = line!();

(events.pop().unwrap(), START_LINE, END_LINE)
};

let trace = event.read_attribute::<EventOrigin>().unwrap();

assert!(trace.line > start_line && trace.line < end_line);
assert_eq!(trace.file, file!());
assert_eq!(trace.pkg_name, env!("CARGO_PKG_NAME"));
assert_eq!(trace.pkg_version, env!("CARGO_PKG_VERSION"));
}

#[test]
fn test_event_trace_roundtrip() {
let serialized = dummy_trace().to_string();
Expand Down Expand Up @@ -267,4 +292,10 @@ mod tests {
)
);
}

#[track_caller]
fn emit_event(event: Event, events: &mut impl EmitEvents) {
let mut tracer = EventTracer::trace(events);
tracer.emit(event);
}
}

0 comments on commit b5dd972

Please sign in to comment.