OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 import 'dart:async' show | |
6 Future; | |
7 | |
8 import 'package:expect/expect.dart'; | |
9 | |
10 import 'package:compiler/src/tokens/precedence_constants.dart' show | |
11 EOF_INFO, | |
12 STRING_INFO, | |
13 OPEN_CURLY_BRACKET_INFO, | |
14 CLOSE_CURLY_BRACKET_INFO; | |
15 | |
16 import 'package:compiler/src/tokens/token.dart' show | |
17 Token, | |
18 KeywordToken, | |
19 StringToken, | |
20 SymbolToken, | |
21 ErrorToken; | |
22 | |
23 import 'test.dart' show | |
24 Test; | |
25 | |
26 import 'package:servicec/scanner.dart' show | |
27 LF_INFO, | |
28 Scanner; | |
29 | |
30 import 'package:servicec/keyword.dart' show | |
31 Keyword; | |
32 | |
33 List<ScannerTest> SCANNER_TESTS = <ScannerTest>[ | |
34 new Success('empty_input', ''' | |
35 ''', | |
36 []), | |
37 new Success('empty_service', ''' | |
38 service EmptyService {} | |
39 ''', | |
40 <Token>[ | |
41 new KeywordToken(Keyword.keywords["service"], 0), | |
42 new StringToken.fromString(STRING_INFO, "EmptyService", 8), | |
43 new SymbolToken(OPEN_CURLY_BRACKET_INFO, 22), | |
44 new SymbolToken(CLOSE_CURLY_BRACKET_INFO, 23), | |
45 new SymbolToken(LF_INFO, 10) | |
46 ]), | |
47 | |
48 new Failure('unmatched_curly', ''' | |
49 service EmptyService { | |
50 ''') | |
51 ]; | |
52 | |
53 abstract class ScannerTest extends Test { | |
54 final String input; | |
55 ScannerTest(String name, this.input) | |
56 : super(name); | |
57 } | |
58 | |
59 class Success extends ScannerTest { | |
60 final List<Token> output; | |
61 | |
62 Success(String name, String input, this.output) | |
63 : super(name, input); | |
64 | |
65 Future perform() async { | |
66 foldScannerOutputTokens( | |
67 input, | |
68 (token, index, _) { | |
69 Expect.isTrue( | |
70 token.toString() == output[index].toString(), | |
71 "Expected $token at index $index to be ${output[index]}"); | |
72 }, | |
73 null); | |
74 } | |
75 } | |
76 | |
77 /// Scanning fails if the output contains [ErrorToken]s. | |
78 class Failure extends ScannerTest { | |
79 | |
80 Failure(String name, String input) | |
81 : super(name, input); | |
82 | |
83 Future perform() async { | |
84 foldScannerOutputTokens( | |
85 input, | |
86 (token, index, foundError) => foundError || token is ErrorToken, | |
87 false); | |
88 } | |
89 } | |
90 | |
91 typedef dynamic TokenReduction(Token token, int index, dynamic accumulated); | |
92 | |
93 dynamic foldScannerOutputTokens( | |
94 String input, | |
95 TokenReduction reduce, | |
96 dynamic identity) { | |
97 var scanner = new Scanner(input); | |
98 Token tokenLinkedList = scanner.tokenize(); | |
99 | |
100 int index = 0; | |
101 while (tokenLinkedList.info != EOF_INFO) { | |
102 identity = reduce(tokenLinkedList, index++, identity); | |
103 tokenLinkedList = tokenLinkedList.next; | |
104 } | |
105 return identity; | |
106 } | |
OLD | NEW |