fix handling of typing cards

This commit is contained in:
Damien Elmes 2019-12-25 13:01:19 +10:00
parent f4c5aac163
commit 94485f36ae
2 changed files with 18 additions and 5 deletions

View File

@ -177,7 +177,12 @@ fn template_is_empty<'a>(nonempty_fields: &HashSet<&str>, nodes: &[ParsedNode<'a
match node {
// ignore normal text
Text(_) => (),
Replacement { key, .. } => {
Replacement { key, filters } => {
// Anki doesn't consider a type: reference as a required field
if filters.contains(&"type") {
continue;
}
if nonempty_fields.contains(*key) {
// a single replacement is enough
return false;
@ -307,7 +312,7 @@ mod test {
let mut tmpl = PT::from_text("{{2}}{{1}}").unwrap();
assert_eq!(tmpl.renders_with_fields(&fields), true);
tmpl = PT::from_text("{{2}}{{type:cloze:1}}").unwrap();
assert_eq!(tmpl.renders_with_fields(&fields), true);
assert_eq!(tmpl.renders_with_fields(&fields), false);
tmpl = PT::from_text("{{2}}{{4}}").unwrap();
assert_eq!(tmpl.renders_with_fields(&fields), false);
tmpl = PT::from_text("{{#3}}{{^2}}{{1}}{{/2}}{{/3}}").unwrap();
@ -346,8 +351,10 @@ mod test {
FieldRequirements::All(HashSet::from_iter(vec![0, 1].into_iter()))
);
// fixme: handling of type in answer card reqs doesn't match desktop,
// which only requires first field
//
tmpl = PT::from_text("{{a}}{{type:b}}").unwrap();
assert_eq!(
tmpl.requirements(&field_map),
FieldRequirements::Any(HashSet::from_iter(vec![0].into_iter()))
);
}
}

View File

@ -371,6 +371,12 @@ def test_req():
mm.save(opt, templates=True)
assert opt['req'][1] == [1, 'none', []]
opt = mm.byName("Basic (type in the answer)")
reqSize(opt)
r = opt['req'][0]
assert r[1] in ("any", "all")
assert r[2] == [0]
# def test_updatereqs_performance():
# import time
# d = getEmptyCol()