pomsky_syntax/exprs/
test.rs

1use crate::Span;
2
3use super::Literal;
4
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
7pub struct Test {
8    pub cases: Vec<TestCase>,
9    pub span: Span,
10}
11
12#[derive(Debug, Clone)]
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14pub enum TestCase {
15    Match(TestCaseMatch),
16    MatchAll(TestCaseMatchAll),
17    Reject(TestCaseReject),
18}
19
20#[derive(Debug, Clone)]
21#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
22pub struct TestCaseMatch {
23    pub literal: Literal,
24    pub captures: Vec<TestCapture>,
25    pub span: Span,
26}
27
28#[derive(Debug, Clone)]
29#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
30pub struct TestCaseMatchAll {
31    pub literal: Literal,
32    pub matches: Vec<TestCaseMatch>,
33}
34
35#[derive(Debug, Clone)]
36#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
37pub struct TestCaseReject {
38    pub literal: Literal,
39    pub as_substring: bool,
40}
41
42#[derive(Debug, Clone)]
43#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
44pub struct TestCapture {
45    pub ident: CaptureIdent,
46    pub ident_span: Span,
47    pub literal: Literal,
48}
49
50#[derive(Debug, Clone)]
51#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
52pub enum CaptureIdent {
53    Name(String),
54    Index(u16),
55}
56
57impl TestCase {
58    #[cfg(feature = "dbg")]
59    pub(super) fn pretty_print(&self, buf: &mut crate::PrettyPrinter) {
60        match self {
61            TestCase::Match(match_) => {
62                buf.push_str("match ");
63                match_.pretty_print(buf);
64                buf.push(';');
65            }
66            TestCase::MatchAll(match_all) => {
67                buf.push_str("match ");
68                let len = match_all.matches.len();
69                buf.increase_indentation(if len == 0 { 3 } else { 6 });
70
71                for (i, match_) in match_all.matches.iter().enumerate() {
72                    match_.pretty_print(buf);
73                    if i < len - 1 {
74                        buf.push(',');
75                    } else {
76                        buf.decrease_indentation(3);
77                    }
78                    buf.write("\n");
79                }
80                buf.push_str("in ");
81                match_all.literal.pretty_print(buf);
82                buf.decrease_indentation(3);
83                buf.push(';');
84            }
85            TestCase::Reject(reject) => {
86                buf.push_str("reject ");
87                if reject.as_substring {
88                    buf.push_str("in ");
89                }
90                reject.literal.pretty_print(buf);
91                buf.push(';');
92            }
93        }
94    }
95}
96
97impl TestCaseMatch {
98    #[cfg(feature = "dbg")]
99    pub(super) fn pretty_print(&self, buf: &mut crate::PrettyPrinter) {
100        self.literal.pretty_print(buf);
101
102        if !self.captures.is_empty() {
103            buf.start_indentation(" as {");
104
105            let len = self.captures.len();
106            for (i, capture) in self.captures.iter().enumerate() {
107                match &capture.ident {
108                    CaptureIdent::Name(name) => buf.push_str(name),
109                    CaptureIdent::Index(idx) => buf.write_fmt(idx),
110                }
111                buf.push_str(": ");
112                capture.literal.pretty_print(buf);
113                buf.push(',');
114                if i < len - 1 {
115                    buf.write("\n");
116                }
117            }
118            buf.end_indentation("}");
119        }
120    }
121}