]> git.proxmox.com Git - rustc.git/blob - vendor/elasticlunr-rs/tests/test-compare.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / elasticlunr-rs / tests / test-compare.rs
1 #[macro_use]
2 extern crate serde_json;
3 extern crate elasticlunr;
4
5 use elasticlunr::*;
6 use std::fs::File;
7 use std::path::Path;
8
9 const DOCS: &'static [[&'static str; 2]] = &[
10 [
11 "Chapter 1",
12 "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
13 ],
14 [
15 "Chapter 2",
16 "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad",
17 ],
18 [
19 "Chapter 3",
20 "minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex",
21 ],
22 [
23 "Chapter 4",
24 "ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate",
25 ],
26 [
27 "Chapter 5",
28 "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat",
29 ],
30 ["Chapter 6", "Spatiëring shouldn’t cause a panic."],
31 ];
32
33 fn create_index() -> serde_json::Value {
34 let mut index = Index::new(&["title", "body"]);
35
36 let mut i = 1;
37 for doc in DOCS.iter() {
38 index.add_doc(&format!("{}", i), doc);
39 i += 1;
40 }
41 json!(index)
42 }
43
44 #[cfg(feature = "ja")]
45 const DOCS_JA: &'static [[&'static str; 2]] = &[
46 [
47 "第1章",
48 "吾輩は猫である。名前はまだ無い。",
49 ],
50 [
51 "第2章",
52 "どこで生れたかとんと見当がつかぬ。何でも薄暗いじめじめした所でニャーニャー泣いていた事だけは記憶している。",
53 ],
54 [
55 "第3章",
56 "吾輩はここで始めて人間というものを見た。しかもあとで聞くとそれは書生という人間中で一番獰悪な種族であったそうだ。この書生というのは時々我々を捕えて煮て食うという話である。しかしその当時は何という考もなかったから別段恐しいとも思わなかった。ただ彼の掌に載せられてスーと持ち上げられた時何だかフワフワした感じがあったばかりである。掌の上で少し落ちついて書生の顔を見たのがいわゆる人間というものの見始であろう。この時妙なものだと思った感じが今でも残っている。",
57 ],
58 [
59 "第4章",
60 "第一毛をもって装飾されべきはずの顔がつるつるしてまるで薬缶だ。その後猫にもだいぶ逢ったがこんな片輪には一度も出会わした事がない。のみならず顔の真中があまりに突起している。",
61 ],
62 ];
63
64 #[cfg(feature = "ja")]
65 fn create_index_ja() -> serde_json::Value {
66 let mut index = Index::with_language(Language::Japanese, &["title", "body"]);
67
68 let mut i = 1;
69 for doc in DOCS_JA.iter() {
70 index.add_doc(&format!("{}", i), doc);
71 i += 1;
72 }
73 json!(index)
74 }
75
76 const GENERATE_FIXTURE: bool = false;
77
78 fn get_fixture() -> serde_json::Value {
79 if GENERATE_FIXTURE {
80 let src = create_index();
81
82 let dest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/searchindex_fixture.json");
83 let dest = File::create(&dest).unwrap();
84 serde_json::to_writer_pretty(dest, &src).unwrap();
85
86 src
87 } else {
88 let json = include_str!("searchindex_fixture.json");
89 serde_json::from_str(json).expect("Unable to deserialize the fixture")
90 }
91 }
92
93 #[cfg(feature = "ja")]
94 const GENERATE_FIXTURE_JA: bool = false;
95
96 #[cfg(feature = "ja")]
97 fn get_fixture_ja() -> serde_json::Value {
98 if GENERATE_FIXTURE_JA {
99 let src = create_index_ja();
100
101 let dest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/searchindex_fixture_ja.json");
102 let dest = File::create(&dest).unwrap();
103 serde_json::to_writer_pretty(dest, &src).unwrap();
104
105 src
106 } else {
107 let json = include_str!("searchindex_fixture_ja.json");
108 serde_json::from_str(json).expect("Unable to deserialize the fixture of Japanese")
109 }
110 }
111
112 #[test]
113 fn search_index_hasnt_changed_accidentally() {
114 let new_index = create_index();
115 let fixture_index = get_fixture();
116
117 if new_index != fixture_index {
118 panic!("The search index has changed from the fixture");
119 }
120 }
121
122 #[cfg(feature = "ja")]
123 #[test]
124 fn ja_search_index_hasnt_changed_accidentally() {
125 let new_index = create_index_ja();
126 let fixture_index = get_fixture_ja();
127
128 if new_index != fixture_index {
129 panic!("The search index has changed from the fixture of Japanese");
130 }
131 }