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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*use std::collections::HashMap;
use mockall::mock;
use crate::model::bookmark_list::BookmarkList;
#[derive(Debug, Clone)]
pub struct BookmarkResolver {
map: HashMap<String, String>,
}
mock! {
#[derive(Debug)]
pub BookmarkResolver {
pub fn new() -> Self;
pub fn update(&mut self, bookmark_list: &BookmarkList);
pub fn is_bookmark(&self, name: &String) -> bool;
pub fn resolve(&self, name: &String) -> Option<String>;
}
impl Clone for BookmarkResolver {
fn clone(&self) -> Self;
}
}
impl BookmarkResolver {
pub fn new() -> BookmarkResolver {
let map = HashMap::new();
BookmarkResolver { map }
}
}
impl Default for BookmarkResolver {
fn default() -> Self {
Self::new()
}
}
impl BookmarkResolver {
pub fn update(&mut self, bookmark_list: &BookmarkList) {
self.map = bookmark_list.bookmarks().clone();
}
pub fn is_bookmark(&self, name: &String) -> bool {
self.map.contains_key(name)
}
pub fn resolve(&self, name: &String) -> Option<String> {
self.map.get(name).cloned()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::bookmark_list::BookmarkList;
fn create_bookmark_list(bookmarks: HashMap<String, String>) -> BookmarkList {
let json = serde_json::json!({
"bookmarks": bookmarks
});
serde_json::from_value(json).unwrap()
}
#[test]
fn test_new() {
let resolver = BookmarkResolver::new();
assert!(!resolver.is_bookmark(&"test".to_string()));
assert!(resolver.resolve(&"test".to_string()).is_none());
}
#[test]
fn test_update_and_resolve() {
let mut resolver = BookmarkResolver::new();
let mut bookmarks = HashMap::new();
bookmarks.insert("google".to_string(), "https://google.com".to_string());
bookmarks.insert("local".to_string(), "http://localhost:8080".to_string());
let list = create_bookmark_list(bookmarks);
resolver.update(&list);
assert_eq!(resolver.resolve(&"google".to_string()), Some("https://google.com".to_string()));
assert_eq!(resolver.resolve(&"local".to_string()), Some("http://localhost:8080".to_string()));
}
#[test]
fn test_is_bookmark() {
let mut resolver = BookmarkResolver::new();
let mut bookmarks = HashMap::new();
bookmarks.insert("exists".to_string(), "target".to_string());
let list = create_bookmark_list(bookmarks);
resolver.update(&list);
assert!(resolver.is_bookmark(&"exists".to_string()));
assert!(!resolver.is_bookmark(&"missing".to_string()));
}
#[test]
fn test_resolve_non_existent() {
let mut resolver = BookmarkResolver::new();
let mut bookmarks = HashMap::new();
bookmarks.insert("exists".to_string(), "target".to_string());
let list = create_bookmark_list(bookmarks);
resolver.update(&list);
assert!(resolver.resolve(&"missing".to_string()).is_none());
}
}*/