Skip to content

Commit

Permalink
chore: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Oct 26, 2022
1 parent 2424a4a commit 7b0f22b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pbjson-build/src/generator/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn write_serialize_scalar_variable<W: Write>(
Indent(indent),
field_name,
variable.as_ref
)
);
}
};

Expand Down Expand Up @@ -611,9 +611,9 @@ fn write_deserialize_message<W: Write>(
writeln!(
writer,
"{indent}{field}: {field}__.ok_or_else(|| serde::de::Error::missing_field(\"{json_name}\"))?,",
indent=Indent(indent + 3),
field= field.rust_field_name(),
json_name= field.json_name()
indent = Indent(indent + 3),
field = field.rust_field_name(),
json_name = field.json_name()
)?;
}
FieldModifier::UseDefault | FieldModifier::Repeated => {
Expand Down Expand Up @@ -967,9 +967,13 @@ fn write_deserialize_field<W: Write>(
}
write!(writer, "{})", Indent(indent + 1))?;
}
FieldType::Message(_) => {
write!(writer, "map.next_value()?")?;
}
FieldType::Message(_) => match field.field_modifier {
FieldModifier::Repeated => {
// No explicit presence for repeated fields
write!(writer, "Some(map.next_value()?)")?;
}
_ => write!(writer, "map.next_value()?")?,
},
},
}
writeln!(writer, ";")?;
Expand Down Expand Up @@ -998,7 +1002,7 @@ fn write_encode_scalar_field<W: Write>(
write!(writer, "map.next_value()?")
}
_ => write!(writer, "Some(map.next_value()?)"),
}
};
}
};

Expand Down
3 changes: 3 additions & 0 deletions pbjson-test/protos/syntax3.proto
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,7 @@ message KitchenSink {
google.protobuf.StringValue string_value = 54;
google.protobuf.UInt32Value uint32_value = 55;
google.protobuf.UInt64Value uint64_value = 56;

repeated google.protobuf.Int32Value repeated_int32_value = 57;
map<string, google.protobuf.Int32Value> map_int32_value = 58;
}
55 changes: 55 additions & 0 deletions pbjson-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ pub mod test {
include!(concat!(env!("OUT_DIR"), "/test.syntax3.rs"));
include!(concat!(env!("OUT_DIR"), "/test.syntax3.serde.rs"));
}

pub mod common {
include!(concat!(env!("OUT_DIR"), "/test.common.rs"));
include!(concat!(env!("OUT_DIR"), "/test.common.serde.rs"));
}

pub mod duplicate_name {
include!(concat!(env!("OUT_DIR"), "/test.duplicate_name.rs"));
include!(concat!(env!("OUT_DIR"), "/test.duplicate_name.serde.rs"));
}

pub mod escape {
include!(concat!(
env!("OUT_DIR"),
Expand Down Expand Up @@ -80,6 +83,7 @@ mod tests {
}
}
}

impl From<(&'static str, &'static str)> for EncodedStrings {
fn from((expected, expected_preserved_proto): (&'static str, &'static str)) -> Self {
EncodedStrings {
Expand Down Expand Up @@ -132,6 +136,14 @@ mod tests {
}
}

fn verify_decode_err(encoded: &str, error: &str) {
let err = serde_json::from_str::<KitchenSink>(encoded)
.unwrap_err()
.to_string();

assert!(err.contains(error), "{}", err);
}

fn verify(decoded: &KitchenSink, expected: impl Into<EncodedStrings>) {
let expected = expected.into();
verify_encode(decoded, expected);
Expand Down Expand Up @@ -690,6 +702,49 @@ mod tests {
verify_decode(&decoded, r#"{"stringValue":null}"#);
verify_decode(&decoded, r#"{"uint32Value":null}"#);
verify_decode(&decoded, r#"{"uint64Value":null}"#);

// Test primitives are not nullable
verify_decode_err(r#"{"i32":null}"#, "data did not match any variant");
verify_decode_err(r#"{"u64":null}"#, "data did not match any variant");
verify_decode_err(r#"{"value":null}"#, "invalid type: null");
verify_decode_err(r#"{"bool":null}"#, "invalid type: null");
verify_decode_err(r#"{"string":null}"#, "invalid type: null");

// Test lists are not nullable
verify_decode_err(
r#"{"repeatedI32":null}"#,
"invalid type: null, expected a sequence",
);
verify_decode_err(
r#"{"repeatedI32":[null]}"#,
"data did not match any variant",
);
verify_decode_err(
r#"{"repeatedInt32Value":null}"#,
"invalid type: null, expected a sequence",
);
verify_decode_err(
r#"{"repeatedInt32Value":[null]}"#,
"data did not match any variant",
);

// Test maps are not nullable
verify_decode_err(
r#"{"stringDict":null}"#,
"invalid type: null, expected a map",
);
verify_decode_err(
r#"{"stringDict": {"foo": null}}"#,
"invalid type: null, expected a string ",
);
verify_decode_err(
r#"{"mapInt32Value":null}"#,
"invalid type: null, expected a map",
);
verify_decode_err(
r#"{"mapInt32Value":{"foo": null}}"#,
"data did not match any variant",
);
}

#[test]
Expand Down

0 comments on commit 7b0f22b

Please sign in to comment.