-
Notifications
You must be signed in to change notification settings - Fork 15
test: Bookmarked: add tests to Bookmarked functions #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JGBSouza
wants to merge
1
commit into
kworkflow:unstable
Choose a base branch
from
JGBSouza:test/bookmarked_patches
base: unstable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,109 @@ impl BookmarkedPatchsets { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use serde_json; | ||
|
|
||
| fn make_patch(title: &str) -> Patch { | ||
| let json = format!( | ||
| r#"{{ | ||
| "title": "{title}", | ||
| "author": {{ "name": "name", "email": "teste@teste.com" }}, | ||
| "link": {{ "@href": "http://lore.kernel.org/some-list/1234-1-teste@teste.com" }}, | ||
| "thr:in-reply-to": null, | ||
| "updated": "2024-07-06T19:15:48Z" | ||
| }}"# | ||
| ); | ||
| serde_json::from_str::<Patch>(&json).unwrap() | ||
| } | ||
|
|
||
| fn mock_bookmarked_patchsets() -> BookmarkedPatchsets { | ||
| BookmarkedPatchsets { | ||
| bookmarked_patchsets: vec![ | ||
| make_patch("Patch 1"), | ||
| make_patch("Patch 2"), | ||
| make_patch("Patch 3"), | ||
| ], | ||
| patchset_index: 0, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_select_below_patchset_increments_index() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| let patchset_index_before = s.patchset_index; | ||
| s.select_below_patchset(); | ||
| assert_eq!(s.patchset_index, patchset_index_before + 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_select_below_patchset_stops_at_end() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| s.patchset_index = s.bookmarked_patchsets.len() - 1; | ||
| s.select_below_patchset(); | ||
| assert_eq!(s.patchset_index, s.bookmarked_patchsets.len() - 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_select_above_patchset_decrements_index() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| s.patchset_index = 2; | ||
| s.select_above_patchset(); | ||
| assert_eq!(s.patchset_index, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_select_above_patchset_stops_at_start() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| s.patchset_index = 0; | ||
| s.select_above_patchset(); | ||
| assert_eq!(s.patchset_index, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_get_selected_patchset_returns_correct_patch() { | ||
| let s = mock_bookmarked_patchsets(); | ||
| let patch = s.get_selected_patchset(); | ||
| assert_eq!(patch.title(), "Patch 1"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bookmark_selected_patch_adds_new_patch() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| let new_patch = make_patch("New Patch"); | ||
| s.bookmark_selected_patch(&new_patch); | ||
| assert!(s.bookmarked_patchsets.contains(&new_patch)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_bookmark_selected_patch_does_not_duplicate() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| let existing_patch = make_patch("Patch 1"); | ||
| s.bookmark_selected_patch(&existing_patch); | ||
| let count = s | ||
| .bookmarked_patchsets | ||
| .iter() | ||
| .filter(|p| **p == existing_patch) | ||
| .count(); | ||
| assert_eq!(count, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_unbookmark_selected_patch_removes_patch() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| let to_remove = s.bookmarked_patchsets[1].clone(); | ||
| s.unbookmark_selected_patch(&to_remove); | ||
| assert!(!s.bookmarked_patchsets.contains(&to_remove)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_unbookmark_selected_patch_ignores_missing_patch() { | ||
| let mut s = mock_bookmarked_patchsets(); | ||
| let missing = make_patch("Inexistent"); | ||
| s.unbookmark_selected_patch(&missing); | ||
| assert_eq!(s.bookmarked_patchsets.len(), 3); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, you should be asserting the len before the unbookmark_selected_patch operation so the test behavior is explicit |
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same thing here. We shouldn't need to see mock_bookmarked_patchsets implementation to understand what's the initial patchset_index. You can assert or set the patchset_index