OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library pub.test.preprocess_test; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 |
| 9 import '../lib/src/preprocess.dart'; |
| 10 import '../lib/src/version.dart'; |
| 11 import 'test_pub.dart'; |
| 12 |
| 13 main() { |
| 14 initConfig(); |
| 15 |
| 16 test("does nothing on a file without preprocessor directives", () { |
| 17 var text = ''' |
| 18 some text |
| 19 // normal comment |
| 20 // # |
| 21 //# not beginning of line |
| 22 '''; |
| 23 |
| 24 expect(_preprocess(text), equals(text)); |
| 25 }); |
| 26 |
| 27 test("allows bare insert directive", () { |
| 28 expect(_preprocess('//> foo'), equals('foo')); |
| 29 }); |
| 30 |
| 31 test("allows empty insert directive", () { |
| 32 expect(_preprocess(''' |
| 33 //> foo |
| 34 //> |
| 35 //> bar |
| 36 '''), equals('foo\n\nbar\n')); |
| 37 }); |
| 38 |
| 39 group("if", () { |
| 40 test("removes sections with non-matching versions", () { |
| 41 expect(_preprocess(''' |
| 42 before |
| 43 //# if barback <1.0.0 |
| 44 inside |
| 45 //# end |
| 46 after |
| 47 '''), equals(''' |
| 48 before |
| 49 after |
| 50 ''')); |
| 51 }); |
| 52 |
| 53 test("doesn't insert section with non-matching versions", () { |
| 54 expect(_preprocess(''' |
| 55 before |
| 56 //# if barback <1.0.0 |
| 57 //> inside |
| 58 //# end |
| 59 after |
| 60 '''), equals(''' |
| 61 before |
| 62 after |
| 63 ''')); |
| 64 }); |
| 65 |
| 66 test("doesn't remove sections with matching versions", () { |
| 67 expect(_preprocess(''' |
| 68 before |
| 69 //# if barback >1.0.0 |
| 70 inside |
| 71 //# end |
| 72 after |
| 73 '''), equals(''' |
| 74 before |
| 75 inside |
| 76 after |
| 77 ''')); |
| 78 }); |
| 79 |
| 80 test("inserts sections with matching versions", () { |
| 81 expect(_preprocess(''' |
| 82 before |
| 83 //# if barback >1.0.0 |
| 84 //> inside |
| 85 //# end |
| 86 after |
| 87 '''), equals(''' |
| 88 before |
| 89 inside |
| 90 after |
| 91 ''')); |
| 92 }); |
| 93 |
| 94 test("allows version ranges", () { |
| 95 expect(_preprocess(''' |
| 96 before |
| 97 //# if barback >=1.0.0 <2.0.0 |
| 98 inside 1 |
| 99 //# end |
| 100 //# if barback >=0.9.0 <1.0.0 |
| 101 inside 2 |
| 102 //# end |
| 103 after |
| 104 '''), equals(''' |
| 105 before |
| 106 inside 1 |
| 107 after |
| 108 ''')); |
| 109 }); |
| 110 }); |
| 111 |
| 112 group("else", () { |
| 113 test("removes non-matching sections", () { |
| 114 expect(_preprocess(''' |
| 115 before |
| 116 //# if barback >1.0.0 |
| 117 inside 1 |
| 118 //# else |
| 119 inside 2 |
| 120 //# end |
| 121 after |
| 122 '''), equals(''' |
| 123 before |
| 124 inside 1 |
| 125 after |
| 126 ''')); |
| 127 }); |
| 128 |
| 129 test("doesn't insert non-matching sections", () { |
| 130 expect(_preprocess(''' |
| 131 before |
| 132 //# if barback >1.0.0 |
| 133 inside 1 |
| 134 //# else |
| 135 //> inside 2 |
| 136 //# end |
| 137 after |
| 138 '''), equals(''' |
| 139 before |
| 140 inside 1 |
| 141 after |
| 142 ''')); |
| 143 }); |
| 144 |
| 145 test("doesn't remove matching sections", () { |
| 146 expect(_preprocess(''' |
| 147 before |
| 148 //# if barback <1.0.0 |
| 149 inside 1 |
| 150 //# else |
| 151 inside 2 |
| 152 //# end |
| 153 after |
| 154 '''), equals(''' |
| 155 before |
| 156 inside 2 |
| 157 after |
| 158 ''')); |
| 159 }); |
| 160 |
| 161 test("inserts matching sections", () { |
| 162 expect(_preprocess(''' |
| 163 before |
| 164 //# if barback <1.0.0 |
| 165 inside 1 |
| 166 //# else |
| 167 //> inside 2 |
| 168 //# end |
| 169 after |
| 170 '''), equals(''' |
| 171 before |
| 172 inside 2 |
| 173 after |
| 174 ''')); |
| 175 }); |
| 176 }); |
| 177 |
| 178 group("errors", () { |
| 179 test("disallows unknown statements", () { |
| 180 expect(() => _preprocess('//# foo bar\n//# end'), throwsFormatException); |
| 181 }); |
| 182 |
| 183 test("disallows insert directive without space", () { |
| 184 expect(() => _preprocess('//>foo'), throwsFormatException); |
| 185 }); |
| 186 |
| 187 group("if", () { |
| 188 test("disallows if with no arguments", () { |
| 189 expect(() => _preprocess('//# if\n//# end'), throwsFormatException); |
| 190 }); |
| 191 |
| 192 test("disallows if with no version range", () { |
| 193 expect(() => _preprocess('//# if barback\n//# end'), |
| 194 throwsFormatException); |
| 195 }); |
| 196 |
| 197 test("disallows if with no package", () { |
| 198 expect(() => _preprocess('//# if <=1.0.0\n//# end'), |
| 199 throwsFormatException); |
| 200 }); |
| 201 |
| 202 test("disallows unknown package name", () { |
| 203 expect(() => _preprocess('//# if polymer <=1.0.0\n//# end'), |
| 204 throwsFormatException); |
| 205 }); |
| 206 |
| 207 test("disallows invalid version constraint", () { |
| 208 expect(() => _preprocess('//# if barback >=1.0\n//# end'), |
| 209 throwsFormatException); |
| 210 }); |
| 211 |
| 212 test("disallows dangling end", () { |
| 213 expect(() => _preprocess('//# end'), |
| 214 throwsFormatException); |
| 215 }); |
| 216 |
| 217 test("disallows if without end", () { |
| 218 expect(() => _preprocess('//# if barback >=1.0.0'), |
| 219 throwsFormatException); |
| 220 }); |
| 221 |
| 222 test("disallows nested if", () { |
| 223 expect(() => _preprocess(''' |
| 224 //# if barback >=1.0.0 |
| 225 //# if barback >= 1.5.0 |
| 226 //# end |
| 227 //# end |
| 228 '''), |
| 229 throwsFormatException); |
| 230 }); |
| 231 }); |
| 232 |
| 233 group("else", () { |
| 234 test("disallows else without if", () { |
| 235 expect(() => _preprocess('//# else\n//# end'), throwsFormatException); |
| 236 }); |
| 237 |
| 238 test("disallows else without end", () { |
| 239 expect(() => _preprocess('//# if barback >=1.0.0\n//# else'), |
| 240 throwsFormatException); |
| 241 }); |
| 242 |
| 243 test("disallows else with an argument", () { |
| 244 expect(() => _preprocess(''' |
| 245 //# if barback >=1.0.0 |
| 246 //# else barback <0.5.0 |
| 247 //# end |
| 248 '''), throwsFormatException); |
| 249 }); |
| 250 }); |
| 251 }); |
| 252 } |
| 253 |
| 254 String _preprocess(String input) => |
| 255 preprocess(input, new Version.parse("1.2.3"), 'source/url'); |
OLD | NEW |