preprocessor.spec.ts (3531B)
1 export const description = ` 2 Test for "pp" preprocessor. 3 `; 4 5 import { makeTestGroup } from '../common/framework/test_group.js'; 6 import { pp } from '../common/util/preprocessor.js'; 7 8 import { UnitTest } from './unit_test.js'; 9 10 class F extends UnitTest { 11 test(act: string, exp: string): void { 12 this.expect(act === exp, 'got: ' + act.replace('\n', '⏎')); 13 } 14 } 15 16 export const g = makeTestGroup(F); 17 18 g.test('empty').fn(t => { 19 t.test(pp``, ''); 20 t.test(pp`\n`, '\n'); 21 t.test(pp`\n\n`, '\n\n'); 22 }); 23 24 g.test('plain').fn(t => { 25 t.test(pp`a`, 'a'); 26 t.test(pp`\na`, '\na'); 27 t.test(pp`\n\na`, '\n\na'); 28 t.test(pp`\na\n`, '\na\n'); 29 t.test(pp`a\n\n`, 'a\n\n'); 30 }); 31 32 g.test('substitutions,1').fn(t => { 33 const act = pp`a ${3} b`; 34 const exp = 'a 3 b'; 35 t.test(act, exp); 36 }); 37 38 g.test('substitutions,2').fn(t => { 39 const act = pp`a ${'x'}`; 40 const exp = 'a x'; 41 t.test(act, exp); 42 }); 43 44 g.test('substitutions,3').fn(t => { 45 const act = pp`a ${'x'} b`; 46 const exp = 'a x b'; 47 t.test(act, exp); 48 }); 49 50 g.test('substitutions,4').fn(t => { 51 const act = pp` 52 a 53 ${pp._if(false)} 54 ${'x'} 55 ${pp._endif} 56 b`; 57 const exp = '\na\n\nb'; 58 t.test(act, exp); 59 }); 60 61 g.test('if,true').fn(t => { 62 const act = pp` 63 a 64 ${pp._if(true)}c${pp._endif} 65 d 66 `; 67 const exp = '\na\nc\nd\n'; 68 t.test(act, exp); 69 }); 70 71 g.test('if,false').fn(t => { 72 const act = pp` 73 a 74 ${pp._if(false)}c${pp._endif} 75 d 76 `; 77 const exp = '\na\n\nd\n'; 78 t.test(act, exp); 79 }); 80 81 g.test('else,1').fn(t => { 82 const act = pp` 83 a 84 ${pp._if(true)} 85 b 86 ${pp._else} 87 c 88 ${pp._endif} 89 d 90 `; 91 const exp = '\na\n\nb\n\nd\n'; 92 t.test(act, exp); 93 }); 94 95 g.test('else,2').fn(t => { 96 const act = pp` 97 a 98 ${pp._if(false)} 99 b 100 ${pp._else} 101 c 102 ${pp._endif} 103 d 104 `; 105 const exp = '\na\n\nc\n\nd\n'; 106 t.test(act, exp); 107 }); 108 109 g.test('elif,1').fn(t => { 110 const act = pp` 111 a 112 ${pp._if(false)} 113 b 114 ${pp._elif(true)} 115 e 116 ${pp._else} 117 c 118 ${pp._endif} 119 d 120 `; 121 const exp = '\na\n\ne\n\nd\n'; 122 t.test(act, exp); 123 }); 124 125 g.test('elif,2').fn(t => { 126 const act = pp` 127 a 128 ${pp._if(true)} 129 b 130 ${pp._elif(true)} 131 e 132 ${pp._else} 133 c 134 ${pp._endif} 135 d 136 `; 137 const exp = '\na\n\nb\n\nd\n'; 138 t.test(act, exp); 139 }); 140 141 g.test('nested,1').fn(t => { 142 const act = pp` 143 a 144 ${pp._if(false)} 145 b 146 ${pp.__if(true)} 147 e 148 ${pp.__endif} 149 c 150 ${pp._endif} 151 d 152 `; 153 const exp = '\na\n\nd\n'; 154 t.test(act, exp); 155 }); 156 157 g.test('nested,2').fn(t => { 158 const act = pp` 159 a 160 ${pp._if(false)} 161 b 162 ${pp._else} 163 h 164 ${pp.__if(false)} 165 e 166 ${pp.__elif(true)} 167 f 168 ${pp.__else} 169 g 170 ${pp.__endif} 171 c 172 ${pp._endif} 173 d 174 `; 175 const exp = '\na\n\nh\n\nf\n\nc\n\nd\n'; 176 t.test(act, exp); 177 }); 178 179 g.test('errors,pass').fn(() => { 180 pp`${pp._if(true)}${pp._endif}`; 181 pp`${pp._if(true)}${pp._else}${pp._endif}`; 182 pp`${pp._if(true)}${pp.__if(true)}${pp.__endif}${pp._endif}`; 183 }); 184 185 g.test('errors,fail').fn(t => { 186 const e = (fn: () => void) => t.shouldThrow('Error', fn); 187 e(() => pp`${pp._if(true)}`); 188 e(() => pp`${pp._elif(true)}`); 189 e(() => pp`${pp._else}`); 190 e(() => pp`${pp._endif}`); 191 e(() => pp`${pp.__if(true)}`); 192 e(() => pp`${pp.__elif(true)}`); 193 e(() => pp`${pp.__else}`); 194 e(() => pp`${pp.__endif}`); 195 196 e(() => pp`${pp._if(true)}${pp._elif(true)}`); 197 e(() => pp`${pp._if(true)}${pp._elif(true)}${pp._else}`); 198 e(() => pp`${pp._if(true)}${pp._else}`); 199 e(() => pp`${pp._else}${pp._endif}`); 200 201 e(() => pp`${pp._if(true)}${pp.__endif}`); 202 e(() => pp`${pp.__if(true)}${pp.__endif}`); 203 e(() => pp`${pp.__if(true)}${pp._endif}`); 204 205 e(() => pp`${pp._if(true)}${pp._else}${pp._else}${pp._endif}`); 206 e(() => pp`${pp._if(true)}${pp.__if(true)}${pp.__else}${pp.__else}${pp.__endif}${pp._endif}`); 207 });