Skip to content

Commit

Permalink
Test LabelOperation::as_log_message()
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Oct 19, 2023
1 parent 6e1daf7 commit 3c15a29
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ pub(crate) struct UpdateLabel {
new_name: Option<LabelName>,
#[serde(serialize_with = "serialize_option_color")]
color: Option<Color>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
}

Expand Down
116 changes: 114 additions & 2 deletions src/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ impl<'a> fmt::Display for LabelOperationMessage<'a> {
if !std::mem::replace(&mut first, false) {
write!(f, ", ")?;
}
write!(f, "color: {:?}", color2rgbhex(c))?;
write!(f, "new color: {:?}", color2rgbhex(c))?;
}
if let Some(d) = description.as_ref() {
if !std::mem::replace(&mut first, false) {
write!(f, ", ")?;
}
write!(f, "description: {d:?}")?;
write!(f, "new description: {d:?}")?;
}
write!(f, ")")?;
}
Expand Down Expand Up @@ -1429,4 +1429,116 @@ mod tests {
assert!(res.is_empty(), "{res:?}");
}
}

#[test]
fn test_log_message_create() {
let op = LabelOperation::Create(Label {
name: "foo".parse().unwrap(),
color: "red".parse().unwrap(),
description: Some(String::from("Foo all the bars")),
});
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Creating label "foo" in octocat/repo (color: "ff0000", description: "Foo all the bars")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would create label "foo" in octocat/repo (color: "ff0000", description: "Foo all the bars")"#
);
}

#[test]
fn test_log_message_create_none_description() {
let op = LabelOperation::Create(Label {
name: "foo".parse().unwrap(),
color: "red".parse().unwrap(),
description: None,
});
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Creating label "foo" in octocat/repo (color: "ff0000", description: "")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would create label "foo" in octocat/repo (color: "ff0000", description: "")"#
);
}

#[test]
fn test_log_message_update_name() {
let op = LabelOperation::Update {
name: "foo".parse().unwrap(),
new_name: Some("bar".parse().unwrap()),
color: None,
description: None,
};
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Updating label "foo" in octocat/repo (new name: "bar")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would update label "foo" in octocat/repo (new name: "bar")"#
);
}

#[test]
fn test_log_message_update_color() {
let op = LabelOperation::Update {
name: "foo".parse().unwrap(),
new_name: None,
color: Some("blue".parse().unwrap()),
description: None,
};
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Updating label "foo" in octocat/repo (new color: "0000ff")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would update label "foo" in octocat/repo (new color: "0000ff")"#
);
}

#[test]
fn test_log_message_update_description() {
let op = LabelOperation::Update {
name: "foo".parse().unwrap(),
new_name: None,
color: None,
description: Some(String::from("What is a foo without its bar?")),
};
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Updating label "foo" in octocat/repo (new description: "What is a foo without its bar?")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would update label "foo" in octocat/repo (new description: "What is a foo without its bar?")"#
);
}

#[test]
fn test_log_message_update_all() {
let op = LabelOperation::Update {
name: "foo".parse().unwrap(),
new_name: Some("bar".parse().unwrap()),
color: Some("blue".parse().unwrap()),
description: Some(String::from("What is a foo without its bar?")),
};
let repo = GHRepo::new("octocat", "repo").unwrap();
assert_eq!(
op.as_log_message(&repo, false).to_string(),
r#"Updating label "foo" in octocat/repo (new name: "bar", new color: "0000ff", new description: "What is a foo without its bar?")"#
);
assert_eq!(
op.as_log_message(&repo, true).to_string(),
r#"Would update label "foo" in octocat/repo (new name: "bar", new color: "0000ff", new description: "What is a foo without its bar?")"#
);
}
}

0 comments on commit 3c15a29

Please sign in to comment.