From ee933fccf5e0dd1e9f7f5d8e8d204625bebd5558 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Mon, 26 Jul 2021 18:48:15 +0200 Subject: [PATCH] Readd some tests and as_vec dynval transformation --- crates/eww/src/widgets/widget_definitions.rs | 13 ++- crates/eww/src/widgets/widget_node.rs | 39 -------- crates/simplexpr/src/dynval.rs | 75 +++++++-------- .../simplexpr__dynval__test__parse_vec-2.snap | 10 ++ .../simplexpr__dynval__test__parse_vec-3.snap | 12 +++ .../simplexpr__dynval__test__parse_vec-4.snap | 10 ++ .../simplexpr__dynval__test__parse_vec-5.snap | 11 +++ .../simplexpr__dynval__test__parse_vec-6.snap | 8 ++ .../simplexpr__dynval__test__parse_vec-7.snap | 12 +++ .../simplexpr__dynval__test__parse_vec-8.snap | 12 +++ .../simplexpr__dynval__test__parse_vec.snap | 10 ++ .../snapshots/yuck__config__test__config.snap | 93 +++++++++++++++++++ crates/yuck/src/config/test.rs | 27 +++--- 13 files changed, 232 insertions(+), 100 deletions(-) create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-2.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-3.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-4.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-5.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-6.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-7.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-8.snap create mode 100644 crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec.snap create mode 100644 crates/yuck/src/config/snapshots/yuck__config__test__config.snap diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index 15b6b75..4ea9b03 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -247,13 +247,12 @@ fn build_gtk_combo_box_text(bargs: &mut BuilderArgs) -> Result>> = Rc::new(RefCell::new(None)); resolve_block!(bargs, gtk_widget, { // @prop items - Items that should be displayed in the combo box - // TODO reimplement, obviously - //prop(items: as_vec) { - //gtk_widget.remove_all(); - //for i in items { - //gtk_widget.append_text(&i); - //} - //}, + prop(items: as_vec) { + gtk_widget.remove_all(); + for i in items { + gtk_widget.append_text(&i); + } + }, // @prop onchange - runs the code when a item was selected, replacing {} with the item as a string prop(onchange: as_string) { let old_id = on_change_handler_id.replace(Some( diff --git a/crates/eww/src/widgets/widget_node.rs b/crates/eww/src/widgets/widget_node.rs index 0f2c3bc..ff675cd 100644 --- a/crates/eww/src/widgets/widget_node.rs +++ b/crates/eww/src/widgets/widget_node.rs @@ -143,42 +143,3 @@ pub fn generate_generic_widget_node( })) } } - -//#[cfg(test)] -// mod test { -// use super::*; -// use crate::config::xml_ext::*; -// use maplit::hashmap; -//#[test] -// fn test_generic_generate() { -// let w_def1 = { -// let input = r#"{{nested1}}{{raw1}}"#; -// let document = roxmltree::Document::parse(input).unwrap(); -// let xml = XmlNode::from(document.root_element().clone()); -// WidgetDefinition::from_xml_element(&xml.as_element().unwrap()).unwrap() -//}; -// let w_def2 = { -// let input = r#""#; -// let document = roxmltree::Document::parse(input).unwrap(); -// let xml = XmlNode::from(document.root_element().clone()); -// WidgetDefinition::from_xml_element(&xml.as_element().unwrap()).unwrap() -//}; -// let w_use = { -// let input = r#""#; -// let document = roxmltree::Document::parse(input).unwrap(); -// let xml = XmlNode::from(document.root_element().clone()); -// WidgetUse::from_xml_node(xml).unwrap() -//}; - -// let generic = generate_generic_widget_node( -//&hashmap! { "foo".to_string() => w_def1, "bar".to_string() => w_def2 }, -//&HashMap::new(), -// w_use, -//) -//.unwrap(); - -//// TODO actually implement this test ._. - -// dbg!(&generic); -//// panic!("REEEEEEEEEE") -//} diff --git a/crates/simplexpr/src/dynval.rs b/crates/simplexpr/src/dynval.rs index 62b71bb..27edfc7 100644 --- a/crates/simplexpr/src/dynval.rs +++ b/crates/simplexpr/src/dynval.rs @@ -173,24 +173,30 @@ impl DynVal { } } - // pub fn as_vec(&self) -> Result> { - // match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) { - // Some(content) => { - // let mut items: Vec = content.split(',').map(|x: &str| x.to_string()).collect(); - // let mut removed = 0; - // for times_ran in 0..items.len() { - //// escapes `,` if there's a `\` before em - // if items[times_ran - removed].ends_with('\\') { - // items[times_ran - removed].pop(); - // let it = items.remove((times_ran + 1) - removed); - // items[times_ran - removed] += ","; - // items[times_ran - removed] += ⁢ - // removed += 1; - //} - // Ok(items) - //} - // None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }), - //} + pub fn as_vec(&self) -> Result> { + if self.0.is_empty() { + Ok(Vec::new()) + } else { + match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) { + Some(content) => { + let mut items: Vec = content.split(',').map(|x: &str| x.to_string()).collect(); + let mut removed = 0; + for times_ran in 0..items.len() { + // escapes `,` if there's a `\` before em + if items[times_ran - removed].ends_with('\\') { + items[times_ran - removed].pop(); + let it = items.remove((times_ran + 1) - removed); + items[times_ran - removed] += ","; + items[times_ran - removed] += ⁢ + removed += 1; + } + } + Ok(items) + } + None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }), + } + } + } pub fn as_json_value(&self) -> Result { serde_json::from_str::(&self.0) @@ -200,25 +206,16 @@ impl DynVal { #[cfg(test)] mod test { - // use super::*; - // use pretty_assertions::assert_eq; - //#[test] - // fn test_parse_vec() { - // assert_eq!(vec![""], parse_vec("[]".to_string()).unwrap(), "should be able to parse empty lists"); - // assert_eq!(vec!["hi"], parse_vec("[hi]".to_string()).unwrap(), "should be able to parse single element list"); - // assert_eq!( - // vec!["hi", "ho", "hu"], - // parse_vec("[hi,ho,hu]".to_string()).unwrap(), - //"should be able to parse three element list" - //); - // assert_eq!(vec!["hi,ho"], parse_vec("[hi\\,ho]".to_string()).unwrap(), "should be able to parse list with escaped comma"); - // assert_eq!( - // vec!["hi,ho", "hu"], - // parse_vec("[hi\\,ho,hu]".to_string()).unwrap(), - //"should be able to parse two element list with escaped comma" - //); - // assert!(parse_vec("".to_string()).is_err(), "Should fail when parsing empty string"); - // assert!(parse_vec("[a,b".to_string()).is_err(), "Should fail when parsing unclosed list"); - // assert!(parse_vec("a]".to_string()).is_err(), "Should fail when parsing unopened list"); - //} + use super::*; + #[test] + fn test_parse_vec() { + insta::assert_debug_snapshot!(DynVal::from_string("[]".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("[hi]".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("[hi,ho,hu]".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("[hi\\,ho]".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("[hi\\,ho,hu]".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("[a,b".to_string()).as_vec()); + insta::assert_debug_snapshot!(DynVal::from_string("a]".to_string()).as_vec()); + } } diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-2.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-2.snap new file mode 100644 index 0000000..bc9aa76 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-2.snap @@ -0,0 +1,10 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[hi]\".to_string()).as_vec()" + +--- +Ok( + [ + "hi", + ], +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-3.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-3.snap new file mode 100644 index 0000000..62819a1 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-3.snap @@ -0,0 +1,12 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[hi,ho,hu]\".to_string()).as_vec()" + +--- +Ok( + [ + "hi", + "ho", + "hu", + ], +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-4.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-4.snap new file mode 100644 index 0000000..485cc90 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-4.snap @@ -0,0 +1,10 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[hi\\\\,ho]\".to_string()).as_vec()" + +--- +Ok( + [ + "hi,ho", + ], +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-5.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-5.snap new file mode 100644 index 0000000..8b754d4 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-5.snap @@ -0,0 +1,11 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[hi\\\\,ho,hu]\".to_string()).as_vec()" + +--- +Ok( + [ + "hi,ho", + "hu", + ], +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-6.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-6.snap new file mode 100644 index 0000000..a458990 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-6.snap @@ -0,0 +1,8 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"\".to_string()).as_vec()" + +--- +Ok( + [], +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-7.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-7.snap new file mode 100644 index 0000000..c02e4e6 --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-7.snap @@ -0,0 +1,12 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[a,b\".to_string()).as_vec()" + +--- +Err( + ConversionError { + value: "[a,b", + target_type: "vec", + source: None, + }, +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-8.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-8.snap new file mode 100644 index 0000000..47e8caf --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec-8.snap @@ -0,0 +1,12 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"a]\".to_string()).as_vec()" + +--- +Err( + ConversionError { + value: "a]", + target_type: "vec", + source: None, + }, +) diff --git a/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec.snap b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec.snap new file mode 100644 index 0000000..b5d5e2c --- /dev/null +++ b/crates/simplexpr/src/snapshots/simplexpr__dynval__test__parse_vec.snap @@ -0,0 +1,10 @@ +--- +source: crates/simplexpr/src/dynval.rs +expression: "DynVal::from_string(\"[]\".to_string()).as_vec()" + +--- +Ok( + [ + "", + ], +) diff --git a/crates/yuck/src/config/snapshots/yuck__config__test__config.snap b/crates/yuck/src/config/snapshots/yuck__config__test__config.snap new file mode 100644 index 0000000..d633ab4 --- /dev/null +++ b/crates/yuck/src/config/snapshots/yuck__config__test__config.snap @@ -0,0 +1,93 @@ +--- +source: crates/yuck/src/config/test.rs +expression: config.unwrap() + +--- +Config( + widget_definitions: { + "bar": WidgetDefinition( + name: "bar", + expected_args: [ + AttrName("arg"), + AttrName("arg2"), + ], + widget: WidgetUse( + name: "foo", + attrs: Attributes( + span: Span(47, 47, 51), + attrs: { + AttrName("arg"): AttrEntry( + key_span: Span(52, 56, 0), + value: Literal(Span(57, 61, 0), DynVal("hi", None)), + ), + }, + ), + children: [], + span: Span(47, 62, 0), + name_span: Span(48, 51, 0), + ), + span: Span(9, 63, 0), + args_span: Span(24, 34, 0), + ), + }, + window_definitions: { + "some-window": WindowDefinition( + name: "some-window", + geometry: Some(WindowGeometry( + anchor_point: AnchorPoint( + x: START, + y: START, + ), + offset: Coords( + x: Pixels(0), + y: Pixels(0), + ), + size: Coords( + x: Percent(12), + y: Pixels(20), + ), + )), + stacking: Foreground, + monitor_number: Some(12), + widget: WidgetUse( + name: "bar", + attrs: Attributes( + span: Span(463, 463, 467), + attrs: { + AttrName("arg"): AttrEntry( + key_span: Span(468, 472, 0), + value: Literal(Span(473, 478, 0), DynVal("bla", None)), + ), + }, + ), + children: [], + span: Span(463, 479, 0), + name_span: Span(464, 467, 0), + ), + resizable: true, + backend_options: BackendWindowOptions( + wm_ignore: false, + sticky: true, + window_type: Dock, + struts: StrutDefinition( + side: Left, + dist: Pixels(30), + ), + ), + ), + }, + var_definitions: { + VarName("some_var"): VarDefinition( + name: VarName("some_var"), + initial_value: DynVal("bla", None), + span: Span(72, 95, 0), + ), + }, + script_vars: { + VarName("stuff"): Listen(ListenScriptVar( + name: VarName("stuff"), + command: "tail -f stuff", + command_span: Span(168, 183, 0), + )), + }, +) diff --git a/crates/yuck/src/config/test.rs b/crates/yuck/src/config/test.rs index fcf0963..1af2a61 100644 --- a/crates/yuck/src/config/test.rs +++ b/crates/yuck/src/config/test.rs @@ -3,31 +3,28 @@ use crate::{ parser::{self, ast::Ast, from_ast::FromAst, lexer::Lexer}, }; +use super::file_provider::YuckFiles; + #[test] fn test_config() { let input = r#" - (defwidget foo [arg] - "heyho") (defwidget bar [arg arg2] - "bla") + (foo :arg "hi")) (defvar some_var "bla") - (defpollvar stuff :interval "12s" "date") - (deftailvar stuff "tail -f stuff") + (defpoll stuff :interval "12s" "date") + (deflisten stuff "tail -f stuff") (defwindow some-window :stacking "fg" :monitor 12 :resizable true :geometry (geometry :width "12%" :height "20px") :reserve (struts :side "left" :distance "30px") - (foo :arg "bla")) + (bar :arg "bla")) "#; - - let lexer = Lexer::new(0, input.to_string()); - let p = parser::parser::ToplevelParser::new(); - let (span, parse_result) = p.parse(0, lexer).unwrap(); - // TODO implement another YuckFiles thing to test here again - // let config = Config::from_ast(Ast::List(span, parse_result)); - // insta::with_settings!({sort_maps => true}, { - // insta::assert_ron_snapshot!(config.unwrap()); - //}); + let mut files = YuckFiles::new(); + let (span, asts) = files.load_str("config.yuck".to_string(), input.to_string()).unwrap(); + let config = Config::generate(&mut files, asts); + insta::with_settings!({sort_maps => true}, { + insta::assert_ron_snapshot!(config.unwrap()); + }); }