Skip to content

Commit

Permalink
test: Add tests for newtypes
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker committed Feb 8, 2019
1 parent 02f0fe0 commit 1bef71d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions general/src/processor/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub trait Processor: Sized {
process_method!(process_context, crate::protocol::Context);
process_method!(process_breadcrumb, crate::protocol::Breadcrumb);
process_method!(process_template_info, crate::protocol::TemplateInfo);
process_method!(process_header_name, crate::protocol::HeaderName);

fn process_other(
&mut self,
Expand Down
2 changes: 1 addition & 1 deletion general/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use self::exception::Exception;
pub use self::fingerprint::Fingerprint;
pub use self::logentry::LogEntry;
pub use self::mechanism::{CError, MachException, Mechanism, MechanismMeta, PosixSignal};
pub use self::request::{Cookies, Headers, Query, Request};
pub use self::request::{Cookies, HeaderName, Headers, Query, Request};
pub use self::stacktrace::{Frame, Stacktrace};
pub use self::tags::{TagEntry, Tags};
pub use self::templateinfo::TemplateInfo;
Expand Down
3 changes: 2 additions & 1 deletion general/src/protocol/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl FromValue for Cookies {

/// A "into-string" type that normalizes header names.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Empty, ToValue, ProcessValue)]
pub struct HeaderName(String);
#[metastructure(process_func = "process_header_name")]
pub struct HeaderName(pub String);

impl HeaderName {
/// Creates a normalized header name.
Expand Down
62 changes: 62 additions & 0 deletions general/tests/test_derive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use semaphore_general::processor::process_value;
use semaphore_general::processor::ProcessingState;
use semaphore_general::processor::Processor;
use semaphore_general::protocol::HeaderName;
use semaphore_general::types::Annotated;
use semaphore_general::types::Meta;
use semaphore_general::types::Value;
use semaphore_general::types::ValueAction;

struct RecordingProcessor(Vec<String>);

impl Processor for RecordingProcessor {
fn process_value(
&mut self,
_value: &mut Value,
_meta: &mut Meta,
_state: &ProcessingState<'_>,
) -> ValueAction {
self.0.push("process_value".to_string());
ValueAction::Keep
}

fn process_string(
&mut self,
_value: &mut String,
_meta: &mut Meta,
_state: &ProcessingState<'_>,
) -> ValueAction {
self.0.push("process_string".to_string());
ValueAction::Keep
}

fn process_header_name(
&mut self,
_value: &mut HeaderName,
_meta: &mut Meta,
_state: &ProcessingState<'_>,
) -> ValueAction {
self.0.push("process_header_name".to_string());
ValueAction::Keep
}
}

#[test]
fn test_enums_processor_calls() {
let mut processor = RecordingProcessor(vec![]);

let mut value = Annotated::new(Value::String("hi".to_string()));
process_value(&mut value, &mut processor, &ProcessingState::root());

assert_eq!(processor.0, &["process_value", "process_string"]);
}

#[test]
fn test_simple_newtype() {
let mut processor = RecordingProcessor(vec![]);

let mut value = Annotated::new(HeaderName("hi".to_string()));
process_value(&mut value, &mut processor, &ProcessingState::root());

assert_eq!(processor.0, &["process_header_name", "process_string"]);
}

0 comments on commit 1bef71d

Please sign in to comment.