whitaker_common/i18n/
testing.rs1use std::borrow::Cow;
8use std::cell::RefCell;
9
10pub use super::helpers::{MessageResolution, safe_resolve_message_set};
11use super::{Arguments, AttrKey, BundleLookup, I18nError, MessageKey};
12
13#[derive(Clone, Debug, Eq, PartialEq)]
18pub struct FailingLookup {
19 message_key: Cow<'static, str>,
20 locale: Cow<'static, str>,
21}
22
23impl FailingLookup {
24 #[must_use]
26 pub fn new(message_key: impl Into<Cow<'static, str>>) -> Self {
27 Self::with_locale(message_key, Cow::Borrowed("test"))
28 }
29
30 #[must_use]
32 pub fn with_locale(
33 message_key: impl Into<Cow<'static, str>>,
34 locale: impl Into<Cow<'static, str>>,
35 ) -> Self {
36 Self {
37 message_key: message_key.into(),
38 locale: locale.into(),
39 }
40 }
41}
42
43impl BundleLookup for FailingLookup {
44 fn message(&self, _key: MessageKey<'_>, _args: &Arguments<'_>) -> Result<String, I18nError> {
45 Err(I18nError::MissingMessage {
46 key: self.message_key.clone().into_owned(),
47 locale: self.locale.clone().into_owned(),
48 })
49 }
50
51 fn attribute(
52 &self,
53 _key: MessageKey<'_>,
54 _attribute: AttrKey<'_>,
55 _args: &Arguments<'_>,
56 ) -> Result<String, I18nError> {
57 Err(I18nError::MissingMessage {
58 key: self.message_key.clone().into_owned(),
59 locale: self.locale.clone().into_owned(),
60 })
61 }
62}
63
64#[derive(Debug, Default)]
66pub struct RecordingEmitter {
67 messages: RefCell<Vec<String>>,
68}
69
70impl RecordingEmitter {
71 #[must_use]
73 pub fn recorded_messages(&self) -> Vec<String> {
74 self.messages.borrow().clone()
75 }
76
77 pub fn record(&self, message: String) {
79 self.messages.borrow_mut().push(message);
80 }
81}