Skip to content

Commit eb95ae0

Browse files
committed
test: verify add_star sends correct param keys for each target type
Addresses review comment: add tests that capture params and assert correct key/value for each target
1 parent 32449f5 commit eb95ae0

1 file changed

Lines changed: 72 additions & 3 deletions

File tree

src/cmd/star/add.rs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,25 @@ pub fn add_with(args: &StarAddArgs, api: &dyn BacklogApi) -> Result<()> {
7373
mod tests {
7474
use super::*;
7575
use anyhow::anyhow;
76+
use std::cell::RefCell;
7677

7778
struct MockApi {
7879
ok: bool,
80+
captured_params: RefCell<Vec<(String, String)>>,
81+
}
82+
83+
impl MockApi {
84+
fn new(ok: bool) -> Self {
85+
Self {
86+
ok,
87+
captured_params: RefCell::new(Vec::new()),
88+
}
89+
}
7990
}
8091

8192
impl crate::api::BacklogApi for MockApi {
82-
fn add_star(&self, _params: &[(String, String)]) -> anyhow::Result<()> {
93+
fn add_star(&self, params: &[(String, String)]) -> anyhow::Result<()> {
94+
*self.captured_params.borrow_mut() = params.to_vec();
8395
if self.ok {
8496
Ok(())
8597
} else {
@@ -94,13 +106,13 @@ mod tests {
94106

95107
#[test]
96108
fn add_with_succeeds() {
97-
let api = MockApi { ok: true };
109+
let api = MockApi::new(true);
98110
assert!(add_with(&args_with_issue(), &api).is_ok());
99111
}
100112

101113
#[test]
102114
fn add_with_propagates_api_error() {
103-
let api = MockApi { ok: false };
115+
let api = MockApi::new(false);
104116
let err = add_with(&args_with_issue(), &api).unwrap_err();
105117
assert!(err.to_string().contains("api error"));
106118
}
@@ -123,4 +135,61 @@ mod tests {
123135
assert!(StarAddArgs::try_new(None, None, None, Some(1), None).is_ok());
124136
assert!(StarAddArgs::try_new(None, None, None, None, Some(1)).is_ok());
125137
}
138+
139+
#[test]
140+
fn add_with_sends_issue_id_param() {
141+
let api = MockApi::new(true);
142+
let args = StarAddArgs::try_new(Some(42), None, None, None, None).unwrap();
143+
add_with(&args, &api).unwrap();
144+
let params = api.captured_params.borrow();
145+
assert_eq!(
146+
params.as_slice(),
147+
[("issueId".to_string(), "42".to_string())]
148+
);
149+
}
150+
151+
#[test]
152+
fn add_with_sends_comment_id_param() {
153+
let api = MockApi::new(true);
154+
let args = StarAddArgs::try_new(None, Some(10), None, None, None).unwrap();
155+
add_with(&args, &api).unwrap();
156+
let params = api.captured_params.borrow();
157+
assert_eq!(
158+
params.as_slice(),
159+
[("commentId".to_string(), "10".to_string())]
160+
);
161+
}
162+
163+
#[test]
164+
fn add_with_sends_wiki_id_param() {
165+
let api = MockApi::new(true);
166+
let args = StarAddArgs::try_new(None, None, Some(5), None, None).unwrap();
167+
add_with(&args, &api).unwrap();
168+
let params = api.captured_params.borrow();
169+
assert_eq!(params.as_slice(), [("wikiId".to_string(), "5".to_string())]);
170+
}
171+
172+
#[test]
173+
fn add_with_sends_pull_request_id_param() {
174+
let api = MockApi::new(true);
175+
let args = StarAddArgs::try_new(None, None, None, Some(7), None).unwrap();
176+
add_with(&args, &api).unwrap();
177+
let params = api.captured_params.borrow();
178+
assert_eq!(
179+
params.as_slice(),
180+
[("pullRequestId".to_string(), "7".to_string())]
181+
);
182+
}
183+
184+
#[test]
185+
fn add_with_sends_pull_request_comment_id_param() {
186+
let api = MockApi::new(true);
187+
let args = StarAddArgs::try_new(None, None, None, None, Some(3)).unwrap();
188+
add_with(&args, &api).unwrap();
189+
let params = api.captured_params.borrow();
190+
assert_eq!(
191+
params.as_slice(),
192+
[("pullRequestCommentId".to_string(), "3".to_string())]
193+
);
194+
}
126195
}

0 commit comments

Comments
 (0)