1 #![recursion_limit = "256"] 2 3 pub mod client; 4 pub mod server; 5 6 pub mod pb { 7 #![allow(dead_code)] 8 #![allow(unused_imports)] 9 include!(concat!(env!("OUT_DIR"), "/grpc.testing.rs")); 10 } 11 12 use std::{default, fmt, iter}; 13 14 pub fn trace_init() { 15 let sub = tracing_subscriber::FmtSubscriber::builder() 16 .with_env_filter(tracing_subscriber::filter::EnvFilter::from_default_env()) 17 .finish(); 18 19 let _ = tracing::subscriber::set_global_default(sub); 20 let _ = tracing_log::LogTracer::init(); 21 } 22 23 pub fn client_payload(size: usize) -> pb::Payload { 24 pb::Payload { 25 r#type: default::Default::default(), 26 body: iter::repeat(0u8).take(size).collect(), 27 } 28 } 29 30 pub fn server_payload(size: usize) -> pb::Payload { 31 pb::Payload { 32 r#type: default::Default::default(), 33 body: iter::repeat(0u8).take(size).collect(), 34 } 35 } 36 37 impl pb::ResponseParameters { 38 fn with_size(size: i32) -> Self { 39 pb::ResponseParameters { 40 size, 41 ..Default::default() 42 } 43 } 44 } 45 46 fn response_length(response: &pb::StreamingOutputCallResponse) -> i32 { 47 match &response.payload { 48 Some(ref payload) => payload.body.len() as i32, 49 None => 0, 50 } 51 } 52 53 fn response_lengths(responses: &[pb::StreamingOutputCallResponse]) -> Vec<i32> { 54 responses.iter().map(&response_length).collect() 55 } 56 57 #[derive(Debug)] 58 pub enum TestAssertion { 59 Passed { 60 description: &'static str, 61 }, 62 Failed { 63 description: &'static str, 64 expression: &'static str, 65 why: Option<String>, 66 }, 67 } 68 69 impl TestAssertion { 70 pub fn is_failed(&self) -> bool { 71 matches!(self, TestAssertion::Failed { .. }) 72 } 73 } 74 75 impl fmt::Display for TestAssertion { 76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 77 use console::{style, Emoji}; 78 match *self { 79 TestAssertion::Passed { ref description } => write!( 80 f, 81 "{check} {desc}", 82 check = style(Emoji("✔", "+")).green(), 83 desc = style(description).green(), 84 ), 85 TestAssertion::Failed { 86 ref description, 87 ref expression, 88 why: Some(ref why), 89 } => write!( 90 f, 91 "{check} {desc}\n in `{exp}`: {why}", 92 check = style(Emoji("✖", "x")).red(), 93 desc = style(description).red(), 94 exp = style(expression).red(), 95 why = style(why).red(), 96 ), 97 TestAssertion::Failed { 98 ref description, 99 ref expression, 100 why: None, 101 } => write!( 102 f, 103 "{check} {desc}\n in `{exp}`", 104 check = style(Emoji("✖", "x")).red(), 105 desc = style(description).red(), 106 exp = style(expression).red(), 107 ), 108 } 109 } 110 } 111 112 #[macro_export] 113 macro_rules! test_assert { 114 ($description:expr, $assertion:expr) => { 115 if $assertion { 116 crate::TestAssertion::Passed { 117 description: $description, 118 } 119 } else { 120 TestAssertion::Failed { 121 description: $description, 122 expression: stringify!($assertion), 123 why: None, 124 } 125 } 126 }; 127 ($description:expr, $assertion:expr, $why:expr) => { 128 if $assertion { 129 crate::TestAssertion::Passed { 130 description: $description, 131 } 132 } else { 133 crate::TestAssertion::Failed { 134 description: $description, 135 expression: stringify!($assertion), 136 why: Some($why), 137 } 138 } 139 }; 140 } 141