Skip to content

Commit

Permalink
Issue #615 and #243, GrammarTest now list CodeGen diagnostics.
Browse files Browse the repository at this point in the history
Added an error counter for these Diagnostics.
  • Loading branch information
smedilol committed Aug 25, 2017
1 parent f70e7c0 commit 4746e7e
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions TypeCobol.Test/GrammarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public static void CheckTests(string rootFolder, string resultFolder, string res
TimeSpan codeGenSumDuration = new TimeSpan(0);
int parseErrors = 0;
int codegenErrors = 0;
int codegenDiff = 0;
foreach (var file in files) {

string filename = Path.GetFileName(file);
Expand Down Expand Up @@ -117,51 +118,70 @@ public static void CheckTests(string rootFolder, string resultFolder, string res

writer.Close();

var expected = AsLines(File.ReadAllText(path, format.Encoding));
var actual = AsLines(writer.ToString());

Directory.CreateDirectory(resultFolder);


var linesKO = new List<int>();
for (int i = 0; i < Math.Min(expected.Count, actual.Count); i++) {
if (!expected[i].Equals(actual[i])) linesKO.Add(i);
}
var errors = new System.Text.StringBuilder();
string fmt = Lines2FormatString(Math.Max(expected.Count, actual.Count));
if (linesKO.Count > 0 || expected.Count != actual.Count) {
errors.AppendLine("--- Lines mismatch ---");
File.WriteAllLines(
resultFolder + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + "-Expected" +
Path.GetExtension(file), expected);
File.WriteAllLines(
resultFolder + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + "-Actual" +
Path.GetExtension(file), actual);
//Error during generation, no need to check the content of generated Cobol
if (generator.Diagnostics != null && generator.Diagnostics.Count > 0) {

codegenErrors += generator.Diagnostics.Count;
displayAndWriteErrorsToGrammarResult(resultFile, generator.Diagnostics);
nbFilesInError++;
if (nbFilesInError >= stopAfterAsManyErrors) break;

} else {
//Compare generated Cobol with expected
var expected = AsLines(File.ReadAllText(path, format.Encoding));
var actual = AsLines(writer.ToString());

Directory.CreateDirectory(resultFolder);


var linesKO = new List<int>();
for (int i = 0; i < Math.Min(expected.Count, actual.Count); i++)
{
if (!expected[i].Equals(actual[i])) linesKO.Add(i);
}
var errors = new System.Text.StringBuilder();
string fmt = Lines2FormatString(Math.Max(expected.Count, actual.Count));
if (linesKO.Count > 0 || expected.Count != actual.Count)
{
errors.AppendLine("--- Lines mismatch ---");
File.WriteAllLines(
resultFolder + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + "-Expected" +
Path.GetExtension(file), expected);
File.WriteAllLines(
resultFolder + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(file) + "-Actual" +
Path.GetExtension(file), actual);
}
int start = -1;
for (int i = 0; i < linesKO.Count; i++)
{
int currentline = linesKO[i];
bool follows = i > 0 && linesKO[i - 1] == currentline - 1;
if (!follows)
{
start = currentline;
before(errors, expected, currentline, 3, fmt);
}
bool preceeds = i + 1 < linesKO.Count && linesKO[i + 1] == currentline + 1;
if (!preceeds)
{
diff(errors, expected, actual, start, currentline, fmt);
after(errors, expected, currentline, 3, fmt);
start = -1;
}
}
for (int i = actual.Count; i < expected.Count; i++)
errors.AppendLine(String.Format("-{0:" + fmt + "} {1}", i, expected[i]));
for (int i = expected.Count; i < actual.Count; i++)
errors.AppendLine(String.Format("+{0:" + fmt + "} {1}", i, actual[i]));
if (errors.Length > 0)
{
codegenDiff += linesKO.Count + Math.Abs(actual.Count - expected.Count);
File.AppendAllText(resultFile, errors.ToString());
if (okay) nbFilesInError++;
}
}
int start = -1;
for (int i = 0; i < linesKO.Count; i++) {
int currentline = linesKO[i];
bool follows = i > 0 && linesKO[i - 1] == currentline - 1;
if (!follows) {
start = currentline;
before(errors, expected, currentline, 3, fmt);
}
bool preceeds = i + 1 < linesKO.Count && linesKO[i + 1] == currentline + 1;
if (!preceeds) {
diff(errors, expected, actual, start, currentline, fmt);
after(errors, expected, currentline, 3, fmt);
start = -1;
}
}
for (int i = actual.Count; i < expected.Count; i++)
errors.AppendLine(String.Format("-{0:" + fmt + "} {1}", i, expected[i]));
for (int i = expected.Count; i < actual.Count; i++)
errors.AppendLine(String.Format("+{0:" + fmt + "} {1}", i, actual[i]));
if (errors.Length > 0) {
codegenErrors += linesKO.Count + Math.Abs(actual.Count - expected.Count);
File.AppendAllText(resultFile, errors.ToString());
if (okay) nbFilesInError++;
}


} else {
File.AppendAllText(resultFile, "\n");
}
Expand All @@ -172,6 +192,7 @@ public static void CheckTests(string rootFolder, string resultFolder, string res
string message = "Files tested=" + tested + "/" + files.Length + ", files in error=" + nbFilesInError + ", ignored=" + ignores + "\n";
if (parseErrors > 0) message += "Parsing errors: "+ parseErrors + '\n';
if (codegenErrors > 0) message += "Codegen errors: "+ codegenErrors + '\n';
if (codegenDiff > 0) message += "Codegen Diff: "+ codegenDiff + '\n';
message += "Parsing time: " + parsingTotalDurationFormatted;
if (codegen) {
string codeGenTotalDurationFormatted = string.Format("{0:00}m{1:00}s{2:000}ms", codeGenSumDuration.Minutes, codeGenSumDuration.Seconds, codeGenSumDuration.Milliseconds);
Expand Down

0 comments on commit 4746e7e

Please sign in to comment.