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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use crate::{ShouldStartWithString, caller_name::get_caller_name};

impl ShouldStartWithString for str {
    fn should_start_with(&self, expected: impl Into<String>) {
        let lhs: String = self.into();
        let rhs: String = expected.into();
        let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
        if !lhs.starts_with(&rhs) {
            panic!("{} should start with {:?} but was {:?}", caller_name, rhs, lhs)
        }
    }

    fn should_not_start_with(&self, expected: impl Into<String>) {
        let lhs: String = self.into();
        let rhs: String = expected.into();
        let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
        if lhs.starts_with(&rhs) {
            panic!("{} should not start with {:?} but was {:?}", caller_name, rhs, lhs)
        }
    }

    fn should_end_with(&self, expected: impl Into<String>) {
        let lhs: String = self.into();
        let rhs: String = expected.into();
        let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
        if !lhs.ends_with(&rhs) {
            panic!("{} should end with {:?} but was {:?}", caller_name, rhs, lhs)
        }
    }

    fn should_not_end_with(&self, expected: impl Into<String>) {
        let lhs: String = self.into();
        let rhs: String = expected.into();
        let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
        if lhs.ends_with(&rhs) {
            panic!("{} should not end with {:?} but was {:?}", caller_name, rhs, lhs)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn success() {
        "foobar".should_start_with("foo");
        "foobar".to_string().should_start_with("foo");
        "foobar".should_start_with("foo".to_string());
        "foobar".to_string().should_start_with("foo".to_string());
        
        "foobar".should_not_start_with("bar");
        "foobar".to_string().should_not_start_with("bar");
        "foobar".should_not_start_with("bar".to_string());
        "foobar".to_string().should_not_start_with("bar".to_string());

        "foobar".should_end_with("bar");
        "foobar".to_string().should_end_with("bar");
        "foobar".should_end_with("bar".to_string());
        "foobar".to_string().should_end_with("bar".to_string());
        
        "foobar".should_not_end_with("foo");
        "foobar".to_string().should_not_end_with("foo");
        "foobar".should_not_end_with("foo".to_string());
        "foobar".to_string().should_not_end_with("foo".to_string());
    }

    #[test]
    #[should_panic(expected = r#""foobar" should start with "bar" but was "foobar""#)]
    fn should_start_with_fail() {
        "foobar".should_start_with("bar");
    }

    #[test]
    #[should_panic(expected = r#""foobar" should not start with "foo" but was "foobar""#)]
    fn should_not_start_with_fail() {
        "foobar".should_not_start_with("foo");
    }

    #[test]
    #[should_panic(expected = r#""foobar" should end with "foo" but was "foobar""#)]
    fn should_end_with_fail() {
        "foobar".should_end_with("foo");
    }

    #[test]
    #[should_panic(expected = r#""foobar" should not end with "bar" but was "foobar""#)]
    fn should_not_end_with_fail() {
        "foobar".should_not_end_with("bar");
    }
}