tracing_mock/expect.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
//! Construct expectations for traces which should be received
//!
//! This module contains constructors for expectations defined
//! in the [`event`], [`span`], and [`field`] modules.
//!
//! # Examples
//!
//! ```
//! use tracing_mock::{expect, subscriber};
//!
//! let (subscriber, handle) = subscriber::mock()
//! // Expect an event with message
//! .event(expect::event().with_fields(expect::msg("message")))
//! .only()
//! .run_with_handle();
//!
//! tracing::subscriber::with_default(subscriber, || {
//! tracing::info!("message");
//! });
//!
//! handle.assert_finished();
//! ```
use std::fmt;
use crate::{
ancestry::ExpectedAncestry,
event::ExpectedEvent,
field::{ExpectedField, ExpectedFields, ExpectedValue},
span::{ExpectedId, ExpectedSpan, NewSpan},
};
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum Expect {
Event(ExpectedEvent),
FollowsFrom {
consequence: ExpectedSpan,
cause: ExpectedSpan,
},
Enter(ExpectedSpan),
Exit(ExpectedSpan),
CloneSpan(ExpectedSpan),
DropSpan(ExpectedSpan),
Visit(ExpectedSpan, ExpectedFields),
NewSpan(NewSpan),
Nothing,
}
/// Create a new [`ExpectedEvent`].
///
/// For details on how to add additional assertions to the expected
/// event, see the [`event`] module and the [`ExpectedEvent`] struct.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
///
/// If we expect an event and instead record something else, the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(expect::event())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// });
///
/// handle.assert_finished();
/// ```
pub fn event() -> ExpectedEvent {
ExpectedEvent {
..Default::default()
}
}
/// Construct a new [`ExpectedSpan`].
///
/// For details on how to add additional assertions to the expected
/// span, see the [`span`] module and the [`ExpectedSpan`] and
/// [`NewSpan`] structs.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .new_span(expect::span())
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// let span = tracing::info_span!("span");
/// let _guard = span.enter();
/// });
///
/// handle.assert_finished();
/// ```
///
/// If we expect to enter a span and instead record something else, the test
/// will fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let (subscriber, handle) = subscriber::mock()
/// .enter(expect::span())
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
pub fn span() -> ExpectedSpan {
ExpectedSpan {
..Default::default()
}
}
/// Construct a new [`ExpectedField`].
///
/// For details on how to set the value of the expected field and
/// how to expect multiple fields, see the [`field`] module and the
/// [`ExpectedField`] and [`ExpectedFields`] structs.
/// span, see the [`span`] module and the [`ExpectedSpan`] and
/// [`NewSpan`] structs.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "field_value");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different field value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event()
/// .with_fields(expect::field("field.name").with_value(&"field_value"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!(field.name = "different_field_value");
/// });
///
/// handle.assert_finished();
/// ```
pub fn field<K>(name: K) -> ExpectedField
where
String: From<K>,
{
ExpectedField {
name: name.into(),
value: ExpectedValue::Any,
}
}
/// Construct a new message [`ExpectedField`].
///
/// For details on how to set the value of the message field and
/// how to expect multiple fields, see the [`field`] module and the
/// [`ExpectedField`] and [`ExpectedFields`] structs.
///
/// This is equivalent to
/// `expect::field("message").with_value(message)`.
///
/// # Examples
///
/// ```
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!("message");
/// });
///
/// handle.assert_finished();
/// ```
///
/// A different message value will cause the test to fail:
///
/// ```should_panic
/// use tracing_mock::{expect, subscriber};
///
/// let event = expect::event().with_fields(
/// expect::msg("message"));
///
/// let (subscriber, handle) = subscriber::mock()
/// .event(event)
/// .run_with_handle();
///
/// tracing::subscriber::with_default(subscriber, || {
/// tracing::info!("different message");
/// });
///
/// handle.assert_finished();
/// ```
pub fn msg(message: impl fmt::Display) -> ExpectedField {
ExpectedField {
name: "message".to_string(),
value: ExpectedValue::Debug(message.to_string()),
}
}
/// Returns a new, unset `ExpectedId`.
///
/// The `ExpectedId` needs to be attached to a [`NewSpan`] or an
/// [`ExpectedSpan`] passed to [`MockSubscriber::new_span`] to
/// ensure that it gets set. When the a clone of the same
/// `ExpectedSpan` is attached to an [`ExpectedSpan`] and passed to
/// any other method on [`MockSubscriber`] that accepts it, it will
/// ensure that it is exactly the same span used across those
/// distinct expectations.
///
/// For more details on how to use this struct, see the documentation
/// on [`ExpectedSpan::with_id`].
///
/// [`MockSubscriber`]: struct@crate::subscriber::MockSubscriber
/// [`MockSubscriber::new_span`]: fn@crate::subscriber::MockSubscriber::new_span
pub fn id() -> ExpectedId {
ExpectedId::new_unset()
}
/// Convenience function that returns [`ExpectedAncestry::IsContextualRoot`].
pub fn is_contextual_root() -> ExpectedAncestry {
ExpectedAncestry::IsContextualRoot
}
/// Convenience function that returns [`ExpectedAncestry::HasContextualParent`] with
/// provided name.
pub fn has_contextual_parent<S: Into<ExpectedSpan>>(span: S) -> ExpectedAncestry {
ExpectedAncestry::HasContextualParent(span.into())
}
/// Convenience function that returns [`ExpectedAncestry::IsExplicitRoot`].
pub fn is_explicit_root() -> ExpectedAncestry {
ExpectedAncestry::IsExplicitRoot
}
/// Convenience function that returns [`ExpectedAncestry::HasExplicitParent`] with
/// provided name.
pub fn has_explicit_parent<S: Into<ExpectedSpan>>(span: S) -> ExpectedAncestry {
ExpectedAncestry::HasExplicitParent(span.into())
}
impl Expect {
pub(crate) fn bad(&self, name: impl AsRef<str>, what: fmt::Arguments<'_>) {
let name = name.as_ref();
match self {
Expect::Event(e) => panic!(
"\n[{}] expected event {}\n[{}] but instead {}",
name, e, name, what,
),
Expect::FollowsFrom { consequence, cause } => panic!(
"\n[{}] expected consequence {} to follow cause {} but instead {}",
name, consequence, cause, what,
),
Expect::Enter(e) => panic!(
"\n[{}] expected to enter {}\n[{}] but instead {}",
name, e, name, what,
),
Expect::Exit(e) => panic!(
"\n[{}] expected to exit {}\n[{}] but instead {}",
name, e, name, what,
),
Expect::CloneSpan(e) => {
panic!(
"\n[{}] expected to clone {}\n[{}] but instead {}",
name, e, name, what,
)
}
Expect::DropSpan(e) => {
panic!(
"\n[{}] expected to drop {}\n[{}] but instead {}",
name, e, name, what,
)
}
Expect::Visit(e, fields) => panic!(
"\n[{}] expected {} to record {}\n[{}] but instead {}",
name, e, fields, name, what,
),
Expect::NewSpan(e) => panic!(
"\n[{}] expected {}\n[{}] but instead {}",
name, e, name, what
),
Expect::Nothing => panic!(
"\n[{}] expected nothing else to happen\n[{}] but {} instead",
name, name, what,
),
}
}
}