Skip to content

Commit acd6f8e

Browse files
committed
test: add tests to Bookmarked functions
This commit add tests to the Bookmarked functions. Signed-off-by: JGBSouza <joaosouzaaa12@usp.br>
1 parent 9f77c75 commit acd6f8e

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

src/app/screens/bookmarked.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,109 @@ impl BookmarkedPatchsets {
3939
}
4040
}
4141
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
use serde_json;
47+
48+
fn make_patch(title: &str) -> Patch {
49+
let json = format!(
50+
r#"{{
51+
"title": "{title}",
52+
"author": {{ "name": "name", "email": "teste@teste.com" }},
53+
"link": {{ "@href": "http://lore.kernel.org/some-list/1234-1-teste@teste.com" }},
54+
"thr:in-reply-to": null,
55+
"updated": "2024-07-06T19:15:48Z"
56+
}}"#
57+
);
58+
serde_json::from_str::<Patch>(&json).unwrap()
59+
}
60+
61+
fn mock_bookmarked_patchsets() -> BookmarkedPatchsets {
62+
BookmarkedPatchsets {
63+
bookmarked_patchsets: vec![
64+
make_patch("Patch 1"),
65+
make_patch("Patch 2"),
66+
make_patch("Patch 3"),
67+
],
68+
patchset_index: 0,
69+
}
70+
}
71+
72+
#[test]
73+
fn test_select_below_patchset_increments_index() {
74+
let mut s = mock_bookmarked_patchsets();
75+
let patchset_index_before = s.patchset_index;
76+
s.select_below_patchset();
77+
assert_eq!(s.patchset_index, patchset_index_before + 1);
78+
}
79+
80+
#[test]
81+
fn test_select_below_patchset_stops_at_end() {
82+
let mut s = mock_bookmarked_patchsets();
83+
s.patchset_index = s.bookmarked_patchsets.len() - 1;
84+
s.select_below_patchset();
85+
assert_eq!(s.patchset_index, s.bookmarked_patchsets.len() - 1);
86+
}
87+
88+
#[test]
89+
fn test_select_above_patchset_decrements_index() {
90+
let mut s = mock_bookmarked_patchsets();
91+
s.patchset_index = 2;
92+
s.select_above_patchset();
93+
assert_eq!(s.patchset_index, 1);
94+
}
95+
96+
#[test]
97+
fn test_select_above_patchset_stops_at_start() {
98+
let mut s = mock_bookmarked_patchsets();
99+
s.patchset_index = 0;
100+
s.select_above_patchset();
101+
assert_eq!(s.patchset_index, 0);
102+
}
103+
104+
#[test]
105+
fn test_get_selected_patchset_returns_correct_patch() {
106+
let s = mock_bookmarked_patchsets();
107+
let patch = s.get_selected_patchset();
108+
assert_eq!(patch.title(), "Patch 1");
109+
}
110+
111+
#[test]
112+
fn test_bookmark_selected_patch_adds_new_patch() {
113+
let mut s = mock_bookmarked_patchsets();
114+
let new_patch = make_patch("New Patch");
115+
s.bookmark_selected_patch(&new_patch);
116+
assert!(s.bookmarked_patchsets.contains(&new_patch));
117+
}
118+
119+
#[test]
120+
fn test_bookmark_selected_patch_does_not_duplicate() {
121+
let mut s = mock_bookmarked_patchsets();
122+
let existing_patch = make_patch("Patch 1");
123+
s.bookmark_selected_patch(&existing_patch);
124+
let count = s
125+
.bookmarked_patchsets
126+
.iter()
127+
.filter(|p| **p == existing_patch)
128+
.count();
129+
assert_eq!(count, 1);
130+
}
131+
132+
#[test]
133+
fn test_unbookmark_selected_patch_removes_patch() {
134+
let mut s = mock_bookmarked_patchsets();
135+
let to_remove = s.bookmarked_patchsets[1].clone();
136+
s.unbookmark_selected_patch(&to_remove);
137+
assert!(!s.bookmarked_patchsets.contains(&to_remove));
138+
}
139+
140+
#[test]
141+
fn test_unbookmark_selected_patch_ignores_missing_patch() {
142+
let mut s = mock_bookmarked_patchsets();
143+
let missing = make_patch("Inexistent");
144+
s.unbookmark_selected_patch(&missing);
145+
assert_eq!(s.bookmarked_patchsets.len(), 3);
146+
}
147+
}

0 commit comments

Comments
 (0)