collect tests
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-09 23:00:36 +03:00
parent 4a385b40ff
commit cb8fd26922
8 changed files with 234 additions and 257 deletions

View file

@ -143,22 +143,6 @@ mod dsl; pub use self::dsl::*;
Ok(()) 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> { //#[cfg(test)] #[test] fn test_examples () -> Result<(), ParseError> {
//// Let's pretend to render some view. //// Let's pretend to render some view.
//let source = include_str!("../../tek/src/view_arranger.edn"); //let source = include_str!("../../tek/src/view_arranger.edn");

View file

@ -64,7 +64,6 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
#[cfg(test)] mod test { #[cfg(test)] mod test {
use crate::*; use crate::*;
use proptest::{prelude::*, option::of}; use proptest::{prelude::*, option::of};
use proptest::option::of;
proptest! { proptest! {
#[test] fn proptest_direction ( #[test] fn proptest_direction (

View file

@ -1,7 +1,11 @@
use crate::*; use crate::*;
pub use Direction::*; pub use Direction::*;
/// A split or layer. /// A split or layer.
pub struct Bsp<X, Y>(Direction, X, Y); pub struct Bsp<A, B>(
pub(crate) Direction,
pub(crate) A,
pub(crate) B,
);
impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> { impl<E: Output, A: Content<E>, B: Content<E>> Content<E> for Bsp<A, B> {
fn layout (&self, outer: E::Area) -> E::Area { fn layout (&self, outer: E::Area) -> E::Area {
let [_, _, c] = self.areas(outer); let [_, _, c] = self.areas(outer);

View file

@ -21,14 +21,7 @@ mod proc_view;
mod proc_expose; mod proc_expose;
mod proc_command; mod proc_command;
#[proc_macro_attribute] #[cfg(test)] use syn::parse_quote as pq;
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),
))
}
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn expose (meta: TokenStream, item: TokenStream) -> TokenStream { 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: ToTokens> (t: T) -> TokenStream { pub(crate) fn write <T: ToTokens> (t: T) -> TokenStream {
let mut out = TokenStream2::new(); let mut out = TokenStream2::new();
t.to_tokens(&mut out); t.to_tokens(&mut out);
@ -67,3 +69,175 @@ pub(crate) fn write_quote_to (out: &mut TokenStream2, quote: TokenStream2) {
out.append(token); 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<SomeOutput> + use<'_> {
//"view-1"
//}
//}
//};
//let written = quote! { #parsed };
//assert_eq!(format!("{written}"), format!("{}", quote! {
//impl SomeView {
//fn view_1 (&self) -> impl Content<SomeOutput> + use<'_> {
//"view-1"
//}
//}
///// Generated by [tengri_proc].
//impl ::tengri::output::Content<SomeOutput> for SomeView {
//fn content (&self) -> impl Render<SomeOutput> {
//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<RenderBox<'a, SomeOutput>> {
//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<bool> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
////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<str>)] {
//":arcstr1" => "foo".into(),
//}
//#[tengri::expose(Option<Arc<str>>)] {
//":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<Arc<str>> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<Arc<str>> {
////Some(match dsl {
////::tengri::Value::Sym(":arcstr1") => "foo".into(),
////_ => return None
////})
////}
////}
////impl ::tengri::Context<Option<Arc<str>>> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<Option<Arc<str>>> {
////Some(match dsl {
////::tengri::Value::Sym(":optarcstr1") => Some("bar".into()),
////::tengri::Value::Sym(":optarcstr2") => Some("baz".into()),
////_ => return None
////})
////}
////}
////impl ::tengri::Context<bool> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
////Some(match dsl {
////::tengri::Value::Sym(":true") => true,
////::tengri::Value::Sym(":false") => false,
////::tengri::Value::Sym(":bool1") => true || false,
////_ => return None
////})
////}
////}
////impl ::tengri::Context<u16> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<u16> {
////Some(match dsl {
////::tengri::Value::Num(n) => *n as u16,
////::tengri::Value::Sym(":u161") => 0 + 1,
////_ => return None
////})
////}
////}
////impl ::tengri::Context<usize> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<usize> {
////Some(match dsl {
////::tengri::Value::Num(n) => *n as usize,
////::tengri::Value::Sym(":usize1") => 1 + 2,
////_ => return None
////})
////}
////}
////})
////)
//}

View file

@ -189,109 +189,3 @@ impl ToTokens for ExposeType {
self.0.to_tokens(out) 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<bool> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
////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<str>)] {
//":arcstr1" => "foo".into(),
//}
//#[tengri::expose(Option<Arc<str>>)] {
//":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<Arc<str>> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<Arc<str>> {
////Some(match dsl {
////::tengri::Value::Sym(":arcstr1") => "foo".into(),
////_ => return None
////})
////}
////}
////impl ::tengri::Context<Option<Arc<str>>> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<Option<Arc<str>>> {
////Some(match dsl {
////::tengri::Value::Sym(":optarcstr1") => Some("bar".into()),
////::tengri::Value::Sym(":optarcstr2") => Some("baz".into()),
////_ => return None
////})
////}
////}
////impl ::tengri::Context<bool> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<bool> {
////Some(match dsl {
////::tengri::Value::Sym(":true") => true,
////::tengri::Value::Sym(":false") => false,
////::tengri::Value::Sym(":bool1") => true || false,
////_ => return None
////})
////}
////}
////impl ::tengri::Context<u16> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<u16> {
////Some(match dsl {
////::tengri::Value::Num(n) => *n as u16,
////::tengri::Value::Sym(":u161") => 0 + 1,
////_ => return None
////})
////}
////}
////impl ::tengri::Context<usize> for Something {
////fn get (&self, dsl: &::tengri::Value) -> Option<usize> {
////Some(match dsl {
////::tengri::Value::Num(n) => *n as usize,
////::tengri::Value::Sym(":usize1") => 1 + 2,
////_ => return None
////})
////}
////}
////})
////)
}

View file

@ -5,7 +5,7 @@ pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct ViewMeta { pub(crate) struct ViewMeta {
output: Ident, pub(crate) output: Ident,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -114,123 +114,3 @@ impl ToTokens for ViewArm {
out.append(Punct::new(',', Alone)); 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<SomeOutput> + use<'_> {
//"view-1"
//}
//}
//};
//let written = quote! { #parsed };
//assert_eq!(format!("{written}"), format!("{}", quote! {
//impl SomeView {
//fn view_1 (&self) -> impl Content<SomeOutput> + use<'_> {
//"view-1"
//}
//}
///// Generated by [tengri_proc].
//impl ::tengri::output::Content<SomeOutput> for SomeView {
//fn content (&self) -> impl Render<SomeOutput> {
//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<RenderBox<'a, SomeOutput>> {
//match value {
//::tengri::dsl::Value::Sym(":view-1") => self.view_1().boxed(),
//_ => panic!("expected Sym(content), got: {value:?}")
//}
//}
//}
//}));
}

View file

@ -24,6 +24,9 @@
fn do_thing (state: &mut Test) -> Perhaps<Self> { fn do_thing (state: &mut Test) -> Perhaps<Self> {
Ok(None) Ok(None)
} }
fn do_thing_arg (state: &mut Test, arg: usize) -> Perhaps<Self> {
Ok(None)
}
fn do_sub (state: &mut Test, command: TestSubcommand) -> Perhaps<Self> { fn do_sub (state: &mut Test, command: TestSubcommand) -> Perhaps<Self> {
Ok(command.execute(state)?.map(|command|Self::DoSub { command })) Ok(command.execute(state)?.map(|command|Self::DoSub { command }))
} }
@ -32,11 +35,17 @@
fn do_other_thing (state: &mut Test) -> Perhaps<Self> { fn do_other_thing (state: &mut Test) -> Perhaps<Self> {
Ok(None) Ok(None)
} }
fn do_other_thing_arg (state: &mut Test, arg: usize) -> Perhaps<Self> {
Ok(None)
}
} }
let mut test = Test { let mut test = Test {
keys: InputMap::new( keys: InputMap::new("
"(@a do-thing) (@b do-sub do-other-thing)".into() (@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 { assert_eq!(Some(true), test.handle(&TuiIn(Default::default(), Event::Key(KeyEvent {
kind: KeyEventKind::Press, kind: KeyEventKind::Press,
@ -50,11 +59,41 @@
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
state: KeyEventState::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, kind: KeyEventKind::Press,
code: KeyCode::Char('c'), code: KeyCode::Char('c'),
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
state: KeyEventState::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(()) 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));
}

View file

@ -43,6 +43,9 @@ handle!(TuiIn: |self: Example, input|{
#[tengri_proc::expose] #[tengri_proc::expose]
impl Example { impl Example {
fn _todo_u16_stub (&self) -> u16 { todo!() }
fn _todo_bool_stub (&self) -> bool { todo!() }
fn _todo_usize_stub (&self) -> usize { todo!() }
//[bool] => {} //[bool] => {}
//[u16] => {} //[u16] => {}
//[usize] => {} //[usize] => {}