summaryrefslogtreecommitdiff
path: root/tests/util/server/src/assertions.rs
blob: f964e56e9938aaa8b9776e7dcc9559eedcd2239f (plain)
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::io::Write;

use crate::colors;

#[macro_export]
macro_rules! assert_starts_with {
  ($string:expr, $($test:expr),+) => {
    let string = $string; // This might be a function call or something
    if !($(string.starts_with($test))||+) {
      panic!("{:?} does not start with {:?}", string, [$($test),+]);
    }
  }
}

#[macro_export]
macro_rules! assert_ends_with {
  ($left:expr, $right:expr $(,)?) => {
    match (&$left, &$right) {
      (actual, expected) => {
        let actual = if expected.len() > actual.len() {
          actual
        } else {
          &actual[actual.len() - expected.len()..]
        };
        pretty_assertions::assert_eq!(
          actual,
          *expected,
          "should end with expected."
        );
      }
    }
  };
}

#[macro_export]
macro_rules! assert_contains {
  ($string:expr, $($test:expr),+ $(,)?) => {
    let string = &$string; // This might be a function call or something
    if !($(string.contains($test))||+) {
      panic!("{:?} does not contain any of {:?}", string, [$($test),+]);
    }
  }
}

#[macro_export]
macro_rules! assert_not_contains {
  ($string:expr, $($test:expr),+ $(,)?) => {
    let string = &$string; // This might be a function call or something
    if !($(!string.contains($test))||+) {
      panic!("{:?} contained {:?}", string, [$($test),+]);
    }
  }
}

#[track_caller]
pub fn assert_wildcard_match(actual: &str, expected: &str) {
  assert_wildcard_match_with_logger(actual, expected, &mut std::io::stderr())
}

#[track_caller]
pub fn assert_wildcard_match_with_logger(
  actual: &str,
  expected: &str,
  logger: &mut dyn Write,
) {
  if !expected.contains("[WILD") && !expected.contains("[UNORDERED_START]") {
    pretty_assertions::assert_eq!(actual, expected);
  } else {
    match crate::wildcard_match_detailed(expected, actual) {
      crate::WildcardMatchResult::Success => {
        // ignore
      }
      crate::WildcardMatchResult::Fail(debug_output) => {
        writeln!(
          logger,
          "{}{}{}",
          colors::bold("-- "),
          colors::bold_red("OUTPUT"),
          colors::bold(" START --"),
        )
        .unwrap();
        writeln!(logger, "{}", actual).unwrap();
        writeln!(logger, "{}", colors::bold("-- OUTPUT END --")).unwrap();
        writeln!(
          logger,
          "{}{}{}",
          colors::bold("-- "),
          colors::bold_green("EXPECTED"),
          colors::bold(" START --"),
        )
        .unwrap();
        writeln!(logger, "{}", expected).unwrap();
        writeln!(logger, "{}", colors::bold("-- EXPECTED END --")).unwrap();
        writeln!(
          logger,
          "{}{}{}",
          colors::bold("-- "),
          colors::bold_blue("DEBUG"),
          colors::bold(" START --"),
        )
        .unwrap();
        writeln!(logger, "{debug_output}").unwrap();
        writeln!(logger, "{}", colors::bold("-- DEBUG END --")).unwrap();
        panic!("pattern match failed");
      }
    }
  }
}