-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
60 lines (54 loc) · 1.61 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! `spongemock` provides a convenient way to MoCk ThInGs in the style of
//! [Mocking SpongeBob](https://knowyourmeme.com/memes/mocking-spongebob).
/// Returns a MoCkInG version of the given input
///
/// # Example
///
/// ```
/// let mocked = spongemock::mock(&"a sensible EXAMPLE.");
///
/// assert_eq!("A sEnSiBlE eXaMpLe.", mocked);
/// ```
pub fn mock(input: &str) -> String {
let mut out = String::with_capacity(input.len());
let mut make_uppercase = true;
for c in input.chars() {
if make_uppercase && c.is_lowercase() {
for upper in c.to_uppercase() {
out.push(upper);
}
} else if !make_uppercase && c.is_uppercase() {
for upper in c.to_lowercase() {
out.push(upper);
}
} else {
out.push(c);
}
if c.is_alphabetic() {
make_uppercase = !make_uppercase;
}
}
out
}
#[cfg(test)]
mod test {
use super::mock;
#[test]
fn basic_mocking() {
assert_eq!(
mock(&"abcdefghijklmnopqrstuvwxyz"),
"AbCdEfGhIjKlMnOpQrStUvWxYz".to_string()
);
assert_eq!(
mock(&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
"AbCdEfGhIjKlMnOpQrStUvWxYz".to_string()
);
assert_eq!(mock(&"a1b2c3 d4e5f6"), "A1b2C3 d4E5f6".to_string());
assert_eq!(
mock(&"1234567890!@#$%^&*()-=_+"),
"1234567890!@#$%^&*()-=_+".to_string()
);
assert_eq!(mock(&"äöüßßâç"), "ÄöÜßSSâÇ".to_string());
assert_eq!(mock(&"✌🤞😎"), "✌🤞😎".to_string());
}
}