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: &Vec<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 match self { 72 TestAssertion::Failed { .. } => true, 73 _ => false, 74 } 75 } 76 } 77 78 impl fmt::Display for TestAssertion { 79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 80 use console::{style, Emoji}; 81 match *self { 82 TestAssertion::Passed { ref description } => write!( 83 f, 84 "{check} {desc}", 85 check = style(Emoji("✔", "+")).green(), 86 desc = style(description).green(), 87 ), 88 TestAssertion::Failed { 89 ref description, 90 ref expression, 91 why: Some(ref why), 92 } => write!( 93 f, 94 "{check} {desc}\n in `{exp}`: {why}", 95 check = style(Emoji("✖", "x")).red(), 96 desc = style(description).red(), 97 exp = style(expression).red(), 98 why = style(why).red(), 99 ), 100 TestAssertion::Failed { 101 ref description, 102 ref expression, 103 why: None, 104 } => write!( 105 f, 106 "{check} {desc}\n in `{exp}`", 107 check = style(Emoji("✖", "x")).red(), 108 desc = style(description).red(), 109 exp = style(expression).red(), 110 ), 111 } 112 } 113 } 114 115 #[macro_export] 116 macro_rules! test_assert { 117 ($description:expr, $assertion:expr) => { 118 if $assertion { 119 crate::TestAssertion::Passed { 120 description: $description, 121 } 122 } else { 123 TestAssertion::Failed { 124 description: $description, 125 expression: stringify!($assertion), 126 why: None, 127 } 128 } 129 }; 130 ($description:expr, $assertion:expr, $why:expr) => { 131 if $assertion { 132 crate::TestAssertion::Passed { 133 description: $description, 134 } 135 } else { 136 crate::TestAssertion::Failed { 137 description: $description, 138 expression: stringify!($assertion), 139 why: Some($why), 140 } 141 } 142 }; 143 } 144