Readd some tests and as_vec dynval transformation

This commit is contained in:
elkowar 2021-07-26 18:48:15 +02:00
parent 44d7ea7a09
commit ee933fccf5
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
13 changed files with 232 additions and 100 deletions

View file

@ -247,13 +247,12 @@ fn build_gtk_combo_box_text(bargs: &mut BuilderArgs) -> Result<gtk::ComboBoxText
let on_change_handler_id: Rc<RefCell<Option<glib::SignalHandlerId>>> = 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(

View file

@ -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#"<def name="foo"><box><box>{{nested1}}{{raw1}}</box></box></def>"#;
// 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#"<def name="bar"><box><foo nested1="{{nested2}}" raw1="raw value"/></box></def>"#;
// 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#"<bar nested2="{{in_root}}"/>"#;
// 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")
//}

View file

@ -173,24 +173,30 @@ impl DynVal {
}
}
// pub fn as_vec(&self) -> Result<Vec<String>> {
// match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) {
// Some(content) => {
// let mut items: Vec<String> = 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] += &it;
// removed += 1;
//}
// Ok(items)
//}
// None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }),
//}
pub fn as_vec(&self) -> Result<Vec<String>> {
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<String> = 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] += &it;
removed += 1;
}
}
Ok(items)
}
None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }),
}
}
}
pub fn as_json_value(&self) -> Result<serde_json::Value> {
serde_json::from_str::<serde_json::Value>(&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());
}
}

View file

@ -0,0 +1,10 @@
---
source: crates/simplexpr/src/dynval.rs
expression: "DynVal::from_string(\"[hi]\".to_string()).as_vec()"
---
Ok(
[
"hi",
],
)

View file

@ -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",
],
)

View file

@ -0,0 +1,10 @@
---
source: crates/simplexpr/src/dynval.rs
expression: "DynVal::from_string(\"[hi\\\\,ho]\".to_string()).as_vec()"
---
Ok(
[
"hi,ho",
],
)

View file

@ -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",
],
)

View file

@ -0,0 +1,8 @@
---
source: crates/simplexpr/src/dynval.rs
expression: "DynVal::from_string(\"\".to_string()).as_vec()"
---
Ok(
[],
)

View file

@ -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,
},
)

View file

@ -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,
},
)

View file

@ -0,0 +1,10 @@
---
source: crates/simplexpr/src/dynval.rs
expression: "DynVal::from_string(\"[]\".to_string()).as_vec()"
---
Ok(
[
"",
],
)

View file

@ -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),
)),
},
)

View file

@ -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());
});
}