Skip to content

Commit 3846fb0

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

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

src/app/screens/bookmarked.rs

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

0 commit comments

Comments
 (0)