Skip to content

Commit bf41121

Browse files
author
Michael Bryan
committed
Added the rest of the tests
1 parent 508b682 commit bf41121

File tree

1 file changed

+118
-1
lines changed

1 file changed

+118
-1
lines changed

fluent-syntax/src/serializer.rs

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl TextWriter {
410410
}
411411

412412
#[cfg(test)]
413-
mod tests {
413+
mod serialize_resource_tests {
414414
use super::*;
415415

416416
#[test]
@@ -594,4 +594,121 @@ mod tests {
594594
"foo = { \"Escaped \\\" quote\" }\n",
595595
);
596596
round_trip_test!(unicode_escape_sequence, "foo = { \"\\u0065\" }\n",);
597+
598+
// Serialize padding around comments
599+
600+
round_trip_test!(
601+
standalone_comment_has_not_padding_when_first,
602+
"# Comment A\n\nfoo = Foo\n\n# Comment B\n\nbar = Bar\n"
603+
);
604+
round_trip_test!(
605+
group_comment_has_not_padding_when_first,
606+
"## Group A\n\nfoo = Foo\n\n## Group B\n\nbar = Bar\n"
607+
);
608+
round_trip_test!(
609+
resource_comment_has_not_padding_when_first,
610+
"### Resource Comment A\n\nfoo = Foo\n\n### Resource Comment B\n\nbar = Bar\n"
611+
);
612+
}
613+
614+
#[cfg(test)]
615+
mod serialize_expression_tests {
616+
use super::*;
617+
618+
macro_rules! expression_test {
619+
($name:ident, $input:expr) => {
620+
#[test]
621+
fn $name() {
622+
let input_without_tabs = $input.replace("\t", " ");
623+
let src = format!("foo = {{ {} }}", input_without_tabs);
624+
let resource = crate::parser::parse(&src).unwrap();
625+
626+
// extract the first expression from the value of the first
627+
// message
628+
assert_eq!(resource.body.len(), 1);
629+
let first_item = &resource.body[0];
630+
let message = match first_item {
631+
ResourceEntry::Entry(Entry::Message(msg)) => msg,
632+
other => panic!("Expected a message but found {:#?}", other),
633+
};
634+
let value = message.value.as_ref().expect("The message has a value");
635+
assert_eq!(value.elements.len(), 1);
636+
let expr = match &value.elements[0] {
637+
PatternElement::Placeable(expr) => expr,
638+
other => panic!("Expected a single expression but found {:#?}", other),
639+
};
640+
641+
// we've finally extracted the first expression, now we can
642+
// actually serialize it and finish the test
643+
let mut serializer = Serializer::new(Options::default());
644+
serializer.serialize_expression(expr).unwrap();
645+
let got = serializer.into_serialized_text();
646+
647+
assert_eq!(got, input_without_tabs);
648+
}
649+
};
650+
}
651+
652+
expression_test!(string_expression, "\"str\"");
653+
expression_test!(number_expression, "3");
654+
expression_test!(message_reference, "msg");
655+
expression_test!(external_arguemnt, "$ext");
656+
expression_test!(attribute_expression, "msg.attr");
657+
expression_test!(call_expression, "BUILTIN(3.14, kwarg: \"value\")");
658+
expression_test!(select_expression, "$num ->\n\t*[one] One\n");
659+
}
660+
661+
#[cfg(test)]
662+
mod serialize_variant_key_tests {
663+
use super::*;
664+
665+
macro_rules! variant_key_test {
666+
($name:ident, $input:expr => $( $keys:expr ),+ $(,)?) => {
667+
#[test]
668+
#[allow(unused_assignments)]
669+
fn $name() {
670+
let input_without_tabs = $input.replace("\t", " ");
671+
let src = format!("foo = {{ {}\n }}", input_without_tabs);
672+
let resource = crate::parser::parse(&src).unwrap();
673+
674+
// extract variant from the first expression from the value of
675+
// the first message
676+
assert_eq!(resource.body.len(), 1);
677+
let first_item = &resource.body[0];
678+
let message = match first_item {
679+
ResourceEntry::Entry(Entry::Message(msg)) => msg,
680+
other => panic!("Expected a message but found {:#?}", other),
681+
};
682+
let value = message.value.as_ref().expect("The message has a value");
683+
assert_eq!(value.elements.len(), 1);
684+
let variants = match &value.elements[0] {
685+
PatternElement::Placeable(Expression::SelectExpression { variants, .. }) => variants,
686+
other => panic!("Expected a single select expression but found {:#?}", other),
687+
};
688+
689+
let mut ix = 0;
690+
691+
$(
692+
let variant_key = &variants[ix].key;
693+
694+
// we've finally extracted the variant key, now we can
695+
// actually serialize it and finish the test
696+
let mut serializer = Serializer::new(Options::default());
697+
serializer.serialize_variant_key(variant_key).unwrap();
698+
let got = serializer.into_serialized_text();
699+
700+
assert_eq!(got, $keys);
701+
702+
ix += 1;
703+
)*
704+
}
705+
};
706+
}
707+
708+
variant_key_test!(identifiers, "$num ->\n\t[one] One\n\t*[other] Other" => "one", "other");
709+
variant_key_test!(
710+
number_literals,
711+
"$num ->\n\t[-123456789] Minus a lot\n\t[0] Zero\n\t*[3.14] Pi\n\t[007] James"
712+
=> "-123456789", "0", "3.14", "007",
713+
);
597714
}

0 commit comments

Comments
 (0)