| 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 library dart2js.test.diagnostic_helper; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:compiler/compiler_new.dart' show |
| 10 CompilerDiagnostics, |
| 11 Diagnostic; |
| 12 import 'package:compiler/src/diagnostics/messages.dart' show |
| 13 Message, |
| 14 MessageKind; |
| 15 import 'package:expect/expect.dart'; |
| 16 |
| 17 class CollectedMessage { |
| 18 final Message message; |
| 19 final Uri uri; |
| 20 final int begin; |
| 21 final int end; |
| 22 final String text; |
| 23 final Diagnostic kind; |
| 24 |
| 25 CollectedMessage( |
| 26 this.message, this.uri, this.begin, this.end, this.text, this.kind); |
| 27 |
| 28 MessageKind get messageKind => message.kind; |
| 29 |
| 30 String toString() => '$uri:$begin:$end:$text:$kind'; |
| 31 } |
| 32 |
| 33 class DiagnosticCollector implements CompilerDiagnostics { |
| 34 List<CollectedMessage> messages = <CollectedMessage>[]; |
| 35 |
| 36 void call(Uri uri, int begin, int end, String message, Diagnostic kind) { |
| 37 throw ''; |
| 38 report(null, uri, begin, end, message, kind); |
| 39 } |
| 40 |
| 41 @override |
| 42 void report(Message message, |
| 43 Uri uri, int begin, int end, String text, Diagnostic kind) { |
| 44 messages.add(new CollectedMessage(message, uri, begin, end, text, kind)); |
| 45 } |
| 46 |
| 47 Iterable<CollectedMessage> filterMessagesByKinds(List<Diagnostic> kinds) { |
| 48 return messages.where( |
| 49 (CollectedMessage message) => kinds.contains(message.kind)); |
| 50 } |
| 51 |
| 52 Iterable<CollectedMessage> get errors { |
| 53 return filterMessagesByKinds([Diagnostic.ERROR]); |
| 54 } |
| 55 |
| 56 Iterable<CollectedMessage> get warnings { |
| 57 return filterMessagesByKinds([Diagnostic.WARNING]); |
| 58 } |
| 59 |
| 60 Iterable<CollectedMessage> get hints { |
| 61 return filterMessagesByKinds([Diagnostic.HINT]); |
| 62 } |
| 63 |
| 64 Iterable<CollectedMessage> get infos { |
| 65 return filterMessagesByKinds([Diagnostic.INFO]); |
| 66 } |
| 67 |
| 68 Iterable<CollectedMessage> get crashes { |
| 69 return filterMessagesByKinds([Diagnostic.CRASH]); |
| 70 } |
| 71 |
| 72 /// `true` if non-verbose messages has been collected. |
| 73 bool get hasRegularMessages { |
| 74 return messages.any((m) => m.kind != Diagnostic.VERBOSE_INFO); |
| 75 } |
| 76 |
| 77 void clear() { |
| 78 messages.clear(); |
| 79 } |
| 80 } |
| 81 |
| 82 |
| 83 void compareWarningKinds(String text, |
| 84 List expectedWarnings, |
| 85 Iterable<CollectedMessage> foundWarnings) { |
| 86 compareMessageKinds(text, expectedWarnings, foundWarnings, 'warning'); |
| 87 } |
| 88 |
| 89 /// [expectedMessages] must be a list of either [MessageKind] or [CheckMessage]. |
| 90 void compareMessageKinds(String text, |
| 91 List expectedMessages, |
| 92 Iterable<CollectedMessage> foundMessages, |
| 93 String kind) { |
| 94 var fail = (message) => Expect.fail('$text: $message'); |
| 95 HasNextIterator expectedIterator = |
| 96 new HasNextIterator(expectedMessages.iterator); |
| 97 HasNextIterator<CollectedMessage> foundIterator = |
| 98 new HasNextIterator(foundMessages.iterator); |
| 99 while (expectedIterator.hasNext && foundIterator.hasNext) { |
| 100 var expected = expectedIterator.next(); |
| 101 var found = foundIterator.next(); |
| 102 if (expected is MessageKind) { |
| 103 Expect.equals(expected, found.message.kind); |
| 104 } else if (expected is CheckMessage) { |
| 105 String error = expected(found.message); |
| 106 Expect.isNull(error, error); |
| 107 } else { |
| 108 Expect.fail("Unexpected $kind value: $expected."); |
| 109 } |
| 110 } |
| 111 if (expectedIterator.hasNext) { |
| 112 do { |
| 113 var expected = expectedIterator.next(); |
| 114 if (expected is CheckMessage) expected = expected(null); |
| 115 print('Expected $kind "${expected}" did not occur'); |
| 116 } while (expectedIterator.hasNext); |
| 117 fail('Too few ${kind}s'); |
| 118 } |
| 119 if (foundIterator.hasNext) { |
| 120 do { |
| 121 CollectedMessage message = foundIterator.next(); |
| 122 print('Additional $kind "${message}: ${message.message}"'); |
| 123 } while (foundIterator.hasNext); |
| 124 fail('Too many ${kind}s'); |
| 125 } |
| 126 } |
| 127 |
| 128 /// A function the checks [message]. If the check fails or if [message] is |
| 129 /// `null`, an error string is returned. Otherwise `null` is returned. |
| 130 typedef String CheckMessage(Message message); |
| 131 |
| 132 CheckMessage checkMessage(MessageKind kind, Map arguments) { |
| 133 return (Message message) { |
| 134 if (message == null) return '$kind'; |
| 135 if (message.kind != kind) return 'Expected message $kind, found $message.'; |
| 136 for (var key in arguments.keys) { |
| 137 if (!message.arguments.containsKey(key)) { |
| 138 return 'Expected argument $key not found in $message.kind.'; |
| 139 } |
| 140 String expectedValue = '${arguments[key]}'; |
| 141 String foundValue = '${message.arguments[key]}'; |
| 142 if (expectedValue != foundValue) { |
| 143 return 'Expected argument $key with value $expectedValue, ' |
| 144 'found $foundValue.'; |
| 145 } |
| 146 } |
| 147 return null; |
| 148 }; |
| 149 } |
| OLD | NEW |