summaryrefslogtreecommitdiffstats
path: root/vendor/elasticlunr-rs/tests/test-pipeline.rs
blob: cdc70592dd0fbee05f5c718dd458bcbb95a3568e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Input text is excerpted from public domain books on gutenberg.org or wikisource.org

use elasticlunr::*;
use std::fs::File;
use std::io::{BufRead, BufReader, Read, Write};
use std::path::Path;

#[allow(dead_code)]
fn write_output(lang: &dyn Language) {
    let code = lang.code();
    let base = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("data");

    let input = base.join(&format!("{}.in.txt", code));
    let mut input_str = String::new();
    File::open(&input)
        .unwrap()
        .read_to_string(&mut input_str)
        .unwrap();

    let output = base.join(&format!("{}.out.txt", code));
    let mut output = File::create(&output).unwrap();

    let pipeline = lang.make_pipeline();
    let tokens = pipeline.run(lang.tokenize(&input_str));

    for tok in tokens {
        writeln!(&mut output, "{}", tok).unwrap();
    }
}

fn compare_to_fixture(lang: &dyn Language) {
    let code = lang.code();
    let base = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("data");

    let input = base.join(&format!("{}.in.txt", code));
    let mut input_str = String::new();
    File::open(&input)
        .unwrap()
        .read_to_string(&mut input_str)
        .unwrap();

    let output = base.join(&format!("{}.out.txt", code));
    let mut output = BufReader::new(File::open(&output).unwrap()).lines();

    let pipeline = lang.make_pipeline();
    let tokens = pipeline.run(lang.tokenize(&input_str));

    for tok in tokens {
        assert_eq!(
            tok,
            output.next().unwrap().unwrap(),
            "Comparing pipeline tokens to fixture for {}",
            lang.name()
        );
    }
}

#[test]
fn test_languages() {
    for lang in lang::languages() {
        //write_output(lang.as_ref());
        compare_to_fixture(lang.as_ref());
    }
}