|
| 1 | +//! This module contains utilities meant only for testing. |
| 2 | +//! Do not use outside of tests. |
| 3 | +
|
| 4 | +use arbitrary::Arbitrary; |
| 5 | + |
| 6 | +/// A template to generate formatting variations of a piece of code. |
| 7 | +#[derive(Debug, Clone)] |
| 8 | +pub struct Template(Vec<Span>); |
| 9 | + |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub enum Span { |
| 12 | + /// A fixed string. |
| 13 | + F(String), |
| 14 | + /// Zero or more whitespace characters. |
| 15 | + Z, |
| 16 | + /// One or more whitespace characters. |
| 17 | + P, |
| 18 | +} |
| 19 | + |
| 20 | +/// Describes a variation the template can generate. |
| 21 | +#[derive(Debug, Clone, Arbitrary)] |
| 22 | +pub struct Variation(Vec<Whitespace>); |
| 23 | + |
| 24 | +impl Default for Variation { |
| 25 | + fn default() -> Self { |
| 26 | + Variation(vec![]) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl Template { |
| 31 | + /// New empty template. |
| 32 | + pub fn new() -> Self { |
| 33 | + Template(vec![]) |
| 34 | + } |
| 35 | + |
| 36 | + /// Add a placeholder for zero or more whitespace characters. |
| 37 | + pub fn z(mut self) -> Self { |
| 38 | + self.0.push(Span::Z); |
| 39 | + self |
| 40 | + } |
| 41 | + |
| 42 | + /// Add a placeholder for one or more whitespace characters. |
| 43 | + pub fn p(mut self) -> Self { |
| 44 | + self.0.push(Span::P); |
| 45 | + self |
| 46 | + } |
| 47 | + |
| 48 | + /// Add a fixed span of text. |
| 49 | + pub fn fixed(mut self: Self, text: &str) -> Self { |
| 50 | + self.0.push(Span::F(text.to_string())); |
| 51 | + self |
| 52 | + } |
| 53 | + |
| 54 | + /// Add tokens, replacing each internal span of whitespace with a `.z()`. |
| 55 | + pub fn tokens(mut self: Self, text: &str) -> Self { |
| 56 | + for (i, non_whitespace) in text.split_whitespace().enumerate() { |
| 57 | + if i > 0 { |
| 58 | + self.0.push(Span::Z); |
| 59 | + } |
| 60 | + self.0.push(Span::F(non_whitespace.to_string())); |
| 61 | + } |
| 62 | + self |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl Template { |
| 67 | + /// Generates an instantiation of the template based on variation information. |
| 68 | + pub fn generate(&self, variation: Variation) -> String { |
| 69 | + let mut output = String::new(); |
| 70 | + |
| 71 | + let mut whitespaces = variation.0.into_iter(); |
| 72 | + |
| 73 | + for span in &self.0 { |
| 74 | + match span { |
| 75 | + Span::F(text) => output.push_str(text), |
| 76 | + Span::Z => { |
| 77 | + let whitespace = whitespaces.next().unwrap_or_default(); |
| 78 | + output.push_str(&whitespace.to_string()); |
| 79 | + } |
| 80 | + Span::P => { |
| 81 | + let whitespace = whitespaces.next().unwrap_or_default(); |
| 82 | + output.push_str(&whitespace.to_non_empty_string()); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + output |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// A non-empty sequence of whitespace characters. |
| 92 | +#[derive(Debug, Clone, Arbitrary)] |
| 93 | +pub struct Whitespace(Vec<WsChar>, WsChar); |
| 94 | + |
| 95 | +impl Default for Whitespace { |
| 96 | + fn default() -> Self { |
| 97 | + Whitespace(vec![], WsChar::Space) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Whitespace { |
| 102 | + fn to_string(self: Whitespace) -> String { |
| 103 | + self.0.into_iter().map(|w| char::from(w)).collect() |
| 104 | + } |
| 105 | + |
| 106 | + fn to_non_empty_string(self: Whitespace) -> String { |
| 107 | + self.0 |
| 108 | + .into_iter() |
| 109 | + .chain(std::iter::once(self.1)) |
| 110 | + .map(|w| char::from(w)) |
| 111 | + .collect() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// A code point Rust recognizes as whitespace. |
| 116 | +#[derive(Debug, Clone, Copy, Arbitrary)] |
| 117 | +pub enum WsChar { |
| 118 | + Space, |
| 119 | + Tab, |
| 120 | + Newline, |
| 121 | + // TODO: expand |
| 122 | +} |
| 123 | + |
| 124 | +impl From<WsChar> for char { |
| 125 | + fn from(value: WsChar) -> Self { |
| 126 | + match value { |
| 127 | + WsChar::Space => ' ', |
| 128 | + WsChar::Tab => '\t', |
| 129 | + WsChar::Newline => '\n', |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments