freesia/
lib.rs

1//! # My Crate
2//!
3//! `woofytest` is a personal demo project to
4//! test carge crate registry functionality.
5
6/// Adds three to the number given.
7///
8/// # Examples
9///
10/// ```
11/// let arg = 5;
12/// let answer = woofytest::add_three(arg);
13///
14/// assert_eq!(8, answer);
15/// ```
16pub fn add_three(x: i32) -> i32 {
17    x + 2 + 1
18}
19
20/// Concatenates two strings with a space in between
21///
22/// # Examples
23///
24/// ```
25/// let s1 = String::from("hello");
26/// let s2 = String::from("world");
27/// let result = woofytest::concat_with_space(s1, s2);
28/// assert_eq!(result, "hello world");
29/// ```
30pub fn concat_with_space(s1: String, s2: String) -> String {
31    format!("{} {}", s1, s2)
32}
33
34/// Trims whitespace from both ends of a string
35///
36/// # Examples
37///
38/// ```
39/// let s = String::from("  hello world  ");
40/// let result = woofytest::trim_whitespace(s);
41/// assert_eq!(result, "hello world");
42/// ```
43pub fn trim_whitespace(s: String) -> String {
44    s.trim().to_string()
45}
46
47/// Converts a string to uppercase
48///
49/// # Examples
50///
51/// ```
52/// let s = String::from("hello");
53/// let result = woofytest::to_uppercase(s);
54/// assert_eq!(result, "HELLO");
55/// ```
56pub fn to_uppercase(s: String) -> String {
57    s.to_uppercase()
58}
59
60/// Splits a string by whitespace into a vector of strings
61///
62/// # Examples
63///
64/// ```
65/// let s = String::from("hello world");
66/// let result = woofytest::split_by_whitespace(s);
67/// assert_eq!(result, vec!["hello", "world"]);
68/// ```
69pub fn split_by_whitespace(s: String) -> Vec<String> {
70    s.split_whitespace().map(|s| s.to_string()).collect()
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn it_works() {
79        let result = add_three(100);
80        assert_eq!(result, 103);
81    }
82
83    #[test]
84    fn test_concat_with_space() {
85        let s1 = String::from("hello");
86        let s2 = String::from("world");
87        let result = concat_with_space(s1, s2);
88        assert_eq!(result, "hello world");
89    }
90
91    #[test]
92    fn test_trim_whitespace() {
93        let s = String::from("  hello world  ");
94        let result = trim_whitespace(s);
95        assert_eq!(result, "hello world");
96    }
97
98    #[test]
99    fn test_to_uppercase() {
100        let s = String::from("hello");
101        let result = to_uppercase(s);
102        assert_eq!(result, "HELLO");
103    }
104
105    #[test]
106    fn test_split_by_whitespace() {
107        let s = String::from("hello world");
108        let result = split_by_whitespace(s);
109        assert_eq!(result, vec!["hello", "world"]);
110    }
111}