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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::TestCaseResults;

const RED: &'static str = "rgb(255, 0, 0)";
const GREEN: &'static str = "rgb(50, 205, 50)";
const BLACK: &'static str = "rgb(0, 0, 0)";

/// Constructs an HTML visualization of a test suite.
pub struct ViewHtml {}

impl ViewHtml {
    #[allow(missing_docs)]
    pub fn new() -> Self {
        ViewHtml {}
    }

    /// Constructs an HTML visualization of a test suite.
    pub fn process_test_results(&self, test_results: &TestCaseResults) -> String {
        let mut output = format!(
            r#"<html>
  <body>
    <h1>{title}</h1>
    <p>{description}</p>"#,
            title = test_results.suite_title(),
            description = test_results.suite_description(),
        );

        for test_result in test_results.results() {
            let ok_or_failed = if test_result.did_pass() {
                "(ok)"
            } else {
                "(FAILED)"
            };

            output += &format!(
                r#"

    <div style="margin-bottom: 20px;">
      <div style="font-size: 24px; font-weight: bold;">
        {title}
        <label style="color: {ok_or_passed_color};"> {ok_or_failed}</label>
        <p style="color: {description_color}; font-size: 14px; margin: 0px;">{description}</p>
      </div>
      {html_visual}
    </div>"#,
                title = test_result.title(),
                description = test_result.description(),
                ok_or_passed_color = if test_result.did_pass() { GREEN } else { RED },
                description_color = if test_result.did_pass() { BLACK } else { RED },
                ok_or_failed = ok_or_failed,
                html_visual = test_result.metadata().get("html-visual").unwrap()
            );
        }

        output += &format!(
            r#"
  </body>
</html>"#,
        );

        output
    }
}

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

    /// Verify the output of one passing test case.
    #[test]
    fn one_test_case_passed() {
        let test_results = test_suite_one(PassedOrFailed::Passed);
        let results = ViewHtml::new().process_test_results(&test_results);

        let expected = r#"<html>
  <body>
    <h1>My Test Suite Title</h1>
    <p>My Test Suite description.</p>

    <div style="margin-bottom: 20px;">
      <div style="font-size: 24px; font-weight: bold;">
        Test Case Title
        <label style="color: rgb(50, 205, 50);"> (ok)</label>
        <p style="color: rgb(0, 0, 0); font-size: 14px; margin: 0px;">Test Case Description</p>
      </div>
      <div><em>Test case html visualization here</em></div>
    </div>
  </body>
</html>"#;

        assert_eq!(results, expected)
    }

    /// Verify the output of one failing test case.
    #[test]
    fn one_test_case_failed() {
        let test_results = test_suite_one(PassedOrFailed::Failed);
        let results = ViewHtml::new().process_test_results(&test_results);

        let expected = r#"<html>
  <body>
    <h1>My Test Suite Title</h1>
    <p>My Test Suite description.</p>

    <div style="margin-bottom: 20px;">
      <div style="font-size: 24px; font-weight: bold;">
        Test Case Title
        <label style="color: rgb(255, 0, 0);"> (FAILED)</label>
        <p style="color: rgb(255, 0, 0); font-size: 14px; margin: 0px;">Test Case Description</p>
      </div>
      <div><em>Test case html visualization here</em></div>
    </div>
  </body>
</html>"#;

        assert_eq!(results, expected)
    }

    /// Verify the output of one passing and one failing test case.
    #[test]
    fn two_test_cases_one_pass_one_fail() {
        let test_results = test_suite_two(PassedOrFailed::Passed, PassedOrFailed::Failed);
        let results = ViewHtml::new().process_test_results(&test_results);

        let expected = r#"<html>
  <body>
    <h1>My Test Suite Title</h1>
    <p>My Test Suite description.</p>

    <div style="margin-bottom: 20px;">
      <div style="font-size: 24px; font-weight: bold;">
        Test Case Title
        <label style="color: rgb(50, 205, 50);"> (ok)</label>
        <p style="color: rgb(0, 0, 0); font-size: 14px; margin: 0px;">Test Case Description</p>
      </div>
      <div><em>Test case html visualization here</em></div>
    </div>

    <div style="margin-bottom: 20px;">
      <div style="font-size: 24px; font-weight: bold;">
        Test Case Title
        <label style="color: rgb(255, 0, 0);"> (FAILED)</label>
        <p style="color: rgb(255, 0, 0); font-size: 14px; margin: 0px;">Test Case Description</p>
      </div>
      <div><em>Test case html visualization here</em></div>
    </div>
  </body>
</html>"#;

        assert_eq!(results, expected)
    }

    fn test_suite_one(pass_fail: PassedOrFailed) -> TestCaseResults {
        let mut result = TestCaseResult::new(
            test_case_title(),
            test_case_description(),
            pass_fail.did_pass(),
        );
        result.insert_metadata("html-visual".to_string(), html_visual());

        TestCaseResults::new(test_suite_title(), test_suite_description(), vec![result])
    }

    fn test_suite_two(pass_fail_1: PassedOrFailed, pass_fail_2: PassedOrFailed) -> TestCaseResults {
        let mut result1 = TestCaseResult::new(
            test_case_title(),
            test_case_description(),
            pass_fail_1.did_pass(),
        );
        result1.insert_metadata("html-visual".to_string(), html_visual());
        let mut result2 = TestCaseResult::new(
            test_case_title(),
            test_case_description(),
            pass_fail_2.did_pass(),
        );
        result2.insert_metadata("html-visual".to_string(), html_visual());

        TestCaseResults::new(
            test_suite_title(),
            test_suite_description(),
            vec![result1, result2],
        )
    }

    fn test_suite_title() -> String {
        "My Test Suite Title".to_string()
    }

    fn test_suite_description() -> String {
        "My Test Suite description.".to_string()
    }

    fn test_case_title() -> String {
        "Test Case Title".to_string()
    }

    fn test_case_description() -> String {
        "Test Case Description".to_string()
    }

    fn html_visual() -> String {
        "<div><em>Test case html visualization here</em></div>".to_string()
    }

    enum PassedOrFailed {
        Passed,
        Failed,
    }

    impl PassedOrFailed {
        fn did_pass(&self) -> bool {
            matches!(self, PassedOrFailed::Passed)
        }
    }
}