generated_permutations.rs
1 mod common; 2 use common::{assert_pair, assert_exported}; 3 4 // Macro to generate tests for single chars as keys 5 macro_rules! tests_for_chars { 6 ($($char:ident),*) => { 7 paste::paste! { 8 $( 9 #[test] 10 #[allow(non_snake_case)] 11 fn [<test_key_char_ $char>]() { 12 let c = stringify!($char); 13 let input = format!("{}=v", c); 14 assert_pair(&input, c, "v"); 15 } 16 )* 17 } 18 } 19 } 20 21 tests_for_chars!( 22 A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, 23 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z 24 ); 25 26 // 0-9 for value content 27 macro_rules! tests_for_val_digits { 28 ($($d:literal),*) => { 29 paste::paste! { 30 $( 31 #[test] 32 fn [<test_val_digit_ $d>]() { 33 let s = stringify!($d); 34 let input = format!("K={}", s); 35 assert_pair(&input, "K", s); 36 } 37 )* 38 } 39 } 40 } 41 42 tests_for_val_digits!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); 43 44 // Special chars 45 macro_rules! tests_for_specials { 46 ($name:ident, $sym:expr) => { 47 paste::paste! { 48 #[test] 49 fn [<test_special_single_ $name>]() { 50 let input = format!("K='{}'", $sym); 51 assert_pair(&input, "K", $sym); 52 } 53 #[test] 54 fn [<test_special_double_ $name>]() { 55 let input = format!("K=\"{}\"", $sym); 56 assert_pair(&input, "K", $sym); 57 } 58 } 59 } 60 } 61 62 tests_for_specials!(exclamation, "!"); 63 tests_for_specials!(at, "@"); 64 tests_for_specials!(hash, "#"); 65 tests_for_specials!(percent, "%"); 66 tests_for_specials!(caret, "^"); 67 tests_for_specials!(ampersand, "&"); 68 tests_for_specials!(asterisk, "*"); 69 tests_for_specials!(lparen, "("); 70 tests_for_specials!(rparen, ")"); 71 tests_for_specials!(minus, "-"); 72 tests_for_specials!(plus, "+"); 73 tests_for_specials!(equals, "="); 74 tests_for_specials!(lbracket, "["); 75 tests_for_specials!(rbracket, "]"); 76 tests_for_specials!(lbrace, "{"); 77 tests_for_specials!(rbrace, "}"); 78 tests_for_specials!(pipe, "|"); 79 tests_for_specials!(colon, ":"); 80 tests_for_specials!(semicolon, ";"); 81 tests_for_specials!(less, "<"); 82 tests_for_specials!(greater, ">"); 83 tests_for_specials!(comma, ","); 84 tests_for_specials!(dot, "."); 85 tests_for_specials!(question, "?"); 86 tests_for_specials!(slash, "/"); 87 tests_for_specials!(tilde, "~"); 88 tests_for_specials!(backtick, "`"); 89 90 // Whitespace padding variations 91 macro_rules! gen_padding_tests { 92 ($($n:literal),*) => { 93 paste::paste! { 94 $( 95 #[test] 96 fn [<test_pad_key_ $n>]() { 97 let pad = " ".repeat($n); 98 let input = format!("{}K=v", pad); 99 assert_pair(&input, "K", "v"); 100 } 101 #[test] 102 fn [<test_pad_val_ $n>]() { 103 let pad = " ".repeat($n); 104 let input = format!("K=v{}", pad); 105 assert_pair(&input, "K", "v"); 106 } 107 #[test] 108 fn [<test_pad_export_ $n>]() { 109 let pad = " ".repeat($n); 110 let input = format!("{}export K=v", pad); 111 assert_exported(&input, "K", "v"); 112 } 113 )* 114 } 115 } 116 } 117 118 gen_padding_tests!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); 119 gen_padding_tests!(21, 22, 23, 24, 25, 26, 27, 28, 29, 30); 120 121 // Duplicate key counts 122 macro_rules! gen_duplicate_tests { 123 ($($n:literal),*) => { 124 paste::paste! { 125 $( 126 #[test] 127 fn [<test_dup_ $n>]() { 128 let mut input = String::new(); 129 for i in 0..$n { 130 input.push_str(&format!("K=v{}\n", i)); 131 } 132 let entries = korni::parse(&input); 133 assert_eq!(entries.len(), $n); 134 } 135 )* 136 } 137 } 138 } 139 140 gen_duplicate_tests!(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); 141 142 // Inline comment padding 143 macro_rules! gen_comment_pad_tests { 144 ($($n:literal),*) => { 145 paste::paste! { 146 $( 147 #[test] 148 fn [<test_comment_pad_ $n>]() { 149 let pad = " ".repeat($n); 150 let input = format!("K=v{}# comment", pad); 151 assert_pair(&input, "K", "v"); 152 } 153 )* 154 } 155 } 156 } 157 gen_comment_pad_tests!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20); 158 159 // Quoted Empty Padding 160 macro_rules! gen_quoted_pad_tests { 161 ($($n:literal),*) => { 162 paste::paste! { 163 $( 164 #[test] 165 fn [<test_quoted_pad_ $n>]() { 166 let pad = " ".repeat($n); 167 let input = format!("K=\"{}\"", pad); 168 assert_pair(&input, "K", &pad); 169 } 170 )* 171 } 172 } 173 } 174 gen_quoted_pad_tests!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19); 175 176 // Mixed case keys 177 macro_rules! tests_for_mixed_case { 178 ($($char:ident),*) => { 179 paste::paste! { 180 $( 181 #[test] 182 #[allow(non_snake_case)] 183 fn [<test_mixed_key_ $char>]() { 184 let c = stringify!($char); 185 let input = format!("A{}z=v", c); // A<char>z 186 assert_pair(&input, &format!("A{}z", c), "v"); 187 } 188 )* 189 } 190 } 191 } 192 tests_for_mixed_case!(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);