This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
/
AntlrGrammarComparatorTestHelper.java
124 lines (110 loc) · 5.17 KB
/
AntlrGrammarComparatorTestHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Copyright (c) 2015 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.generator.parser;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.parser.AntlrGrammarComparator;
import org.eclipse.xtext.xbase.lib.Exceptions;
/**
* @author Christian Schneider - Initial contribution and API
*/
@SuppressWarnings("all")
public class AntlrGrammarComparatorTestHelper {
public static class ComparisonError extends AssertionError {
private final int lineNoTestee;
private final int lineNoExpected;
public ComparisonError(final String msg) {
super(msg);
this.lineNoTestee = (-1);
this.lineNoExpected = (-1);
}
public ComparisonError(final int lineNoTestee, final int lineNoExpected, final String msg) {
super(msg);
this.lineNoTestee = lineNoTestee;
this.lineNoExpected = lineNoExpected;
}
}
private static class TestErrorHandler implements AntlrGrammarComparator.IErrorHandler {
@Override
public void handleMismatch(final String match, final String matchReference, final AntlrGrammarComparator.ErrorContext context) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Inputs differs at token ");
_builder.append(match);
_builder.append(" (line ");
int _lineNumber = context.getTestedGrammar().getLineNumber();
_builder.append(_lineNumber);
_builder.append("), expected token ");
_builder.append(matchReference);
_builder.append(" (line ");
int _lineNumber_1 = context.getReferenceGrammar().getLineNumber();
_builder.append(_lineNumber_1);
_builder.append(").");
_builder.newLineIfNotEmpty();
AntlrGrammarComparatorTestHelper.fail(context.getTestedGrammar().getLineNumber(), context.getReferenceGrammar().getLineNumber(), _builder.toString());
}
@Override
public void handleInvalidGeneratedGrammarFile(final AntlrGrammarComparator.ErrorContext context) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Noticed an unmatched character sequence in \'testee\' in/before line ");
int _lineNumber = context.getTestedGrammar().getLineNumber();
_builder.append(_lineNumber);
_builder.append(".");
_builder.newLineIfNotEmpty();
AntlrGrammarComparatorTestHelper.fail(_builder.toString());
}
@Override
public void handleInvalidReferenceGrammarFile(final AntlrGrammarComparator.ErrorContext context) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Noticed an unmatched character sequence in \'expected\' in/before line ");
int _lineNumber = context.getReferenceGrammar().getLineNumber();
_builder.append(_lineNumber);
_builder.append(".");
_builder.newLineIfNotEmpty();
AntlrGrammarComparatorTestHelper.fail(_builder.toString());
}
}
private final AntlrGrammarComparator comparator = new AntlrGrammarComparator();
private final AntlrGrammarComparatorTestHelper.TestErrorHandler errorHandler = new AntlrGrammarComparatorTestHelper.TestErrorHandler();
public AntlrGrammarComparator.ErrorContext compare(final CharSequence grammar, final CharSequence grammarReference) {
return this.comparator.compareGrammars(grammar, grammarReference, this.errorHandler);
}
/**
* This method delegates to 'compare(..., ...)' and expects a {@link ComparisonError} reporting a mismatch
* in the given line numbers. If so the error is dropped. If the error reports different line numbers
* the error is re-thrown indicating a failure in the test. If no error occurs in 'compare(..., ...)'
* this test is supposed to fail.
*/
public void compareAndExpectMismatchInLines(final String testee, final String expected, final int lineNoTestee, final int lineNoExpected) {
try {
this.compare(testee, expected);
} catch (final Throwable _t) {
if (_t instanceof AntlrGrammarComparatorTestHelper.ComparisonError) {
final AntlrGrammarComparatorTestHelper.ComparisonError e = (AntlrGrammarComparatorTestHelper.ComparisonError)_t;
if (((lineNoTestee == e.lineNoTestee) && (lineNoExpected == e.lineNoExpected))) {
return;
} else {
throw e;
}
} else {
throw Exceptions.sneakyThrow(_t);
}
}
StringConcatenation _builder = new StringConcatenation();
_builder.append("Expected mismatch in lines ");
_builder.append(lineNoTestee);
_builder.append("/");
_builder.append(lineNoExpected);
_builder.append(".");
AntlrGrammarComparatorTestHelper.fail(_builder.toString());
}
private static void fail(final String msg) {
throw new AntlrGrammarComparatorTestHelper.ComparisonError(msg);
}
private static void fail(final int lineNoTestee, final int lineNoExpected, final String msg) {
throw new AntlrGrammarComparatorTestHelper.ComparisonError(lineNoTestee, lineNoExpected, msg);
}
}