From cb8fd26922fd1cfad4ceadeb89e48544531a178e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 9 May 2025 23:00:36 +0300 Subject: [PATCH] collect tests --- dsl/src/lib.rs | 16 ---- output/src/lib.rs | 1 - output/src/ops/bsp.rs | 6 +- proc/src/lib.rs | 190 ++++++++++++++++++++++++++++++++++++++-- proc/src/proc_expose.rs | 106 ---------------------- proc/src/proc_view.rs | 122 +------------------------- tengri/src/lib.rs | 47 +++++++++- tui/examples/tui.rs | 3 + 8 files changed, 234 insertions(+), 257 deletions(-) diff --git a/dsl/src/lib.rs b/dsl/src/lib.rs index 52f8281..c685572 100644 --- a/dsl/src/lib.rs +++ b/dsl/src/lib.rs @@ -143,22 +143,6 @@ mod dsl; pub use self::dsl::*; Ok(()) } -#[cfg(test)] #[test] fn test_dsl_context () { - struct Test; - #[tengri_proc::expose] - impl Test { - fn some_bool (&self) -> bool { - true - } - } - assert_eq!(Test.get(&Value::Sym(":false")), Some(false)); - assert_eq!(Test.get(&Value::Sym(":true")), Some(true)); - assert_eq!(Test.get(&Value::Sym(":some-bool")), Some(true)); - assert_eq!(Test.get(&Value::Sym(":missing-bool")), None); - assert_eq!(Test.get(&Value::Num(0)), Some(false)); - assert_eq!(Test.get(&Value::Num(1)), Some(true)); -} - //#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> { //// Let's pretend to render some view. //let source = include_str!("../../tek/src/view_arranger.edn"); diff --git a/output/src/lib.rs b/output/src/lib.rs index 064d8da..45bf90a 100644 --- a/output/src/lib.rs +++ b/output/src/lib.rs @@ -64,7 +64,6 @@ pub type Perhaps = Result, Box>; #[cfg(test)] mod test { use crate::*; use proptest::{prelude::*, option::of}; - use proptest::option::of; proptest! { #[test] fn proptest_direction ( diff --git a/output/src/ops/bsp.rs b/output/src/ops/bsp.rs index 0ab80c3..fe48efc 100644 --- a/output/src/ops/bsp.rs +++ b/output/src/ops/bsp.rs @@ -1,7 +1,11 @@ use crate::*; pub use Direction::*; /// A split or layer. -pub struct Bsp(Direction, X, Y); +pub struct Bsp( + pub(crate) Direction, + pub(crate) A, + pub(crate) B, +); impl, B: Content> Content for Bsp { fn layout (&self, outer: E::Area) -> E::Area { let [_, _, c] = self.areas(outer); diff --git a/proc/src/lib.rs b/proc/src/lib.rs index f566fa0..132d416 100644 --- a/proc/src/lib.rs +++ b/proc/src/lib.rs @@ -21,14 +21,7 @@ mod proc_view; mod proc_expose; mod proc_command; -#[proc_macro_attribute] -pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream { - use self::proc_view::{ViewDef, ViewMeta, ViewImpl}; - write(ViewDef( - parse_macro_input!(meta as ViewMeta), - parse_macro_input!(item as ViewImpl), - )) -} +#[cfg(test)] use syn::parse_quote as pq; #[proc_macro_attribute] pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream { @@ -48,6 +41,15 @@ pub fn command (meta: TokenStream, item: TokenStream) -> TokenStream { )) } +#[proc_macro_attribute] +pub fn view (meta: TokenStream, item: TokenStream) -> TokenStream { + use self::proc_view::{ViewDef, ViewMeta, ViewImpl}; + write(ViewDef( + parse_macro_input!(meta as ViewMeta), + parse_macro_input!(item as ViewImpl), + )) +} + pub(crate) fn write (t: T) -> TokenStream { let mut out = TokenStream2::new(); t.to_tokens(&mut out); @@ -67,3 +69,175 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) { out.append(token); } } + +#[cfg(test)] #[test] fn test_proc_view () { + let x: crate::proc_view::ViewMeta = pq! { SomeOutput }; + let output: Ident = pq! { SomeOutput }; + assert_eq!(x.output, output); + + // TODO + let x: crate::proc_view::ViewImpl = pq! { + impl Foo { + /// docstring1 + #[tengri::view(":view1")] #[bar] fn a_view () {} + + #[baz] + /// docstring2 + #[baz] fn is_not_view () {} + } + }; + let expected_target: Ident = pq! { Foo }; + //assert_eq!(x.target, expected_target); + //assert_eq!(x.items.len(), 2); + //assert_eq!(x.items[0].item, pq! { + ///// docstring1 + //#[bar] fn a_view () {} + //}); + //assert_eq!(x.items[1].item, pq! { + //#[baz] + ///// docstring2 + //#[baz] fn is_not_view () {} + //}); + //assert_eq!(x.syms, vec![ + //ViewArm( { symbol: pq! { ":view1" }, name: pq! { a_view }, }, + //]); + // FIXME + //let parsed: ViewDefinition = pq! { + //#[tengri_proc::view(SomeOutput)] + //impl SomeView { + //#[tengri::view(":view-1")] + //fn view_1 (&self) -> impl Content + use<'_> { + //"view-1" + //} + //} + //}; + //let written = quote! { #parsed }; + //assert_eq!(format!("{written}"), format!("{}", quote! { + //impl SomeView { + //fn view_1 (&self) -> impl Content + use<'_> { + //"view-1" + //} + //} + ///// Generated by [tengri_proc]. + //impl ::tengri::output::Content for SomeView { + //fn content (&self) -> impl Render { + //self.size.of(::tengri::output::View(self, self.config.view)) + //} + //} + ///// Generated by [tengri_proc]. + //impl<'a> ::tengri::dsl::ViewContext<'a, SomeOutput> for SomeView { + //fn get_content_sym (&'a self, value: &Value<'a>) -> Option> { + //match value { + //::tengri::dsl::Value::Sym(":view-1") => self.view_1().boxed(), + //_ => panic!("expected Sym(content), got: {value:?}") + //} + //} + //} + //})); +} + +//#[cfg(test)] #[test] fn test_expose_definition () { + // TODO + //let parsed: ExposeImpl = pq! { + ////#[tengri_proc::expose] + //impl Something { + //fn something () -> bool {} + //} + //}; + //// FIXME: + ////assert_eq!( + ////format!("{}", quote! { #parsed }), + ////format!("{}", quote! { + ////impl Something { + ////fn something () {} + ////} + ////impl ::tengri::Context for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option { + ////Some(match dsl { + ////::tengri::Value::Sym(":true") => true, + ////::tengri::Value::Sym(":false") => false, + ////::tengri::Value::Sym(":bool1") => true || false, + ////_ => return None + ////}) + ////} + ////} + ////}) + ////); + + //let parsed: ExposeImpl = pq! { + ////#[tengri_proc::expose] + //impl Something { + //#[tengri::expose(bool)] { + //":bool1" => true || false, + //} + //#[tengri::expose(u16)] { + //":u161" => 0 + 1, + //} + //#[tengri::expose(usize)] { + //":usize1" => 1 + 2, + //} + //#[tengri::expose(Arc)] { + //":arcstr1" => "foo".into(), + //} + //#[tengri::expose(Option>)] { + //":optarcstr1" => Some("bar".into()), + //":optarcstr2" => Some("baz".into()), + //} + //fn something () {} + //} + //}; + //// FIXME: + ////assert_eq!( + ////format!("{}", quote! { #parsed }), + ////format!("{}", quote! { + ////impl Something { + ////fn something () {} + ////} + ////impl ::tengri::Context> for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option> { + ////Some(match dsl { + ////::tengri::Value::Sym(":arcstr1") => "foo".into(), + ////_ => return None + ////}) + ////} + ////} + ////impl ::tengri::Context>> for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option>> { + ////Some(match dsl { + ////::tengri::Value::Sym(":optarcstr1") => Some("bar".into()), + ////::tengri::Value::Sym(":optarcstr2") => Some("baz".into()), + ////_ => return None + ////}) + ////} + ////} + ////impl ::tengri::Context for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option { + ////Some(match dsl { + ////::tengri::Value::Sym(":true") => true, + ////::tengri::Value::Sym(":false") => false, + ////::tengri::Value::Sym(":bool1") => true || false, + ////_ => return None + ////}) + ////} + ////} + ////impl ::tengri::Context for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option { + ////Some(match dsl { + ////::tengri::Value::Num(n) => *n as u16, + ////::tengri::Value::Sym(":u161") => 0 + 1, + ////_ => return None + ////}) + ////} + ////} + ////impl ::tengri::Context for Something { + ////fn get (&self, dsl: &::tengri::Value) -> Option { + ////Some(match dsl { + ////::tengri::Value::Num(n) => *n as usize, + ////::tengri::Value::Sym(":usize1") => 1 + 2, + ////_ => return None + ////}) + ////} + ////} + ////}) + ////) +//} diff --git a/proc/src/proc_expose.rs b/proc/src/proc_expose.rs index 36fc418..4a11347 100644 --- a/proc/src/proc_expose.rs +++ b/proc/src/proc_expose.rs @@ -189,109 +189,3 @@ impl ToTokens for ExposeType { self.0.to_tokens(out) } } - -#[cfg(test)] #[test] fn test_expose_definition () { - // TODO - //let parsed: ExposeImpl = pq! { - ////#[tengri_proc::expose] - //impl Something { - //fn something () -> bool {} - //} - //}; - //// FIXME: - ////assert_eq!( - ////format!("{}", quote! { #parsed }), - ////format!("{}", quote! { - ////impl Something { - ////fn something () {} - ////} - ////impl ::tengri::Context for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option { - ////Some(match dsl { - ////::tengri::Value::Sym(":true") => true, - ////::tengri::Value::Sym(":false") => false, - ////::tengri::Value::Sym(":bool1") => true || false, - ////_ => return None - ////}) - ////} - ////} - ////}) - ////); - - //let parsed: ExposeImpl = pq! { - ////#[tengri_proc::expose] - //impl Something { - //#[tengri::expose(bool)] { - //":bool1" => true || false, - //} - //#[tengri::expose(u16)] { - //":u161" => 0 + 1, - //} - //#[tengri::expose(usize)] { - //":usize1" => 1 + 2, - //} - //#[tengri::expose(Arc)] { - //":arcstr1" => "foo".into(), - //} - //#[tengri::expose(Option>)] { - //":optarcstr1" => Some("bar".into()), - //":optarcstr2" => Some("baz".into()), - //} - //fn something () {} - //} - //}; - //// FIXME: - ////assert_eq!( - ////format!("{}", quote! { #parsed }), - ////format!("{}", quote! { - ////impl Something { - ////fn something () {} - ////} - ////impl ::tengri::Context> for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option> { - ////Some(match dsl { - ////::tengri::Value::Sym(":arcstr1") => "foo".into(), - ////_ => return None - ////}) - ////} - ////} - ////impl ::tengri::Context>> for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option>> { - ////Some(match dsl { - ////::tengri::Value::Sym(":optarcstr1") => Some("bar".into()), - ////::tengri::Value::Sym(":optarcstr2") => Some("baz".into()), - ////_ => return None - ////}) - ////} - ////} - ////impl ::tengri::Context for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option { - ////Some(match dsl { - ////::tengri::Value::Sym(":true") => true, - ////::tengri::Value::Sym(":false") => false, - ////::tengri::Value::Sym(":bool1") => true || false, - ////_ => return None - ////}) - ////} - ////} - ////impl ::tengri::Context for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option { - ////Some(match dsl { - ////::tengri::Value::Num(n) => *n as u16, - ////::tengri::Value::Sym(":u161") => 0 + 1, - ////_ => return None - ////}) - ////} - ////} - ////impl ::tengri::Context for Something { - ////fn get (&self, dsl: &::tengri::Value) -> Option { - ////Some(match dsl { - ////::tengri::Value::Num(n) => *n as usize, - ////::tengri::Value::Sym(":usize1") => 1 + 2, - ////_ => return None - ////}) - ////} - ////} - ////}) - ////) -} diff --git a/proc/src/proc_view.rs b/proc/src/proc_view.rs index 25ec680..7fdd1fd 100644 --- a/proc/src/proc_view.rs +++ b/proc/src/proc_view.rs @@ -5,7 +5,7 @@ pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl); #[derive(Debug, Clone)] pub(crate) struct ViewMeta { - output: Ident, + pub(crate) output: Ident, } #[derive(Debug, Clone)] @@ -114,123 +114,3 @@ impl ToTokens for ViewArm { out.append(Punct::new(',', Alone)); } } - -//impl ToTokens for ViewSym { - //fn to_tokens (&self, out: &mut TokenStream2) { - //out.append(Punct::new(':', Joint)); - //out.append(Punct::new(':', Alone)); - //out.append(Ident::new("tengri", Span::call_site())); - //out.append(Punct::new(':', Joint)); - //out.append(Punct::new(':', Alone)); - //out.append(Ident::new("dsl", Span::call_site())); - //out.append(Punct::new(':', Joint)); - //out.append(Punct::new(':', Alone)); - //out.append(Ident::new("Value", Span::call_site())); - //out.append(Punct::new(':', Joint)); - //out.append(Punct::new(':', Alone)); - //out.append(Ident::new("Sym", Span::call_site())); - //out.append(Group::new(Delimiter::Parenthesis, { - //let mut out = TokenStream2::new(); - //out.append(self.symbol.clone()); - //out - //})); - //out.append(Punct::new('=', Joint)); - //out.append(Punct::new('>', Alone)); - //out.append(Ident::new("Some", Span::call_site())); - //out.append(Group::new(Delimiter::Parenthesis, { - //let mut out = TokenStream2::new(); - //out.append(Ident::new("self", Span::call_site())); - //out.append(Punct::new('.', Alone)); - //out.append(self.name.clone()); - //out.append(Group::new(Delimiter::Parenthesis, TokenStream2::new())); - //out.append(Punct::new('.', Alone)); - //out.append(Ident::new("boxed", Span::call_site())); - //out.append(Group::new(Delimiter::Parenthesis, TokenStream2::new())); - //out - //})); - //out.append(Punct::new(',', Alone)); - //} -//} - -//impl std::cmp::PartialEq for ViewItem { - //fn eq (&self, other: &Self) -> bool { - //self.item == other.item && (format!("{:?}", self.expose) == format!("{:?}", other.expose)) - //} -//} - -//impl std::cmp::PartialEq for ViewSym { - //fn eq (&self, other: &Self) -> bool { - //self.name == other.name && (format!("{}", self.symbol) == format!("{}", other.symbol)) - //} -//} - -#[cfg(test)] #[test] fn test_view_meta () { - let x: ViewMeta = pq! { SomeOutput }; - let output: Ident = pq! { SomeOutput }; - assert_eq!(x.output, output); -} - -#[cfg(test)] #[test] fn test_view_impl () { - // TODO - let x: ViewImpl = pq! { - impl Foo { - /// docstring1 - #[tengri::view(":view1")] #[bar] fn a_view () {} - - #[baz] - /// docstring2 - #[baz] fn is_not_view () {} - } - }; - let expected_target: Ident = pq! { Foo }; - //assert_eq!(x.target, expected_target); - //assert_eq!(x.items.len(), 2); - //assert_eq!(x.items[0].item, pq! { - ///// docstring1 - //#[bar] fn a_view () {} - //}); - //assert_eq!(x.items[1].item, pq! { - //#[baz] - ///// docstring2 - //#[baz] fn is_not_view () {} - //}); - //assert_eq!(x.syms, vec![ - //ViewArm( { symbol: pq! { ":view1" }, name: pq! { a_view }, }, - //]); -} - -#[cfg(test)] #[test] fn test_view_definition () { - // FIXME - //let parsed: ViewDefinition = pq! { - //#[tengri_proc::view(SomeOutput)] - //impl SomeView { - //#[tengri::view(":view-1")] - //fn view_1 (&self) -> impl Content + use<'_> { - //"view-1" - //} - //} - //}; - //let written = quote! { #parsed }; - //assert_eq!(format!("{written}"), format!("{}", quote! { - //impl SomeView { - //fn view_1 (&self) -> impl Content + use<'_> { - //"view-1" - //} - //} - ///// Generated by [tengri_proc]. - //impl ::tengri::output::Content for SomeView { - //fn content (&self) -> impl Render { - //self.size.of(::tengri::output::View(self, self.config.view)) - //} - //} - ///// Generated by [tengri_proc]. - //impl<'a> ::tengri::dsl::ViewContext<'a, SomeOutput> for SomeView { - //fn get_content_sym (&'a self, value: &Value<'a>) -> Option> { - //match value { - //::tengri::dsl::Value::Sym(":view-1") => self.view_1().boxed(), - //_ => panic!("expected Sym(content), got: {value:?}") - //} - //} - //} - //})); -} diff --git a/tengri/src/lib.rs b/tengri/src/lib.rs index 7703a14..53850cd 100644 --- a/tengri/src/lib.rs +++ b/tengri/src/lib.rs @@ -24,6 +24,9 @@ fn do_thing (state: &mut Test) -> Perhaps { Ok(None) } + fn do_thing_arg (state: &mut Test, arg: usize) -> Perhaps { + Ok(None) + } fn do_sub (state: &mut Test, command: TestSubcommand) -> Perhaps { Ok(command.execute(state)?.map(|command|Self::DoSub { command })) } @@ -32,11 +35,17 @@ fn do_other_thing (state: &mut Test) -> Perhaps { Ok(None) } + fn do_other_thing_arg (state: &mut Test, arg: usize) -> Perhaps { + Ok(None) + } } let mut test = Test { - keys: InputMap::new( - "(@a do-thing) (@b do-sub do-other-thing)".into() - ) + keys: InputMap::new(" + (@a do-thing) + (@b do-thing-arg 0) + (@c do-sub do-other-thing) + (@d do-sub do-other-thing-arg 0) + ".into()) }; assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent { kind: KeyEventKind::Press, @@ -50,11 +59,41 @@ modifiers: KeyModifiers::NONE, state: KeyEventState::NONE, })))?); - assert_eq!(None, test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent { + assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent { kind: KeyEventKind::Press, code: KeyCode::Char('c'), modifiers: KeyModifiers::NONE, state: KeyEventState::NONE, })))?); + assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent { + kind: KeyEventKind::Press, + code: KeyCode::Char('d'), + modifiers: KeyModifiers::NONE, + state: KeyEventState::NONE, + })))?); + assert_eq!(None, test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent { + kind: KeyEventKind::Press, + code: KeyCode::Char('z'), + modifiers: KeyModifiers::NONE, + state: KeyEventState::NONE, + })))?); Ok(()) } + +#[cfg(test)] #[test] fn test_dsl_context () { + use crate::dsl::Value; + + struct Test; + #[tengri_proc::expose] + impl Test { + fn some_bool (&self) -> bool { + true + } + } + assert_eq!(Test.get(&Value::Sym(":false")), Some(false)); + assert_eq!(Test.get(&Value::Sym(":true")), Some(true)); + assert_eq!(Test.get(&Value::Sym(":some-bool")), Some(true)); + assert_eq!(Test.get(&Value::Sym(":missing-bool")), None); + assert_eq!(Test.get(&Value::Num(0)), Some(false)); + assert_eq!(Test.get(&Value::Num(1)), Some(true)); +} diff --git a/tui/examples/tui.rs b/tui/examples/tui.rs index 3869a8e..0d3ef75 100644 --- a/tui/examples/tui.rs +++ b/tui/examples/tui.rs @@ -43,6 +43,9 @@ handle!(TuiIn: |self: Example, input|{ #[tengri_proc::expose] impl Example { + fn _todo_u16_stub (&self) -> u16 { todo!() } + fn _todo_bool_stub (&self) -> bool { todo!() } + fn _todo_usize_stub (&self) -> usize { todo!() } //[bool] => {} //[u16] => {} //[usize] => {}