Skip to content

Commit

Permalink
Add tests for name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
akiomik committed Jan 1, 2024
1 parent 7de0b2d commit b5f155e
Showing 1 changed file with 81 additions and 3 deletions.
84 changes: 81 additions & 3 deletions src/widgets/text_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ impl TextNote {
pub fn name(&self) -> Option<String> {
if let Some(profile) = self.profile.clone() {
if let Some(name) = profile.name {
if let Some(display_name) = self.display_name() {
if !name.is_empty() && display_name != name {
return Some(format!("@{name}"));
if !name.is_empty() {
match self.display_name() {
Some(display_name) if name == display_name => return None,
_ => return Some(format!("@{name}")),
}
}
}
Expand Down Expand Up @@ -204,6 +205,83 @@ mod tests {
).unwrap()
}

#[fixture]
fn area() -> Rect {
Rect::new(0, 0, 0, 0)
}

#[fixture]
fn padding() -> Padding {
Padding::new(0, 0, 0, 0)
}

#[rstest]
fn test_pubkey(event: Event, area: Rect, padding: Padding) {
let note = TextNote::new(
event,
None,
HashSet::new(),
HashSet::new(),
HashSet::new(),
area,
padding,
);
assert_eq!(note.pubkey(), "4d39c:aae25");
}

#[rstest]
#[case(None, Some(String::from("4d39c:aae25")))]
#[case(Some(Metadata::new()), Some(String::from("4d39c:aae25")))]
#[case(Some(Metadata::new().name("foo")), None)]
#[case(Some(Metadata::new().display_name("foo")), Some(String::from("foo")))]
#[case(Some(Metadata::new().display_name("")), Some(String::from("4d39c:aae25")))]
#[case(Some(Metadata::new().display_name("").name("")), Some(String::from("4d39c:aae25")))]
#[case(Some(Metadata::new().display_name("").name("hoge")), None)]
fn test_display_name(
#[case] metadata: Option<Metadata>,
#[case] expected: Option<String>,
event: Event,
area: Rect,
padding: Padding,
) {
let note = TextNote::new(
event,
metadata,
HashSet::new(),
HashSet::new(),
HashSet::new(),
area,
padding,
);
assert_eq!(note.display_name(), expected);
}

#[rstest]
#[case(None, None)]
#[case(Some(Metadata::new()), None)]
#[case(Some(Metadata::new().name("foo")), Some(String::from("@foo")))]
#[case(Some(Metadata::new().display_name("foo")), None)]
#[case(Some(Metadata::new().name("")), None)]
#[case(Some(Metadata::new().name("foo").display_name("foo")), None)]
fn test_name(
#[case] metadata: Option<Metadata>,
#[case] expected: Option<String>,
event: Event,
area: Rect,
padding: Padding,
) {
let note = TextNote::new(
event,
metadata,
HashSet::new(),
HashSet::new(),
HashSet::new(),
area,
padding,
);
assert_eq!(note.name(), expected);
}

#[rstest]
fn test_created_at(event: Event) {
let note = TextNote::new(
Expand Down

0 comments on commit b5f155e

Please sign in to comment.