saphyr-serde/parser/tests/spec_test.rs

135 lines
3.5 KiB
Rust
Raw Normal View History

2015-05-27 13:57:42 +00:00
#![allow(dead_code)]
2015-05-30 10:49:54 +00:00
#![allow(non_upper_case_globals)]
2024-02-08 06:12:14 +00:00
extern crate yaml_rust2;
2015-05-27 13:57:42 +00:00
2024-02-08 06:12:14 +00:00
use yaml_rust2::parser::{Event, EventReceiver, Parser};
use yaml_rust2::scanner::TScalarStyle;
2015-05-27 13:57:42 +00:00
2017-05-08 18:35:53 +00:00
// These names match the names used in the C++ test suite.
2023-08-11 23:54:46 +00:00
#[cfg_attr(feature = "cargo-clippy", allow(clippy::enum_variant_names))]
2015-05-27 13:57:42 +00:00
#[derive(Clone, PartialEq, PartialOrd, Debug)]
enum TestEvent {
OnDocumentStart,
OnDocumentEnd,
OnSequenceStart,
OnSequenceEnd,
OnMapStart,
OnMapEnd,
OnScalar,
OnAlias,
OnNull,
}
struct YamlChecker {
2018-09-15 16:49:04 +00:00
pub evs: Vec<TestEvent>,
2015-05-27 13:57:42 +00:00
}
impl EventReceiver for YamlChecker {
fn on_event(&mut self, ev: Event) {
let tev = match ev {
2015-05-27 13:57:42 +00:00
Event::DocumentStart => TestEvent::OnDocumentStart,
Event::DocumentEnd => TestEvent::OnDocumentEnd,
2015-05-28 17:56:03 +00:00
Event::SequenceStart(..) => TestEvent::OnSequenceStart,
2015-05-27 13:57:42 +00:00
Event::SequenceEnd => TestEvent::OnSequenceEnd,
2015-05-28 17:56:03 +00:00
Event::MappingStart(..) => TestEvent::OnMapStart,
2015-05-27 13:57:42 +00:00
Event::MappingEnd => TestEvent::OnMapEnd,
2018-09-15 16:49:04 +00:00
Event::Scalar(ref v, style, _, _) => {
if v == "~" && style == TScalarStyle::Plain {
2015-05-27 13:57:42 +00:00
TestEvent::OnNull
} else {
TestEvent::OnScalar
}
2018-09-15 16:49:04 +00:00
}
2015-05-28 17:56:03 +00:00
Event::Alias(_) => TestEvent::OnAlias,
2018-09-15 16:49:04 +00:00
_ => return, // ignore other events
2015-05-27 13:57:42 +00:00
};
self.evs.push(tev);
}
}
fn str_to_test_events(docs: &str) -> Vec<TestEvent> {
2018-09-15 16:49:04 +00:00
let mut p = YamlChecker { evs: Vec::new() };
2015-05-27 13:57:42 +00:00
let mut parser = Parser::new(docs.chars());
parser.load(&mut p, true).unwrap();
p.evs
}
macro_rules! assert_next {
2018-09-15 16:49:04 +00:00
($v:expr, $p:pat) => {
2015-05-27 13:57:42 +00:00
match $v.next().unwrap() {
2018-09-15 16:49:04 +00:00
$p => {}
e => {
2023-12-26 17:08:21 +00:00
panic!("unexpected event: {:?} (expected {:?})", e, stringify!($p));
2018-09-15 16:49:04 +00:00
}
2015-05-27 13:57:42 +00:00
}
2018-09-15 16:49:04 +00:00
};
2015-05-27 13:57:42 +00:00
}
// auto generated from handler_spec_test.cpp
include!("specexamples.rs.inc");
include!("spec_test.rs.inc");
2015-05-28 18:26:37 +00:00
// hand-crafted tests
//#[test]
//fn test_hc_alias() {
//}
#[test]
fn test_mapvec_legal() {
2024-02-08 06:12:14 +00:00
use yaml_rust2::yaml::{Hash, Yaml};
use yaml_rust2::{YamlEmitter, YamlLoader};
2018-09-15 16:49:04 +00:00
// Emitting a `map<map<seq<_>>, _>` should result in legal yaml that
// we can parse.
2023-08-11 23:54:46 +00:00
let key = vec![Yaml::Integer(1), Yaml::Integer(2), Yaml::Integer(3)];
2018-09-15 16:49:04 +00:00
let mut keyhash = Hash::new();
keyhash.insert(Yaml::String("key".into()), Yaml::Array(key));
2023-08-11 23:54:46 +00:00
let val = vec![Yaml::Integer(4), Yaml::Integer(5), Yaml::Integer(6)];
2018-09-15 16:49:04 +00:00
let mut hash = Hash::new();
hash.insert(Yaml::Hash(keyhash), Yaml::Array(val));
let mut out_str = String::new();
{
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.dump(&Yaml::Hash(hash)).unwrap();
}
// At this point, we are tempted to naively render like this:
//
// ```yaml
// ---
// {key:
// - 1
// - 2
// - 3}:
// - 4
// - 5
// - 6
// ```
//
// However, this doesn't work, because the key sequence [1, 2, 3] is
// rendered in block mode, which is not legal (as far as I can tell)
// inside the flow mode of the key. We need to either fully render
// everything that's in a key in flow mode (which may make for some
// long lines), or use the explicit map identifier '?':
//
// ```yaml
// ---
// ?
// key:
// - 1
// - 2
// - 3
// :
// - 4
// - 5
// - 6
// ```
YamlLoader::load_from_str(&out_str).unwrap();
}