Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3 Implement string fakeit #27

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn main() {
- [payment](#payment-6-functions)
- [person](#person-3-functions)
- [status code](#status-code-2-functions)
- [string](#string-9-functions)
- [unique](#unique-2-functions)
- [user agent](#user-agent-8-functions)
- [vehicle](#vehicle-6-functions)
Expand Down Expand Up @@ -430,6 +431,26 @@ fn main() {
}
```

##### string (9 functions)

```rust
extern crate fakeit;

use fakeit::string;

fn main() {
let data = string::letter(); // letter: l
let data = string::letter_n(4); // letter_n: ailf
let data = string::vowel(); // vowel: e
let data = string::digit(); // digit: 7
let data = string::digit_n(2); // digit_n: 92
let data = string::numerify("H#LL# W#RLD!!".to_owned()); // string: "H3LL7 W2RLD!!"
let data = string::lexify("H?LL? W?RLD!!".to_owned()); // string: "HoLLu WiRLD!!"
let data = string::shuffle_strings(test_strings); // [String]: shuffled array of given strings
let data = string::random_string(test_strings); // random_string: a random string from the given array
}
```

##### unique (2 functions)

```rust
Expand Down
1 change: 1 addition & 0 deletions src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ pub mod lorem;
pub mod payment;
pub mod person;
pub mod status_code;
pub mod string;
pub mod vehicle;
20 changes: 20 additions & 0 deletions src/data/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[allow(dead_code)]
pub static LOWER_CASE: &'static [&str] = &[
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z",
];

#[allow(dead_code)]
pub static UPPER_CASE: &'static [&str] = &[
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z",
];

#[allow(dead_code)]
pub static LOWER_VOWELS: &'static [&str] = &["a", "e", "i", "o", "u"];

#[allow(dead_code)]
pub static UPPER_VOWELS: &'static [&str] = &["A", "E", "I", "O", "U"];

#[allow(dead_code)]
pub static DIGITS: &'static [&str] = &["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod password;
pub mod payment;
pub mod person;
pub mod status_code;
pub mod string;
pub mod unique;
pub mod user_agent;
pub mod vehicle;
Expand Down
171 changes: 171 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
use rand::seq::SliceRandom;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't use rand package because the whole package using simplerand. It is because the rand package is an overkill for this library. Also if you are using rand package and I add the custom seed value functionality to the code-base (one of the requested feature), it won't work for string functions, because it will use the rand package's seed value.

Please use simplerand, or write a wrapper/helper function for that in the misc.rs.

use rand::Rng;

use crate::data::string;
use crate::misc;

// letter will generate a single random lower case ASCII letter
pub fn letter() -> String {
rand_letter()
}

fn rand_letter() -> String {
String::from(misc::random_data(string::LOWER_CASE))
}

// letter_n will generate a random ASCII string with length N. Note that this function returns a string with a length of 1 when 0 is passed.
pub fn letter_n(n: i32) -> String {
rand_letter_n(n)
}

fn rand_letter_n(n: i32) -> String {
let mut values: String = String::from("");
let counter: i32 = if n == 0 { 1 } else { n };

for _ in 0..counter {
values = values + &*rand_letter();
}
String::from(values)
}

// vowel will generate a single random lower case vowel
pub fn vowel() -> String {
rand_vowel()
}

fn rand_vowel() -> String {
String::from(misc::random_data(string::LOWER_VOWELS))
}

// Digit will generate a single ASCII digit
pub fn digit() -> String {
rand_digit()
}

fn rand_digit() -> String {
String::from(misc::random_data(string::DIGITS))
}

// digit_n will generate a random string of length N consists of ASCII digits. Note that the string generated can start with 0 and this function returns a string with a length of 1 when 0 is passed.
pub fn digit_n(n: i32) -> String {
rand_digit_n(n)
}

fn rand_digit_n(n: i32) -> String {
let mut values: String = String::from("");
let counter: i32 = if n == 0 { 1 } else { n };

for _ in 0..counter {
values = values + &*rand_digit();
}
String::from(values)
}

// numerify will replace # with random numerical values
pub fn numerify(initial_string: String) -> String {
misc::replace_with_numbers(initial_string)
}

// lexify will replace ? with random generated letters
pub fn lexify(initial_string: String) -> String {
misc::replace_with_letter(initial_string)
}

// shuffle_strings will randomize a slice of strings
pub fn shuffle_strings(s: &mut [String]) {
let mut rng = rand::thread_rng();
s.shuffle(&mut rng)
}

// random_string will take in a slice of string and return a randomly selected value
pub fn random_string(s: &mut [String]) -> String {
return_random_string(s)
}

fn return_random_string(s: &mut [String]) -> String {
let mut rng = rand::thread_rng();
let rand_index = rng.gen_range(0, s.len() - 1);
s[rand_index].clone()
}

#[cfg(test)]
mod tests {
use crate::string;
use crate::testify::exec_mes;

#[test]
fn letter() {
exec_mes("string::letter", || string::letter());
}

#[test]
fn letter_n() {
exec_mes("string::letter_n", || string::letter_n(7));
exec_mes("string::letter_n", || string::letter_n(3));
}

#[test]
fn vowel() {
exec_mes("string::vowel", || string::vowel());
}

#[test]
fn digit() {
exec_mes("string::digit", || string::digit());
}

#[test]
fn digit_n() {
exec_mes("string::digit_n", || string::digit_n(5));
exec_mes("string::digit_n", || string::digit_n(11));
}

#[test]
fn numerify() {
exec_mes("string::numerify", || {
string::numerify("H#LL# W#RLD!!".to_owned())
});
}

#[test]
fn lexify() {
exec_mes("string::lexify", || {
string::lexify("H?LL? W?RLD!!".to_owned())
})
}

#[test]
fn shuffle_strings() {
let stable_strings: &mut [String] = &mut [
"first".to_string(),
"second".to_string(),
"third".to_string(),
"fourth".to_string(),
"fifth".to_string(),
];
let test_strings: &mut [String] = &mut [
"first".to_string(),
"second".to_string(),
"third".to_string(),
"fourth".to_string(),
"fifth".to_string(),
];
string::shuffle_strings(test_strings);
println!("{:?}", test_strings);
assert_ne!(stable_strings, test_strings)
}

#[test]
fn random_string() {
let stable_strings: &mut [String] = &mut [
"first".to_string(),
"second".to_string(),
"third".to_string(),
"fourth".to_string(),
"fifth".to_string(),
];
let return_string: String = string::random_string(stable_strings);
println!("return string: {:?}", return_string);
assert!(stable_strings.contains(&return_string))
}
}