Skip to content

Commit 957f96b

Browse files
committed
fixing failing tests and removing warnings
1 parent 6838e71 commit 957f96b

File tree

4 files changed

+144
-137
lines changed

4 files changed

+144
-137
lines changed

anathema-state-derive/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syn::spanned::Spanned as _;
2-
use syn::{DeriveInput, parse_macro_input};
2+
use syn::{parse_macro_input, DeriveInput};
33

44
static DERIVE_NAMESPACE: &str = "anathema";
55

@@ -10,20 +10,18 @@ pub fn anathema(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
1010
syn::Data::Struct(data) => structs::generate(&input, data),
1111

1212
syn::Data::Enum(data) => syn::Error::new(
13-
data.enum_token.span(), //
13+
data.enum_token.span(),
1414
"anathema's State cannot be derived on enums currently",
1515
)
1616
.to_compile_error()
1717
.into(),
1818

19-
syn::Data::Union(data) => {
20-
syn::Error::new(
21-
data.union_token.span(), //
22-
"anathema's State cannot be derived on unions currently",
23-
)
24-
.to_compile_error()
25-
.into()
26-
}
19+
syn::Data::Union(data) => syn::Error::new(
20+
data.union_token.span(),
21+
"anathema's State cannot be derived on unions currently",
22+
)
23+
.to_compile_error()
24+
.into(),
2725
}
2826
}
2927

anathema-value-resolver/src/expression.rs

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use std::collections::HashMap;
33

44
use anathema_state::{Color, Hex, PendingValue, SubTo, Subscriber, Type};
55
use anathema_store::slab::Key;
6-
use anathema_templates::Primitive;
76
use anathema_templates::expressions::{Equality, LogicalOp, Op};
7+
use anathema_templates::Primitive;
88

9-
use crate::AttributeStorage;
109
use crate::value::ValueKind;
10+
use crate::AttributeStorage;
1111

1212
macro_rules! or_null {
1313
($val:expr) => {
@@ -442,89 +442,72 @@ fn float_op(lhs: f64, rhs: f64, op: Op) -> f64 {
442442

443443
#[cfg(test)]
444444
mod test {
445-
use anathema_state::{Changes, States};
445+
use anathema_state::{drain_changes, Changes, States};
446+
use anathema_templates::expressions::{ident, index, num, strlit};
446447

447448
use crate::testing::setup;
448449

449450
#[test]
450451
fn subscribe_if_not_exist() {
451-
// // TODO: finish this test.
452-
// // The test runner should take a custom state.
453-
// // Right now it uses a Map<Box<dyn AnyState>> and that
454-
// // is just annoying
452+
// In this case the list is empty but it exists
455453

456-
// // In this case the list is empty but it exists
454+
let mut changes = Changes::empty();
455+
drain_changes(&mut changes);
456+
assert!(changes.is_empty());
457457

458-
// let mut changes = Changes::empty();
459-
// drain_changes(&mut changes);
460-
// assert!(changes.is_empty());
461-
462-
// let mut states = States::new();
463-
// setup(&mut states, Default::default(), |test| {
464-
// let expr = index(index(ident("state"), strlit("list")), num(0));
465-
466-
// let mut list = List::<u32>::empty();
467-
// test.set_state("list", list);
458+
let mut states = States::new();
459+
setup(&mut states, Default::default(), |test| {
460+
let expr = index(index(ident("state"), strlit("list")), num(0));
468461

469-
// let mut value = test.eval(&expr);
462+
let mut value = test.eval(&expr);
470463

471-
// assert_eq!(value.as_int(), None);
464+
assert_eq!(value.as_int(), None);
472465

473-
// test.with_state(|state| {
474-
// let list = state.get_mut("list").unwrap();
475-
// let mut list = list.to_mut_cast::<List<u32>>();
476-
// // list.push(1);
477-
// });
466+
test.with_state(|state| state.list.push("a"));
478467

479-
// drain_changes(&mut changes);
480-
// for (subs, change) in changes.drain() {
481-
// for sub in subs.iter() {
482-
// if sub == value.sub {
483-
// value.reload(&test.attributes);
484-
// }
485-
// }
486-
// }
468+
drain_changes(&mut changes);
469+
for (subs, _) in changes.drain() {
470+
for sub in subs.iter() {
471+
if sub == value.sub {
472+
value.reload(&test.attributes);
473+
}
474+
}
475+
}
487476

488-
// assert_eq!(value.as_int().unwrap(), 1);
489-
// });
477+
assert_eq!(value.as_str().unwrap(), "a");
478+
});
490479
}
491480

492481
#[test]
493482
fn list_preceding_value_removed() {
494-
// TODO: finish this test.
495-
// The test runner should take a custom state.
496-
// Right now it uses a Map<Box<dyn AnyState>> and that
497-
// is just annoying
498-
let changes = Changes::empty();
483+
let mut changes = Changes::empty();
499484

500485
let mut states = States::new();
501486
setup(&mut states, Default::default(), |test| {
502-
// let expr = index(index(ident("state"), strlit("list")), num(1));
487+
let expr = index(index(ident("state"), strlit("list")), num(1));
503488

504-
// let mut list = List::from_iter([0u32, 1, 2]);
505-
// test.set_state("list", list);
489+
test.with_state(|state| {
490+
state.list.push("a");
491+
state.list.push("b");
492+
state.list.push("c");
493+
});
506494

507-
// let mut value = test.eval(&expr);
495+
let mut value = test.eval(&expr);
508496

509-
// assert_eq!(value.as_int().unwrap(), 1);
497+
assert_eq!(value.as_str().unwrap(), "b");
510498

511-
// test.with_state(|state| {
512-
// // let list = state.get_mut("list".into()).unwrap();
513-
// // let mut list = list.to_mut_cast::<List<u32>>();
514-
// // list.remove(0);
515-
// // panic!()
516-
// });
499+
test.with_state(|state| state.list.remove(0));
517500

518-
// drain_changes(&mut changes);
519-
// for (subs, change) in changes.drain() {
520-
// for sub in subs.iter() {
521-
// if sub == value.sub {
522-
// value.reload(&test.attributes);
523-
// }
524-
// }
525-
// }
501+
drain_changes(&mut changes);
502+
for (subs, _) in changes.drain() {
503+
for sub in subs.iter() {
504+
if sub == value.sub {
505+
value.reload(&test.attributes);
506+
}
507+
}
508+
}
526509

527-
// assert_eq!(value.as_int().unwrap(), 2);
510+
assert_eq!(value.as_str().unwrap(), "c");
528511
});
529512
}
530513
}

anathema-value-resolver/src/testing.rs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
1-
use anathema_state::{AnyState, Map, StateId, States, Subscriber};
1+
use anathema_state::{AnyMap, AnyState, List, Map, State, StateId, States, Subscriber, Value};
22
use anathema_store::slab::Key;
33
use anathema_templates::{Expression, Globals, Variables};
44

55
use super::*;
66
use crate::context::ResolverCtx;
77
use crate::scope::Scope;
8-
use crate::value::Value;
8+
9+
#[derive(Debug)]
10+
pub(crate) struct TestState {
11+
pub(crate) string: Value<&'static str>,
12+
pub(crate) num: Value<i32>,
13+
pub(crate) num_2: Value<i32>,
14+
pub(crate) float: Value<f64>,
15+
pub(crate) list: Value<List<&'static str>>,
16+
pub(crate) map: Value<Map<i32>>,
17+
}
18+
19+
impl TestState {
20+
pub fn new() -> Self {
21+
Self {
22+
string: "".into(),
23+
num: 0.into(),
24+
num_2: 0.into(),
25+
float: 0.0.into(),
26+
list: List::empty().into(),
27+
map: Map::empty().into(),
28+
}
29+
}
30+
}
31+
32+
impl State for TestState {
33+
fn type_info(&self) -> anathema_state::Type {
34+
anathema_state::Type::Composite
35+
}
36+
37+
fn as_any_map(&self) -> Option<&dyn AnyMap> {
38+
Some(self)
39+
}
40+
}
41+
42+
impl AnyMap for TestState {
43+
fn lookup(&self, key: &str) -> Option<anathema_state::PendingValue> {
44+
match key {
45+
"list" => self.list.reference().into(),
46+
"num" => self.num.reference().into(),
47+
"num_2" => self.num_2.reference().into(),
48+
"float" => self.float.reference().into(),
49+
"string" => self.string.reference().into(),
50+
"map" => self.map.reference().into(),
51+
_ => None,
52+
}
53+
}
54+
}
955

1056
pub(crate) struct TestCase<'a, 'bp> {
1157
globals: &'static Globals,
@@ -25,8 +71,7 @@ impl<'a, 'bp> TestCase<'a, 'bp> {
2571
}
2672
}
2773

28-
pub(crate) fn eval(&self, expr: &'bp Expression) -> Value<'bp> {
29-
let root = Scope::root();
74+
pub(crate) fn eval(&self, expr: &'bp Expression) -> crate::value::Value<'bp> {
3075
let state_id = StateId::ZERO;
3176
let scope = Scope::with_component(state_id, Key::ZERO, None);
3277

@@ -38,21 +83,17 @@ impl<'a, 'bp> TestCase<'a, 'bp> {
3883
let scope = Scope::with_component(StateId::ZERO, Key::ZERO, None);
3984
self.attributes.with_mut(Key::ZERO, |attributes, storage| {
4085
let ctx = ResolverCtx::new(&self.globals, &scope, &self.states, storage);
41-
attributes.insert_with(ValueKey::Attribute(key), |index| resolve(expr, &ctx, Subscriber::ZERO));
86+
attributes.insert_with(ValueKey::Attribute(key), |_index| resolve(expr, &ctx, Subscriber::ZERO));
4287
});
4388
}
4489

45-
pub(crate) fn with_state<F>(&mut self, f: F)
90+
pub(crate) fn with_state<F, U>(&mut self, f: F) -> U
4691
where
47-
F: FnOnce(&mut Map<Box<dyn AnyState>>),
92+
F: FnOnce(&mut TestState) -> U,
4893
{
4994
let state = self.states.get_mut(StateId::ZERO).unwrap();
50-
let mut state = state.to_mut_cast::<Map<Box<dyn AnyState>>>();
51-
f(&mut *state);
52-
}
53-
54-
pub(crate) fn set_state(&mut self, key: &str, value: impl AnyState) {
55-
self.with_state(|state| state.insert(key, Box::new(value)));
95+
let mut state = state.to_mut_cast::<TestState>();
96+
f(&mut *state)
5697
}
5798
}
5899

@@ -61,7 +102,7 @@ where
61102
F: FnMut(&mut TestCase<'_, 'bp>),
62103
{
63104
// let state: Map<Box<dyn AnyState>> = Value::new(Map::empty());
64-
let state: Box<dyn AnyState> = Box::new(Map::<Box<dyn AnyState>>::empty());
105+
let state: Box<dyn AnyState> = Box::new(TestState::new());
65106
let state: anathema_state::Value<Box<dyn AnyState>> = anathema_state::Value::new(state);
66107
states.insert(state);
67108
let mut test = TestCase::new(states, globals.into());

0 commit comments

Comments
 (0)