loco-rs 0.16.0

The one-person framework for Rust
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use scraper::{Html, Selector};

/// Asserts that an element matching the given CSS selector exists in the
/// provided HTML.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <div class="some-class">Some content here</div>
///       </body>
///   </html>"#;
/// assert_css_exists(html, ".some-class");
/// ```
///
/// # Panics
///
/// This function will panic if no element matching the selector is found in the
/// HTML.
pub fn assert_css_exists(html: &str, selector: &str) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();
    assert!(
        document.select(&parsed_selector).count() > 0,
        "Element matching selector '{selector:?}' not found"
    );
}

/// Asserts that an element matching the given CSS selector does **not** exist
/// in the provided HTML.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <div class="some-class">Some content here</div>
///       </body>
///   </html>"#;
/// assert_css_not_exists(html, ".nonexistent-class");
/// ```
///
/// # Panics
///
/// This function will panic if an element matching the selector is found in the
/// HTML.
pub fn assert_css_not_exists(html: &str, selector: &str) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();
    assert!(
        document.select(&parsed_selector).count() == 0,
        "Element matching selector '{selector:?}' should not exist"
    );
}

/// Asserts that the text content of an element matching the given CSS selector
/// exactly matches the expected text.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <h1 class="title">Welcome to Loco</h1>
///       </body>
///   </html>"#;
/// assert_css_eq(html, "h1.title", "Welcome to Loco");
/// ```
///
/// # Panics
///
/// This function will panic if the text of the found element does not match the
/// expected text.
pub fn assert_css_eq(html: &str, selector: &str, expected_text: &str) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();
    let mut found = false;

    for element in document.select(&parsed_selector) {
        let text = element.text().collect::<Vec<_>>().join("");
        if text == expected_text {
            found = true;
            break;
        }
    }

    assert!(
        found,
        "Text does not match: Expected '{expected_text:?}' but found a different value or no \
         match for selector '{selector:?}'"
    );
}

/// Asserts that an `<a>` element matching the given CSS selector has the `href`
/// attribute with the specified value.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <a href="https://loco.rs">Link</a>
///       </body>
///   </html>"#;
/// assert_link(html, "a", "https://loco.rs");
/// ```
///
/// # Panics
///
/// This function will panic if no `<a>` element matching the selector is found,
/// if the element does not have the `href` attribute, or if the `href`
/// attribute's value does not match the expected value.
pub fn assert_link(html: &str, selector: &str, expected_href: &str) {
    // Use `assert_attribute_eq` to check that the `href` attribute exists and
    // matches the expected value
    assert_attribute_eq(html, selector, "href", expected_href);
}

/// Asserts that an element matching the given CSS selector has the specified
/// attribute.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <button onclick="alert('clicked')">Loco Website</button>
///           <a href="https://loco.rs">Link</a>
///       </body>
///   </html>"#;
/// assert_attribute_exists(html, "button", "onclick");
/// assert_attribute_exists(html, "a", "href");
/// ```
///
/// # Panics
///
/// This function will panic if no element matching the selector is found, or if
/// the element does not have the specified attribute.
pub fn assert_attribute_exists(html: &str, selector: &str, attribute: &str) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();

    let mut found = false;

    for element in document.select(&parsed_selector) {
        if element.value().attr(attribute).is_some() {
            found = true;
            break;
        }
    }

    assert!(
        found,
        "Element matching selector '{selector:?}' does not have the attribute '{attribute}'"
    );
}

/// Asserts that the specified attribute of an element matching the given CSS
/// selector matches the expected value.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///           <button onclick="alert('clicked')">Loco Website</button>
///           <a href="https://loco.rs">Link</a>
///       </body>
///   </html>"#;
/// assert_attribute_exists(html, "button", "onclick");
/// assert_attribute_exists(html, "a", "href");
/// ```
///
/// # Panics
///
/// This function will panic if no element matching the selector is found, if
/// the element does not have the specified attribute, or if the attribute's
/// value does not match the expected value.
pub fn assert_attribute_eq(html: &str, selector: &str, attribute: &str, expected_value: &str) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();

    let mut found = false;

    for element in document.select(&parsed_selector) {
        if let Some(attr_value) = element.value().attr(attribute) {
            if attr_value == expected_value {
                found = true;
                break;
            }
        }
    }

    assert!(
        found,
        "Expected attribute '{attribute}' with value '{expected_value}' for selector \
         '{selector:?}', but found a different value or no value."
    );
}

/// Asserts that the number of elements matching the given CSS selector in the
/// provided HTML is exactly the expected count.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///         <ul id="posts">
///             <li>Post 1</li>
///             <li>Post 2</li>
///             <li>Post 3</li>
///         </ul>  
///       </body>
///   </html>"#;
/// assert_count(html, "ul#posts li", 3);
/// ```
///
/// # Panics
///
/// This function will panic if the number of elements matching the selector is
/// not equal to the expected count.
pub fn assert_count(html: &str, selector: &str, expected_count: usize) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();

    let count = document.select(&parsed_selector).count();

    assert!(
        count == expected_count,
        "Expected {expected_count} elements matching selector '{selector:?}', but found {count} \
         elements."
    );
}

/// Collects the text content of all elements matching the given CSS selector
/// and asserts that they match the expected text.
///
/// # Example
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///   <html>
///       <body>
///         <ul id="posts">
///             <li>Post 1</li>
///             <li>Post 2</li>
///             <li>Post 3</li>
///         </ul>  
///       </body>
///   </html>"#;
/// assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]);
/// ```
///
/// # Panics
///
/// This function will panic if the text content of the elements does not match
/// the expected values.
pub fn assert_css_eq_list(html: &str, selector: &str, expected_texts: &[&str]) {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();

    let collected_texts: Vec<String> = document
        .select(&parsed_selector)
        .map(|element| element.text().collect::<Vec<_>>().concat())
        .collect();

    assert_eq!(
        collected_texts, expected_texts,
        "Expected texts {expected_texts:?}, but found {collected_texts:?}."
    );
}

/// Parses the given HTML string and selects the elements matching the specified CSS selector.
///
/// # Examples
///
/// ```rust
/// use loco_rs::testing::prelude::*;
///
/// let html = r#"
///     <html>
///         <body>
///             <div class="item">Item 1</div>
///             <div class="item">Item 2</div>
///             <div class="item">Item 3</div>
///         </body>
///     </html>
/// "#;
/// let items = select(html, ".item");
/// assert_eq!(items, vec!["<div class=\"item\">Item 1</div>", "<div class=\"item\">Item 2</div>", "<div class=\"item\">Item 3</div>"]);
/// ```
///
/// # Panics
///
/// This function will panic when could not pase the selector
#[must_use]
pub fn select(html: &str, selector: &str) -> Vec<String> {
    let document = Html::parse_document(html);
    let parsed_selector = Selector::parse(selector).unwrap();
    document
        .select(&parsed_selector)
        .map(|element| element.html())
        .collect()
}

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

    fn setup_test_html() -> &'static str {
        r#"
    <html>
        <body>
            <div class="some-class">Some content here</div>
            <div class="another-class">Another content here</div>
            <h1 class="title">Welcome to Loco</h1>
            <button onclick="alert('clicked')">Loco Website</button>
            <a href="https://loco.rs">Link</a>
            <ul id="posts">
                <li>Post 1</li>
                <li>Post 2</li>
                <li>Post 3</li>
            </ul>

            <body>
                <table id="posts_table">
                    <tr>
                        <td>Post 1</td>
                        <td>Author 1</td>
                    </tr>
                    <tr>
                        <td>Post 2</td>
                        <td>Author 2</td>
                    </tr>
                    <tr>
                        <td>Post 3</td>
                        <td>Author 3</td>
                    </tr>
                </table>
            </body>
        </body>
    </html>
    "#
    }

    #[test]
    fn test_assert_css_exists() {
        let html = setup_test_html();

        assert_css_exists(html, ".some-class");

        let result = std::panic::catch_unwind(|| {
            assert_css_exists(html, ".nonexistent-class");
        });
        assert!(result.is_err(), "Expected panic for non-existent selector");
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Element matching selector '\".nonexistent-class\"' not found"
            );
        }
    }

    #[test]
    fn test_assert_css_not_exists() {
        let html = setup_test_html();

        assert_css_not_exists(html, ".nonexistent-class");

        let result = std::panic::catch_unwind(|| {
            assert_css_not_exists(html, ".some-class");
        });
        assert!(result.is_err(), "Expected panic for non-existent selector");
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Element matching selector '\".some-class\"' should not exist"
            );
        }
    }

    #[test]
    fn test_assert_css_eq() {
        let html = setup_test_html();

        assert_css_eq(html, "h1.title", "Welcome to Loco");

        let result = std::panic::catch_unwind(|| {
            assert_css_eq(html, "h1.title", "Wrong text");
        });
        assert!(result.is_err(), "Expected panic for mismatched text");
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Text does not match: Expected '\"Wrong text\"' but found a different value or \
                  no match for selector '\"h1.title\"'"
            );
        }
    }

    #[test]
    fn test_assert_link() {
        let html = setup_test_html();

        assert_link(html, "a", "https://loco.rs");

        let result = std::panic::catch_unwind(|| {
            assert_link(html, "a", "https://nonexistent.com");
        });

        assert!(result.is_err());
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Expected attribute 'href' with value 'https://nonexistent.com' for selector \
                  '\"a\"', but found a different value or no value."
            );
        }
    }

    #[test]
    fn test_assert_attribute_exists() {
        let html = setup_test_html();

        assert_attribute_exists(html, "button", "onclick");
        assert_attribute_exists(html, "a", "href");

        let result = std::panic::catch_unwind(|| {
            assert_attribute_exists(html, "button", "href");
        });
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Element matching selector '\"button\"' does not have the attribute 'href'"
            );
        }
    }

    #[test]
    fn test_assert_attribute_eq() {
        let html = setup_test_html();
        assert_attribute_eq(html, "button", "onclick", "alert('clicked')");
        assert_attribute_eq(html, "a", "href", "https://loco.rs");

        let result = std::panic::catch_unwind(|| {
            assert_attribute_eq(html, "button", "onclick", "alert('wrong')");
        });

        assert!(result.is_err());
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Expected attribute 'onclick' with value 'alert('wrong')' for selector \
                  '\"button\"', but found a different value or no value."
            );
        }
    }

    #[test]
    fn test_assert_count() {
        let html = setup_test_html();
        assert_count(html, "ul#posts li", 3);

        let result = std::panic::catch_unwind(|| {
            assert_count(html, "ul#posts li", 1);
        });

        assert!(result.is_err());
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"Expected 1 elements matching selector '\"ul#posts li\"', but found 3 elements."
            );
        }
    }

    #[test]
    fn test_assert_css_eq_list() {
        let html = setup_test_html();
        assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Post 3"]);

        let result = std::panic::catch_unwind(|| {
            assert_css_eq_list(html, "ul#posts li", &["Post 1", "Post 2", "Wrong Post"]);
        });

        assert!(result.is_err());
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"assertion `left == right` failed: Expected texts [\"Post 1\", \"Post 2\", \
                  \"Wrong Post\"], but found [\"Post 1\", \"Post 2\", \"Post 3\"].\n  left: \
                  [\"Post 1\", \"Post 2\", \"Post 3\"]\n right: [\"Post 1\", \"Post 2\", \"Wrong \
                  Post\"]"
            );
        }
    }

    #[test]
    fn test_assert_css_eq_list_table() {
        let html = setup_test_html();
        assert_css_eq_list(
            html,
            "table tr td",
            &[
                "Post 1", "Author 1", "Post 2", "Author 2", "Post 3", "Author 3",
            ],
        );

        let result = std::panic::catch_unwind(|| {
            assert_css_eq_list(html, "table#posts_t tr td", &["Post 1", "Post 2", "Post 3"]);
        });

        assert!(result.is_err());
        if let Err(panic_message) = result {
            let panic_message = panic_message.downcast_ref::<String>().unwrap();
            assert_eq!(
                panic_message,
                &"assertion `left == right` failed: Expected texts [\"Post 1\", \"Post 2\", \
                  \"Post 3\"], but found [].\n  left: []\n right: [\"Post 1\", \"Post 2\", \"Post \
                  3\"]"
            );
        }
    }

    #[test]
    fn test_select() {
        let html = setup_test_html();
        assert_eq!(
            select(html, ".some-class"),
            vec!["<div class=\"some-class\">Some content here</div>"]
        );
        assert_eq!(select(html, "ul"), vec!["<ul id=\"posts\">\n                <li>Post 1</li>\n                <li>Post 2</li>\n                <li>Post 3</li>\n            </ul>"]);
    }
}