| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, 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 'package:front_end/src/fasta/scanner/string_scanner.dart'; |
| 6 import 'package:front_end/src/fasta/scanner/token.dart' as fasta; |
| 7 import 'package:front_end/src/scanner/token.dart'; |
| 8 import 'package:front_end/src/scanner/reader.dart' as analyzer; |
| 9 import 'package:test/test.dart'; |
| 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 11 import 'scanner_roundtrip_test.dart' show TestScanner; |
| 12 |
| 13 main() { |
| 14 defineReflectiveSuite(() { |
| 15 defineReflectiveTests(TokenTest); |
| 16 }); |
| 17 } |
| 18 |
| 19 /// Assert that fasta PrecedenceInfo implements analyzer TokenType. |
| 20 @reflectiveTest |
| 21 class TokenTest { |
| 22 void test_applyDelta() { |
| 23 var scanner = new StringScanner('/* 1 */ foo', includeComments: true); |
| 24 var token = scanner.tokenize(); |
| 25 expect(token.offset, 8); |
| 26 expect(token.precedingComments.offset, 0); |
| 27 token.applyDelta(12); |
| 28 expect(token.offset, 20); |
| 29 expect(token.precedingComments.offset, 12); |
| 30 } |
| 31 |
| 32 void test_copy() { |
| 33 String source = '/* 1 */ /* 2 */ main() {print("hello"); return;}'; |
| 34 int commentCount = 0; |
| 35 |
| 36 void assertCopiedToken(Token token1, Token token2) { |
| 37 if (token1 == null) { |
| 38 expect(token2, isNull); |
| 39 return; |
| 40 } |
| 41 expect(token1.lexeme, token2.lexeme); |
| 42 expect(token1.offset, token2.offset, reason: token1.lexeme); |
| 43 var comment1 = token1.precedingComments; |
| 44 var comment2 = token2.precedingComments; |
| 45 while (comment1 != null) { |
| 46 ++commentCount; |
| 47 assertCopiedToken(comment1, comment2); |
| 48 comment1 = comment1.next; |
| 49 comment2 = comment2.next; |
| 50 } |
| 51 expect(comment2, isNull, reason: comment2?.lexeme); |
| 52 } |
| 53 |
| 54 var token1 = new StringScanner(source, includeComments: true).tokenize(); |
| 55 var analyzerScanner = |
| 56 new TestScanner(new analyzer.CharSequenceReader(source)); |
| 57 analyzerScanner.preserveComments = true; |
| 58 var token2 = analyzerScanner.tokenize(); |
| 59 |
| 60 bool stringTokenFound = false; |
| 61 bool keywordTokenFound = false; |
| 62 bool symbolTokenFound = false; |
| 63 bool beginGroupTokenFound = false; |
| 64 |
| 65 while (!token1.isEof) { |
| 66 if (token1 is fasta.StringToken) stringTokenFound = true; |
| 67 if (token1 is fasta.KeywordToken) keywordTokenFound = true; |
| 68 if (token1 is fasta.SymbolToken) symbolTokenFound = true; |
| 69 if (token1 is fasta.BeginGroupToken) beginGroupTokenFound = true; |
| 70 |
| 71 var copy1 = token1.copy(); |
| 72 expect(copy1, isNotNull); |
| 73 |
| 74 var copy2 = token2.copy(); |
| 75 expect(copy2, isNotNull); |
| 76 |
| 77 assertCopiedToken(copy1, copy2); |
| 78 |
| 79 token1 = token1.next; |
| 80 token2 = token2.next; |
| 81 } |
| 82 expect(token2.type, TokenType.EOF); |
| 83 |
| 84 expect(commentCount, 2); |
| 85 expect(stringTokenFound, isTrue); |
| 86 expect(keywordTokenFound, isTrue); |
| 87 expect(symbolTokenFound, isTrue); |
| 88 expect(beginGroupTokenFound, isTrue); |
| 89 } |
| 90 |
| 91 void test_isSynthetic() { |
| 92 var scanner = new StringScanner('/* 1 */ foo', includeComments: true); |
| 93 var token = scanner.tokenize(); |
| 94 expect(token.isSynthetic, false); |
| 95 expect(token.precedingComments.isSynthetic, false); |
| 96 expect(token.previous.isSynthetic, true); |
| 97 expect(token.next.isEof, true); |
| 98 expect(token.next.isSynthetic, true); |
| 99 } |
| 100 |
| 101 void test_matchesAny() { |
| 102 var scanner = new StringScanner('true', includeComments: true); |
| 103 var token = scanner.tokenize(); |
| 104 expect(token.matchesAny([TokenType.KEYWORD]), true); |
| 105 expect(token.matchesAny([TokenType.AMPERSAND, TokenType.KEYWORD]), true); |
| 106 expect(token.matchesAny([TokenType.AMPERSAND]), false); |
| 107 } |
| 108 |
| 109 void test_value() { |
| 110 var scanner = new StringScanner('true & "home"', includeComments: true); |
| 111 var token = scanner.tokenize(); |
| 112 // Keywords |
| 113 expect(token.lexeme, 'true'); |
| 114 expect(token.value(), Keyword.TRUE); |
| 115 // General tokens |
| 116 token = token.next; |
| 117 expect(token.lexeme, '&'); |
| 118 expect(token.value(), '&'); |
| 119 // String tokens |
| 120 token = token.next; |
| 121 print('String token :: ${token.runtimeType}'); |
| 122 expect(token.lexeme, '"home"'); |
| 123 expect(token.value(), '"home"'); |
| 124 } |
| 125 } |
| OLD | NEW |