Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait AdjectiveGenerator {
- fn random_adjective(&self) -> Box<str>;
- }
- struct FixedAdjectiveGenerator {}
- impl AdjectiveGenerator for FixedAdjectiveGenerator {
- fn random_adjective(&self) -> Box<str> {
- Box::from("ADJ")
- }
- }
- fn replace_adjectives_in<A: AdjectiveGenerator>(generator: Box<A>, review_contents: &str) -> Box<str> {
- let mut string_builder = String::new();
- for segment in review_contents.split("*") {
- if string_builder.is_empty() {
- // First segment before any asterisk
- string_builder.push_str(segment);
- continue;
- }
- let words_in_segment = segment.split(" ");
- let remaining_words = words_in_segment.skip(1);
- let random_adjective = generator.random_adjective();
- string_builder.push_str(&random_adjective);
- for remaining_word in remaining_words {
- string_builder.push(' ');
- string_builder.push_str(remaining_word);
- }
- }
- return string_builder.into_boxed_str();
- }
- #[cfg(test)]
- mod tests {
- use crate::totp::totp::{replace_adjectives_in, FixedAdjectiveGenerator, AdjectiveGenerator};
- use std::borrow::Borrow;
- use std::ops::Deref;
- #[test]
- fn replace_adjectives_in_test1() {
- let generator = Box::new(FixedAdjectiveGenerator {});
- assert_eq!(
- replace_adjectives_in(generator, "this is a *spicy review"),
- Box::from("this is a ADJ review"));
- }
- #[test]
- fn replace_adjectives_in_test2() {
- let generator = Box::new(FixedAdjectiveGenerator {});
- assert_eq!(
- replace_adjectives_in(generator, "this is a *spicy review with lots of *salty salt"),
- Box::from("this is a ADJ review with lots of ADJ salt"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement