use async_graphql::{Pos, ScalarType, Value};
use super::{AudioCodec, SubtitleCodec, VideoCodec};
fn door(sent: Value) -> Result<VideoCodec, String> {
<VideoCodec as ScalarType>::parse(sent)
.map_err(|refused| refused.into_server_error(Pos::default()).message)
}
#[test]
fn the_door_is_the_types_own_grammar() {
for sent in ["h264", "H264", "av1", "some_codec_9000"] {
assert_eq!(
door(Value::String(String::from(sent))),
Ok(VideoCodec::new(sent)),
"`{sent}`",
);
}
}
#[test]
fn every_spelling_is_written_back_as_the_canonical_one() {
for sent in ["h264", "H264", "H264"] {
let admitted = door(Value::String(String::from(sent))).expect("a string is admitted");
assert_eq!(
admitted.to_value(),
Value::String(String::from("h264")),
"`{sent}` is written back canonical",
);
}
let unknown = door(Value::String(String::from("some_codec_9000"))).expect("admitted");
assert_eq!(
unknown.to_value(),
Value::String(String::from("some_codec_9000")),
);
}
#[test]
fn a_non_string_is_refused_and_it_is_the_only_refusal() {
for sent in [
Value::Number(7.into()),
Value::Boolean(true),
Value::Null,
Value::List(vec![Value::String(String::from("h264"))]),
] {
assert!(door(sent.clone()).is_err(), "{sent:?} is refused");
}
for sent in ["", " ", "!!!", "日本語", "not a codec at all"] {
assert!(
door(Value::String(String::from(sent))).is_ok(),
"`{sent}` is admitted",
);
}
}
#[test]
fn validation_and_parsing_admit_the_same_values() {
let sent = [
Value::String(String::from("h264")),
Value::String(String::from("")),
Value::String(String::from("!!!")),
Value::Number(7.into()),
Value::Boolean(false),
Value::Null,
];
for value in sent {
assert_eq!(
<VideoCodec as ScalarType>::is_valid(&value),
VideoCodec::parse(value.clone()).is_ok(),
"{value:?} — the validator and the parser must agree",
);
assert_eq!(
<AudioCodec as ScalarType>::is_valid(&value),
AudioCodec::parse(value.clone()).is_ok(),
"{value:?}",
);
assert_eq!(
<SubtitleCodec as ScalarType>::is_valid(&value),
SubtitleCodec::parse(value.clone()).is_ok(),
"{value:?}",
);
}
}
#[test]
fn the_other_two_doors_are_the_same_door() {
let audio = AudioCodec::parse(Value::String(String::from("AAC"))).expect("admitted");
assert_eq!(audio, AudioCodec::new("aac"));
assert_eq!(audio.to_value(), Value::String(String::from("aac")));
let subtitle = SubtitleCodec::parse(Value::String(String::from("SubRip"))).expect("admitted");
assert_eq!(subtitle, SubtitleCodec::new("subrip"));
assert_eq!(subtitle.to_value(), Value::String(String::from("subrip")));
assert!(AudioCodec::parse(Value::Number(7.into())).is_err());
assert!(SubtitleCodec::parse(Value::Number(7.into())).is_err());
}