1
use super::{load, lookups};
2
use crate::lookup_log::{Lookup, LookupKind};
3

            
4
const ORG: &str = "* Title\nprose (get-account \"Prose\")\n\
5
#+begin_src lisp :tangle \"x.nms\"\n(string= name \"Suica\")(get-account \"Metro\")\n#+end_src\n\
6
#+begin_src sh\necho \"Shell\"\n#+end_src\n";
7

            
8
2
fn keys(found: &[Lookup]) -> Vec<(LookupKind, &str)> {
9
4
    found.iter().map(|l| (l.kind, l.key.as_str())).collect()
10
2
}
11

            
12
#[test]
13
1
fn only_the_lisp_blocks_of_an_org_file_are_code() -> Result<(), String> {
14
1
    let code = super::lisp_blocks(ORG);
15

            
16
1
    assert_eq!(keys(&lookups(&code)?), [(LookupKind::Account, "Metro")]);
17
1
    Ok(())
18
1
}
19

            
20
#[test]
21
1
fn lookup_keys_are_found_in_nested_forms_and_logical_rows() -> Result<(), String> {
22
1
    let found = lookups(
23
1
        "(defun f () (let ((a (get-commodity \"usd\"))) (g `(:from \"A\" :to-commodity \"EUR\" :note \"N\"))))",
24
1
    )?;
25

            
26
1
    let mut found = keys(&found);
27
4
    found.sort_by_key(|(_, key)| *key);
28
1
    assert_eq!(
29
1
        found,
30
1
        [
31
1
            (LookupKind::Account, "A"),
32
1
            (LookupKind::Commodity, "EUR"),
33
1
            (LookupKind::Commodity, "usd")
34
1
        ]
35
    );
36
1
    Ok(())
37
1
}
38

            
39
#[test]
40
1
fn other_string_literals_are_not_lookups() -> Result<(), String> {
41
1
    assert_eq!(
42
1
        lookups("(string= x \"Suica\")(get-account y)(list \"Cash\")")?,
43
1
        []
44
    );
45
1
    Ok(())
46
1
}
47

            
48
#[test]
49
1
fn a_source_that_does_not_parse_is_reported() {
50
1
    let err = lookups("(get-account").unwrap_err();
51
1
    assert!(err.starts_with("does not parse"), "{err}");
52
1
}
53

            
54
#[test]
55
1
fn a_directory_loads_the_declared_extensions_in_order() -> anyhow::Result<()> {
56
1
    let dir = tempfile::tempdir()?;
57
1
    std::fs::write(dir.path().join("b.org"), ORG)?;
58
1
    std::fs::write(dir.path().join("b.nms"), "(get-account \"Mirror\")")?;
59
1
    std::fs::write(dir.path().join("a.nms"), "(get-account \"A\")")?;
60
1
    std::fs::write(dir.path().join("notes.txt"), "(")?;
61
1
    std::fs::write(dir.path().join("broken.lisp"), "(")?;
62

            
63
1
    let (sources, findings) = load(dir.path());
64

            
65
1
    let names: Vec<_> = sources
66
1
        .iter()
67
2
        .filter_map(|s| s.name.rsplit('/').next())
68
        .collect();
69
1
    assert_eq!(names, ["a.nms", "b.org"]);
70
1
    assert_eq!(findings.len(), 1);
71
1
    assert!(findings[0].subject.to_string().ends_with("broken.lisp"));
72
1
    Ok(())
73
1
}
74

            
75
#[test]
76
1
fn a_missing_directory_is_reported() {
77
1
    let (sources, findings) = load(std::path::Path::new("/nonexistent/sources"));
78
1
    assert!(sources.is_empty());
79
1
    assert_eq!(findings.len(), 1);
80
1
}