From 2495cd60c34061b4e2b383beee6947787388f4cf Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 5 Nov 2018 03:39:50 +0100 Subject: parser: Add tests for initial whitespace and comments in mappings file This broke recently I think. You can no longer have a mappings file that starts with blank lines or comment lines. Removing the empty and blank handler in `definitions()` fixes the tests added by this commit but fails these: parser::tests::map_group_empty_input_does_not_fail parser::tests::map_group_skipped_input_outputs_default_map_group Using this diff: diff --git a/src/parser.rs b/src/parser.rs index 3f3d7b9..d18b56a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -629,11 +629,11 @@ where I: Stream, I::Error: ParseError, { - or( - ( - blank(), - eof(), - ).map(|_| MapGroup::default()), + // or( + // ( + // blank(), + // eof(), + // ).map(|_| MapGroup::default()), ( definitions(), eof(), @@ -661,8 +661,8 @@ where } map_group - }), - ) + }) + // ) } fn comment() -> impl Parser Need to figure out some way to get both sets of tests to pass. --- src/parser.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) (limited to 'src') diff --git a/src/parser.rs b/src/parser.rs index e198ee3..3f3d7b9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1494,6 +1494,106 @@ cmd /usr/bin/say hello assert_eq!(result, Ok(expected)); } + #[test] + fn map_group_allows_initial_whitespace() { + let text = " +map x +"; + let result = map_group().easy_parse(text).map(|t| t.0); + + let mut maps: MapCollection = HashMap::new(); + maps.insert( + vec![HeadphoneButton::Up], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_SOUND_UP), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + maps.insert( + vec![HeadphoneButton::Play], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::Character(Character::new('x')), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + maps.insert( + vec![HeadphoneButton::Down], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_SOUND_DOWN), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + let expected = MapGroup { + maps: maps, + modes: HashMap::new(), + }; + + assert_eq!(result, Ok(expected)); + } + + #[test] + fn map_group_allows_initial_comment_lines() { + let text = "# A comment + +cmd echo test +"; + let result = map_group().easy_parse(text).map(|t| t.0); + + let mut maps: MapCollection = HashMap::new(); + maps.insert( + vec![HeadphoneButton::Up], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_SOUND_UP), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + maps.insert( + vec![HeadphoneButton::Play], + MapAction { + action: Action::Map( + vec![KeyboardKeyWithModifiers::new( + KeyboardKey::NXKey(key_code::NX_KEYTYPE_PLAY), + vec![], + )] + ), + kind: MapKind::Map, + }, + ); + maps.insert( + vec![HeadphoneButton::Down], + MapAction { + action: Action::String("echo test".to_owned()), + kind: MapKind::Command, + }, + ); + let expected = MapGroup { + maps: maps, + modes: HashMap::new(), + }; + + assert_eq!(result, Ok(expected)); + } + #[test] fn map_group_empty_input_does_not_fail() { let text = ""; -- cgit v1.2.3