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)?;
Ok(http::Body::Text(value))
}
Bytes::Xml { value, .. } => Ok(http::Body::Text(value.clone())),
Bytes::Json { value, .. } => {
Bytes::Xml(value) => Ok(http::Body::Text(value.clone())),
Bytes::Json(value) => {
let value = eval_json_value(value, variables)?;
Ok(http::Body::Text(value))
}

View File

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

View File

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

View File

@ -930,9 +930,9 @@ impl Htmlable for Bytes {
Bytes::Base64(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::Json { value } => value.to_html(),
Bytes::Json(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> {
match xml::parse(reader) {
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> {
match parse_json(reader) {
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] ");
assert_eq!(
bytes(&mut reader).unwrap(),
Bytes::Json {
value: JsonValue::List {
Bytes::Json(JsonValue::List {
space0: "".to_string(),
elements: vec![
JsonListElement {
space0: "".to_string(),
value: JsonValue::Number("1".to_string()),
space1: "".to_string()
space1: "".to_string(),
},
JsonListElement {
space0: "".to_string(),
value: JsonValue::Number("2".to_string()),
space1: "".to_string()
space1: "".to_string(),
},
JsonListElement {
space0: "".to_string(),
value: JsonValue::Number("3".to_string()),
space1: "".to_string()
space1: "".to_string(),
},
],
}
}
})
);
assert_eq!(reader.state.cursor, 7);
let mut reader = Reader::init("{ } ");
assert_eq!(
bytes(&mut reader).unwrap(),
Bytes::Json {
value: JsonValue::Object {
Bytes::Json(JsonValue::Object {
space0: " ".to_string(),
elements: vec![],
}
}
})
);
assert_eq!(reader.state.cursor, 3);
let mut reader = Reader::init("true");
assert_eq!(
bytes(&mut reader).unwrap(),
Bytes::Json {
value: JsonValue::Boolean(true)
}
Bytes::Json(JsonValue::Boolean(true))
);
assert_eq!(reader.state.cursor, 4);
let mut reader = Reader::init("\"\" x");
assert_eq!(
bytes(&mut reader).unwrap(),
Bytes::Json {
value: JsonValue::String(Template {
Bytes::Json(JsonValue::String(Template {
delimiter: Some('"'),
elements: vec![],
source_info: SourceInfo::new(1, 2, 1, 2),
})
}
}))
);
assert_eq!(reader.state.cursor, 2);
}
@ -144,9 +136,7 @@ mod tests {
let mut reader = Reader::init("<a/>");
assert_eq!(
bytes(&mut reader).unwrap(),
Bytes::Xml {
value: String::from("<a/>")
}
Bytes::Xml(String::from("<a/>"))
);
}
@ -189,9 +179,7 @@ mod tests {
let mut reader = Reader::init("100");
assert_eq!(
json_bytes(&mut reader).unwrap(),
Bytes::Json {
value: JsonValue::Number("100".to_string())
}
Bytes::Json(JsonValue::Number("100".to_string()))
);
}
}

View File

@ -227,7 +227,7 @@ fn status(reader: &mut Reader) -> ParseResult<'static, Status> {
pos: start,
recoverable: false,
inner: ParseError::Status {},
})
});
}
},
};
@ -429,7 +429,7 @@ mod tests {
}],
delimiter: None,
source_info: SourceInfo::new(3, 1, 4, 1),
}
},
})),
line_terminator0: LineTerminator {
space0: Whitespace {
@ -453,8 +453,7 @@ mod tests {
assert_eq!(r.method, Method::Post);
assert_eq!(
r.body.unwrap().value,
Bytes::Json {
value: JsonValue::List {
Bytes::Json(JsonValue::List {
space0: "".to_string(),
elements: vec![
JsonListElement {
@ -473,8 +472,7 @@ mod tests {
space1: "".to_string(),
},
],
}
}
})
);
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.body.unwrap().value,
Bytes::Json {
value: JsonValue::String(Template {
Bytes::Json(JsonValue::String(Template {
delimiter: Some('"'),
elements: vec![TemplateElement::String {
value: "Hello".to_string(),
encoded: "Hello".to_string(),
}],
source_info: SourceInfo::new(2, 2, 2, 7),
})
}
}))
);
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.body.unwrap().value,
Bytes::Json {
value: JsonValue::Number("100".to_string())
}
Bytes::Json(JsonValue::Number("100".to_string()))
);
}
@ -571,8 +565,7 @@ mod tests {
assert_eq!(b.line_terminators.len(), 0);
assert_eq!(
b.value,
Bytes::Json {
value: JsonValue::List {
Bytes::Json(JsonValue::List {
space0: "".to_string(),
elements: vec![
JsonListElement {
@ -591,8 +584,7 @@ mod tests {
space1: "".to_string(),
},
],
}
}
})
);
assert_eq!(reader.state.cursor, 8);
@ -601,12 +593,10 @@ mod tests {
assert_eq!(b.line_terminators.len(), 0);
assert_eq!(
b.value,
Bytes::Json {
value: JsonValue::Object {
Bytes::Json(JsonValue::Object {
space0: "".to_string(),
elements: vec![],
}
}
})
);
assert_eq!(reader.state.cursor, 2);
@ -615,12 +605,10 @@ mod tests {
assert_eq!(b.line_terminators.len(), 1);
assert_eq!(
b.value,
Bytes::Json {
value: JsonValue::Object {
Bytes::Json(JsonValue::Object {
space0: "".to_string(),
elements: vec![],
}
}
})
);
assert_eq!(reader.state.cursor, 24);

View File

@ -133,11 +133,11 @@ impl ToJson for Bytes {
Bytes::Base64(value) => value.to_json(),
Bytes::Hex(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())),
("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())),
("value".to_string(), JValue::String(value.clone())),
]),

View File

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

View File

@ -602,20 +602,13 @@ impl Lintable<Bytes> for 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 {
Bytes::File(value) => Bytes::File(value.lint()),
Bytes::Base64(value) => Bytes::Base64(value.lint()),
Bytes::Hex(value) => Bytes::Hex(value.lint()),
Bytes::Json { value } => Bytes::Json {
value: value.clone(),
},
Bytes::Json(value) => Bytes::Json(value.clone()),
Bytes::MultilineString(value) => Bytes::MultilineString(value.lint()),
Bytes::Xml { value } => Bytes::Xml {
value: value.clone(),
},
Bytes::Xml(value) => Bytes::Xml(value.clone()),
}
}
}