Add test and format
This commit is contained in:
@@ -25,16 +25,10 @@ impl NewPost {
|
|||||||
.content(&self.content);
|
.content(&self.content);
|
||||||
|
|
||||||
for hashtag in Self::extract_hashtags(&self.content) {
|
for hashtag in Self::extract_hashtags(&self.content) {
|
||||||
builder = builder
|
builder = builder.start_tag().tag_str("t").tag_str(&hashtag);
|
||||||
.start_tag()
|
|
||||||
.tag_str("t")
|
|
||||||
.tag_str(&hashtag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder
|
builder.sign(seckey).build().expect("note should be ok")
|
||||||
.sign(seckey)
|
|
||||||
.build()
|
|
||||||
.expect("note should be ok")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_reply(&self, seckey: &[u8; 32], replying_to: &Note) -> Note {
|
pub fn to_reply(&self, seckey: &[u8; 32], replying_to: &Note) -> Note {
|
||||||
@@ -115,15 +109,10 @@ impl NewPost {
|
|||||||
enostr::NoteId::new(*quoting.id()).to_bech().unwrap()
|
enostr::NoteId::new(*quoting.id()).to_bech().unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut builder = NoteBuilder::new()
|
let mut builder = NoteBuilder::new().kind(1).content(&new_content);
|
||||||
.kind(1)
|
|
||||||
.content(&new_content);
|
|
||||||
|
|
||||||
for hashtag in Self::extract_hashtags(&self.content) {
|
for hashtag in Self::extract_hashtags(&self.content) {
|
||||||
builder = builder
|
builder = builder.start_tag().tag_str("t").tag_str(&hashtag);
|
||||||
.start_tag()
|
|
||||||
.tag_str("t")
|
|
||||||
.tag_str(&hashtag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder
|
builder
|
||||||
@@ -142,7 +131,8 @@ impl NewPost {
|
|||||||
let mut hashtags = HashSet::new();
|
let mut hashtags = HashSet::new();
|
||||||
for word in content.split_whitespace() {
|
for word in content.split_whitespace() {
|
||||||
if word.starts_with('#') && word.len() > 1 {
|
if word.starts_with('#') && word.len() > 1 {
|
||||||
let tag = word[1..].trim_end_matches(|c: char| !c.is_alphanumeric())
|
let tag = word[1..]
|
||||||
|
.trim_end_matches(|c: char| !c.is_alphanumeric())
|
||||||
.to_lowercase();
|
.to_lowercase();
|
||||||
if !tag.is_empty() {
|
if !tag.is_empty() {
|
||||||
hashtags.insert(tag);
|
hashtags.insert(tag);
|
||||||
@@ -170,17 +160,16 @@ mod tests {
|
|||||||
("Testing emoji with space #🍌 sfd", vec!["🍌"]),
|
("Testing emoji with space #🍌 sfd", vec!["🍌"]),
|
||||||
("Duplicate #tag #tag #tag", vec!["tag"]),
|
("Duplicate #tag #tag #tag", vec!["tag"]),
|
||||||
("Mixed case #TaG #tag #TAG", vec!["tag"]),
|
("Mixed case #TaG #tag #TAG", vec!["tag"]),
|
||||||
|
(
|
||||||
|
"#tag1, #tag2, #tag3 with commas",
|
||||||
|
vec!["tag1", "tag2", "tag3"],
|
||||||
|
),
|
||||||
];
|
];
|
||||||
|
|
||||||
for (input, expected) in test_cases {
|
for (input, expected) in test_cases {
|
||||||
let result = NewPost::extract_hashtags(input);
|
let result = NewPost::extract_hashtags(input);
|
||||||
let expected: HashSet<String> = expected.into_iter().map(String::from).collect();
|
let expected: HashSet<String> = expected.into_iter().map(String::from).collect();
|
||||||
assert_eq!(
|
assert_eq!(result, expected, "Failed for input: {}", input);
|
||||||
result,
|
|
||||||
expected,
|
|
||||||
"Failed for input: {}",
|
|
||||||
input
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user