Change struct Bytes with tupple instead of anonymous struct.

This commit is contained in:
jcamiel 2022-12-05 10:20:28 +01:00
parent 6ea9d90841
commit 2841a9459b
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
9 changed files with 112 additions and 160 deletions

View File

@ -46,8 +46,8 @@ pub fn eval_bytes(
let value = eval_multiline(value, variables)?; let value = eval_multiline(value, variables)?;
Ok(http::Body::Text(value)) Ok(http::Body::Text(value))
} }
Bytes::Xml { value, .. } => Ok(http::Body::Text(value.clone())), Bytes::Xml(value) => Ok(http::Body::Text(value.clone())),
Bytes::Json { value, .. } => { Bytes::Json(value) => {
let value = eval_json_value(value, variables)?; let value = eval_json_value(value, variables)?;
Ok(http::Body::Text(value)) Ok(http::Body::Text(value))
} }

View File

@ -124,14 +124,7 @@ pub fn eval_asserts(
asserts asserts
} }
/// Check the body of an actual HTTP response against a spec body. /// Check the body of an actual HTTP response against a spec body, given a set of variables.
///
/// # Arguments
///
/// * `spec_body` - The spec HTTP response body
/// * `variables` - A map of input variables
/// * `http_response` - The actual HTTP response
/// * `context_dir` - The context directory for files
fn eval_implicit_body_asserts( fn eval_implicit_body_asserts(
spec_body: &Body, spec_body: &Body,
variables: &HashMap<String, Value>, variables: &HashMap<String, Value>,
@ -139,7 +132,7 @@ fn eval_implicit_body_asserts(
context_dir: &ContextDir, context_dir: &ContextDir,
) -> AssertResult { ) -> AssertResult {
match &spec_body.value { match &spec_body.value {
Bytes::Json { value } => { Bytes::Json(value) => {
let expected = match eval_json_value(value, variables) { let expected = match eval_json_value(value, variables) {
Ok(s) => Ok(Value::String(s)), Ok(s) => Ok(Value::String(s)),
Err(e) => Err(e), Err(e) => Err(e),
@ -161,7 +154,7 @@ fn eval_implicit_body_asserts(
source_info: spec_body.space0.source_info.clone(), source_info: spec_body.space0.source_info.clone(),
} }
} }
Bytes::Xml { value } => { Bytes::Xml(value) => {
let expected = Ok(Value::String(value.to_string())); let expected = Ok(Value::String(value.to_string()));
let actual = match http_response.text() { let actual = match http_response.text() {
Ok(s) => Ok(Value::String(s)), Ok(s) => Ok(Value::String(s)),

View File

@ -624,8 +624,8 @@ pub struct LineTerminator {
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum Bytes { pub enum Bytes {
Json { value: json::Value }, Json(json::Value),
Xml { value: String }, Xml(String),
MultilineString(MultilineString), MultilineString(MultilineString),
Base64(Base64), Base64(Base64),
File(File), File(File),

View File

@ -930,9 +930,9 @@ impl Htmlable for Bytes {
Bytes::Base64(value) => format!("<span class=\"line\">{}</span>", value.to_html()), Bytes::Base64(value) => format!("<span class=\"line\">{}</span>", value.to_html()),
Bytes::File(value) => format!("<span class=\"line\">{}</span>", value.to_html()), Bytes::File(value) => format!("<span class=\"line\">{}</span>", value.to_html()),
Bytes::Hex(value) => format!("<span class=\"line\">{}</span>", value.to_html()), Bytes::Hex(value) => format!("<span class=\"line\">{}</span>", value.to_html()),
Bytes::Json { value } => value.to_html(), Bytes::Json(value) => value.to_html(),
Bytes::MultilineString(value) => value.to_html(), Bytes::MultilineString(value) => value.to_html(),
Bytes::Xml { value } => xml_html(value), Bytes::Xml(value) => xml_html(value),
} }
} }
} }

View File

@ -42,14 +42,14 @@ pub fn bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
fn xml_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> { fn xml_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
match xml::parse(reader) { match xml::parse(reader) {
Err(e) => Err(e), Err(e) => Err(e),
Ok(value) => Ok(Bytes::Xml { value }), Ok(value) => Ok(Bytes::Xml(value)),
} }
} }
fn json_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> { fn json_bytes(reader: &mut Reader) -> ParseResult<'static, Bytes> {
match parse_json(reader) { match parse_json(reader) {
Err(e) => Err(e), Err(e) => Err(e),
Ok(value) => Ok(Bytes::Json { value }), Ok(value) => Ok(Bytes::Json(value)),
} }
} }
@ -79,62 +79,54 @@ mod tests {
let mut reader = Reader::init("[1,2,3] "); let mut reader = Reader::init("[1,2,3] ");
assert_eq!( assert_eq!(
bytes(&mut reader).unwrap(), bytes(&mut reader).unwrap(),
Bytes::Json { Bytes::Json(JsonValue::List {
value: JsonValue::List { space0: "".to_string(),
space0: "".to_string(), elements: vec![
elements: vec![ JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("1".to_string()),
value: JsonValue::Number("1".to_string()), space1: "".to_string(),
space1: "".to_string() },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("2".to_string()),
value: JsonValue::Number("2".to_string()), space1: "".to_string(),
space1: "".to_string() },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("3".to_string()),
value: JsonValue::Number("3".to_string()), space1: "".to_string(),
space1: "".to_string() },
}, ],
], })
}
}
); );
assert_eq!(reader.state.cursor, 7); assert_eq!(reader.state.cursor, 7);
let mut reader = Reader::init("{ } "); let mut reader = Reader::init("{ } ");
assert_eq!( assert_eq!(
bytes(&mut reader).unwrap(), bytes(&mut reader).unwrap(),
Bytes::Json { Bytes::Json(JsonValue::Object {
value: JsonValue::Object { space0: " ".to_string(),
space0: " ".to_string(), elements: vec![],
elements: vec![], })
}
}
); );
assert_eq!(reader.state.cursor, 3); assert_eq!(reader.state.cursor, 3);
let mut reader = Reader::init("true"); let mut reader = Reader::init("true");
assert_eq!( assert_eq!(
bytes(&mut reader).unwrap(), bytes(&mut reader).unwrap(),
Bytes::Json { Bytes::Json(JsonValue::Boolean(true))
value: JsonValue::Boolean(true)
}
); );
assert_eq!(reader.state.cursor, 4); assert_eq!(reader.state.cursor, 4);
let mut reader = Reader::init("\"\" x"); let mut reader = Reader::init("\"\" x");
assert_eq!( assert_eq!(
bytes(&mut reader).unwrap(), bytes(&mut reader).unwrap(),
Bytes::Json { Bytes::Json(JsonValue::String(Template {
value: JsonValue::String(Template { delimiter: Some('"'),
delimiter: Some('"'), elements: vec![],
elements: vec![], source_info: SourceInfo::new(1, 2, 1, 2),
source_info: SourceInfo::new(1, 2, 1, 2), }))
})
}
); );
assert_eq!(reader.state.cursor, 2); assert_eq!(reader.state.cursor, 2);
} }
@ -144,9 +136,7 @@ mod tests {
let mut reader = Reader::init("<a/>"); let mut reader = Reader::init("<a/>");
assert_eq!( assert_eq!(
bytes(&mut reader).unwrap(), bytes(&mut reader).unwrap(),
Bytes::Xml { Bytes::Xml(String::from("<a/>"))
value: String::from("<a/>")
}
); );
} }
@ -189,9 +179,7 @@ mod tests {
let mut reader = Reader::init("100"); let mut reader = Reader::init("100");
assert_eq!( assert_eq!(
json_bytes(&mut reader).unwrap(), json_bytes(&mut reader).unwrap(),
Bytes::Json { Bytes::Json(JsonValue::Number("100".to_string()))
value: JsonValue::Number("100".to_string())
}
); );
} }
} }

View File

@ -227,7 +227,7 @@ fn status(reader: &mut Reader) -> ParseResult<'static, Status> {
pos: start, pos: start,
recoverable: false, recoverable: false,
inner: ParseError::Status {}, inner: ParseError::Status {},
}) });
} }
}, },
}; };
@ -429,7 +429,7 @@ mod tests {
}], }],
delimiter: None, delimiter: None,
source_info: SourceInfo::new(3, 1, 4, 1), source_info: SourceInfo::new(3, 1, 4, 1),
} },
})), })),
line_terminator0: LineTerminator { line_terminator0: LineTerminator {
space0: Whitespace { space0: Whitespace {
@ -453,28 +453,26 @@ mod tests {
assert_eq!(r.method, Method::Post); assert_eq!(r.method, Method::Post);
assert_eq!( assert_eq!(
r.body.unwrap().value, r.body.unwrap().value,
Bytes::Json { Bytes::Json(JsonValue::List {
value: JsonValue::List { space0: "".to_string(),
space0: "".to_string(), elements: vec![
elements: vec![ JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("1".to_string()),
value: JsonValue::Number("1".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("2".to_string()),
value: JsonValue::Number("2".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("3".to_string()),
value: JsonValue::Number("3".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, ],
], })
}
}
); );
let mut reader = Reader::init("POST http://localhost:8000/post-json-string\n\"Hello\""); let mut reader = Reader::init("POST http://localhost:8000/post-json-string\n\"Hello\"");
@ -482,16 +480,14 @@ mod tests {
assert_eq!(r.method, Method::Post); assert_eq!(r.method, Method::Post);
assert_eq!( assert_eq!(
r.body.unwrap().value, r.body.unwrap().value,
Bytes::Json { Bytes::Json(JsonValue::String(Template {
value: JsonValue::String(Template { delimiter: Some('"'),
delimiter: Some('"'), elements: vec![TemplateElement::String {
elements: vec![TemplateElement::String { value: "Hello".to_string(),
value: "Hello".to_string(), encoded: "Hello".to_string(),
encoded: "Hello".to_string(), }],
}], source_info: SourceInfo::new(2, 2, 2, 7),
source_info: SourceInfo::new(2, 2, 2, 7), }))
})
}
); );
let mut reader = Reader::init("POST http://localhost:8000/post-json-number\n100"); let mut reader = Reader::init("POST http://localhost:8000/post-json-number\n100");
@ -499,9 +495,7 @@ mod tests {
assert_eq!(r.method, Method::Post); assert_eq!(r.method, Method::Post);
assert_eq!( assert_eq!(
r.body.unwrap().value, r.body.unwrap().value,
Bytes::Json { Bytes::Json(JsonValue::Number("100".to_string()))
value: JsonValue::Number("100".to_string())
}
); );
} }
@ -571,28 +565,26 @@ mod tests {
assert_eq!(b.line_terminators.len(), 0); assert_eq!(b.line_terminators.len(), 0);
assert_eq!( assert_eq!(
b.value, b.value,
Bytes::Json { Bytes::Json(JsonValue::List {
value: JsonValue::List { space0: "".to_string(),
space0: "".to_string(), elements: vec![
elements: vec![ JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("1".to_string()),
value: JsonValue::Number("1".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("2".to_string()),
value: JsonValue::Number("2".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, JsonListElement {
JsonListElement { space0: "".to_string(),
space0: "".to_string(), value: JsonValue::Number("3".to_string()),
value: JsonValue::Number("3".to_string()), space1: "".to_string(),
space1: "".to_string(), },
}, ],
], })
}
}
); );
assert_eq!(reader.state.cursor, 8); assert_eq!(reader.state.cursor, 8);
@ -601,12 +593,10 @@ mod tests {
assert_eq!(b.line_terminators.len(), 0); assert_eq!(b.line_terminators.len(), 0);
assert_eq!( assert_eq!(
b.value, b.value,
Bytes::Json { Bytes::Json(JsonValue::Object {
value: JsonValue::Object { space0: "".to_string(),
space0: "".to_string(), elements: vec![],
elements: vec![], })
}
}
); );
assert_eq!(reader.state.cursor, 2); assert_eq!(reader.state.cursor, 2);
@ -615,12 +605,10 @@ mod tests {
assert_eq!(b.line_terminators.len(), 1); assert_eq!(b.line_terminators.len(), 1);
assert_eq!( assert_eq!(
b.value, b.value,
Bytes::Json { Bytes::Json(JsonValue::Object {
value: JsonValue::Object { space0: "".to_string(),
space0: "".to_string(), elements: vec![],
elements: vec![], })
}
}
); );
assert_eq!(reader.state.cursor, 24); assert_eq!(reader.state.cursor, 24);

View File

@ -133,11 +133,11 @@ impl ToJson for Bytes {
Bytes::Base64(value) => value.to_json(), Bytes::Base64(value) => value.to_json(),
Bytes::Hex(value) => value.to_json(), Bytes::Hex(value) => value.to_json(),
Bytes::File(value) => value.to_json(), Bytes::File(value) => value.to_json(),
Bytes::Json { value } => JValue::Object(vec![ Bytes::Json(value) => JValue::Object(vec![
("type".to_string(), JValue::String("json".to_string())), ("type".to_string(), JValue::String("json".to_string())),
("value".to_string(), value.to_json()), ("value".to_string(), value.to_json()),
]), ]),
Bytes::Xml { value } => JValue::Object(vec![ Bytes::Xml(value) => JValue::Object(vec![
("type".to_string(), JValue::String("xml".to_string())), ("type".to_string(), JValue::String("xml".to_string())),
("value".to_string(), JValue::String(value.clone())), ("value".to_string(), JValue::String(value.clone())),
]), ]),

View File

@ -160,22 +160,12 @@ impl Tokenizable for Bytes {
fn tokenize(&self) -> Vec<Token> { fn tokenize(&self) -> Vec<Token> {
let mut tokens: Vec<Token> = vec![]; let mut tokens: Vec<Token> = vec![];
match self { match self {
Bytes::Json { value } => tokens.append(&mut value.tokenize()), Bytes::Json(value) => tokens.append(&mut value.tokenize()),
Bytes::Xml { value } => { Bytes::Xml(value) => tokens.push(Token::String(value.to_string())),
tokens.push(Token::String(value.to_string())); Bytes::MultilineString(value) => tokens.append(&mut value.tokenize()),
} Bytes::Base64(value) => tokens.append(&mut value.tokenize()),
Bytes::MultilineString(value) => { Bytes::Hex(value) => tokens.append(&mut value.tokenize()),
tokens.append(&mut value.tokenize()); Bytes::File(value) => tokens.append(&mut value.tokenize()),
}
Bytes::Base64(value) => {
tokens.append(&mut value.tokenize());
}
Bytes::Hex(value) => {
tokens.append(&mut value.tokenize());
}
Bytes::File(value) => {
tokens.append(&mut value.tokenize());
}
} }
tokens tokens
} }

View File

@ -602,20 +602,13 @@ impl Lintable<Bytes> for Bytes {
} }
fn lint(&self) -> Bytes { fn lint(&self) -> Bytes {
//let space0 = Whitespace { value: String::from(""), source_info: SourceInfo::init(0, 0, 0, 0) };
//let value = self.value.lint();
//let line_terminator0 = self.clone().line_terminator0;
match self { match self {
Bytes::File(value) => Bytes::File(value.lint()), Bytes::File(value) => Bytes::File(value.lint()),
Bytes::Base64(value) => Bytes::Base64(value.lint()), Bytes::Base64(value) => Bytes::Base64(value.lint()),
Bytes::Hex(value) => Bytes::Hex(value.lint()), Bytes::Hex(value) => Bytes::Hex(value.lint()),
Bytes::Json { value } => Bytes::Json { Bytes::Json(value) => Bytes::Json(value.clone()),
value: value.clone(),
},
Bytes::MultilineString(value) => Bytes::MultilineString(value.lint()), Bytes::MultilineString(value) => Bytes::MultilineString(value.lint()),
Bytes::Xml { value } => Bytes::Xml { Bytes::Xml(value) => Bytes::Xml(value.clone()),
value: value.clone(),
},
} }
} }
} }