Tests for the new highlight algorithm
@@ -593,6 +593,35 @@ mod tests {
}
+ #[test]
+ fn calculate_matches() {
+ let mut matches = Vec::new();
+ matches.push(Highlight { attribute: 0, char_index: 0, char_length: 3});
+ matches.push(Highlight { attribute: 0, char_index: 0, char_length: 2});
+
+ let mut attributes_to_retrieve: HashSet<String> = HashSet::new();
+ attributes_to_retrieve.insert("title".to_string());
+
+ let schema = Schema::with_primary_key("title");
+
+ let matches_result = super::calculate_matches(matches, Some(attributes_to_retrieve), &schema);
+
+ let mut matches_result_expected: HashMap<String, Vec<MatchPosition>> = HashMap::new();
+
+ let mut positions = Vec::new();
+ positions.push(MatchPosition {
+ start: 0,
+ length: 2,
+ });
+ positions.push(MatchPosition {
+ start: 0,
+ length: 3,
+ });
+ matches_result_expected.insert("title".to_string(), positions);
+
+ assert_eq!(matches_result, matches_result_expected);
+ }
+
#[test]
fn calculate_highlights() {
let data = r#"{
@@ -631,4 +660,38 @@ mod tests {
assert_eq!(result, result_expected);
}
+
+ #[test]
+ fn highlight_longest_match() {
+ let data = r#"{
+ "title": "Ice"
+ }"#;
+
+ let document: IndexMap<String, Value> = serde_json::from_str(data).unwrap();
+ let mut attributes_to_highlight = HashSet::new();
+ attributes_to_highlight.insert("title".to_string());
+
+ let mut matches = HashMap::new();
+
+ let mut m = Vec::new();
+ m.push(MatchPosition {
+ start: 0,
+ length: 2,
+ });
+ m.push(MatchPosition {
+ start: 0,
+ length: 3,
+ });
+ matches.insert("title".to_string(), m);
+
+ let result = super::calculate_highlights(&document, &matches, &attributes_to_highlight);
+
+ let mut result_expected = IndexMap::new();
+ result_expected.insert(
+ "title".to_string(),
+ Value::String("<em>Ice</em>".to_string()),
+ );
+
+ assert_eq!(result, result_expected);
+ }
}
