|
1 | | -using System; |
2 | | -using System.IO; |
3 | 1 | using ServerCodeExcision; |
4 | 2 | using ServerCodeExcisionCommon; |
| 3 | +using Spectre.Console; |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using UnrealAngelscriptServerCodeExcision; |
5 | 7 |
|
6 | 8 | public class IntegrationTests |
7 | 9 | { |
8 | | - private static string TestProblemPath = @"Problems"; |
9 | | - //private static string TestProblemPath = @"ProblemTestBed"; |
10 | | - private static string TestAnswerPath = @"Answers"; |
11 | | - private static string TestSolutionPath = @"Solutions"; |
12 | | - |
13 | | - private static string CommonSubPath = @"Common"; |
14 | | - private static string AngelscriptSubPath = @"Angelscript"; |
15 | | - |
16 | | - public static void Main(string[] args) |
17 | | - { |
18 | | - ConsoleColor initialColor = Console.ForegroundColor; |
19 | | - bool excisionWasSuccessful = true; |
20 | | - int nrCorrectAnswers = 0; |
21 | | - int nrErrors = 0; |
22 | | - int nrProblems = 0; |
23 | | - |
24 | | - var commonProblemPath = Path.Combine(TestProblemPath, CommonSubPath); |
25 | | - var commonSolutionPath = Path.Combine(TestSolutionPath, CommonSubPath); |
26 | | - |
27 | | - // Run for Angelscript |
28 | | - excisionWasSuccessful = excisionWasSuccessful && RunExciserIntegrationTests("as", |
29 | | - Path.Combine(TestProblemPath, AngelscriptSubPath), |
30 | | - Path.Combine(TestAnswerPath, AngelscriptSubPath), |
31 | | - Path.Combine(TestSolutionPath, AngelscriptSubPath), |
32 | | - commonProblemPath, |
33 | | - commonSolutionPath, |
34 | | - ref nrCorrectAnswers, ref nrErrors, ref nrProblems); |
35 | | - |
36 | | - if (excisionWasSuccessful) |
37 | | - { |
38 | | - Console.ForegroundColor = initialColor; |
39 | | - Console.WriteLine("----------------------------"); |
40 | | - Console.WriteLine(nrCorrectAnswers > 0 && nrCorrectAnswers == nrProblems |
41 | | - ? string.Format("{0} test(s) ran successfully.", nrCorrectAnswers) |
42 | | - : string.Format("{0} error(s) detected running {1} tests", nrErrors, nrProblems)); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - private static bool RunExciserIntegrationTests(string fileExtension, string testProblemPath, string testAnswerPath, string testSolutionPath, string commonProblemPath, string commonSolutionPath, ref int nrCorrectAnswers, ref int nrErrors, ref int nrProblems) |
47 | | - { |
48 | | - string problemPath = Path.Combine(Environment.CurrentDirectory, testProblemPath); |
49 | | - string answerPath = Path.Combine(Environment.CurrentDirectory, testAnswerPath); |
50 | | - string solutionPath = Path.Combine(Environment.CurrentDirectory, testSolutionPath); |
51 | | - |
52 | | - // First copy common problems to their language folders and rename them so they are picked up by the exciser if they exist. |
53 | | - if (Directory.Exists(commonProblemPath) && Directory.Exists(commonSolutionPath)) |
54 | | - { |
55 | | - CopyCommonTestFiles(fileExtension, problemPath, commonProblemPath); |
56 | | - CopyCommonTestFiles(fileExtension, solutionPath, commonSolutionPath); |
57 | | - } |
58 | | - |
59 | | - // Clean up earlier answers. |
60 | | - if (Directory.Exists(answerPath)) |
61 | | - { |
62 | | - Directory.Delete(answerPath, true); |
63 | | - } |
64 | | - |
65 | | - string[] exciserArgs = |
66 | | - { |
67 | | - problemPath, |
68 | | - "-u", |
69 | | - "-f", |
70 | | - "FullExcise1/.*", |
71 | | - "-a", |
72 | | - "AllFunctionExcise1/.*|||AllFunctionExcise2/.*", |
73 | | - "-o", |
74 | | - answerPath |
75 | | - }; |
76 | | - |
77 | | - var excisionReturnCode = (EExciserReturnValues)ServerCodeExciser.Main(exciserArgs); |
78 | | - if (excisionReturnCode != EExciserReturnValues.Success) |
79 | | - { |
80 | | - Console.Error.WriteLine("Excision error: " + excisionReturnCode); |
81 | | - return false; |
82 | | - } |
83 | | - |
84 | | - if (Directory.Exists(answerPath)) |
85 | | - { |
86 | | - foreach (var answerFilePath in Directory.EnumerateFiles(answerPath, "*." + fileExtension, SearchOption.AllDirectories)) |
87 | | - { |
88 | | - nrProblems++; |
89 | | - |
90 | | - var relativePath = Path.GetRelativePath(answerPath, answerFilePath); |
91 | | - var solutionFilePath = Path.Combine(solutionPath, relativePath); |
92 | | - var fileName = Path.GetFileName(answerFilePath); |
93 | | - |
94 | | - var answer = File.ReadAllText(answerFilePath); |
95 | | - var solution = File.ReadAllText(solutionFilePath); |
96 | | - |
97 | | - if(answer == solution) |
98 | | - { |
99 | | - Console.ForegroundColor = ConsoleColor.Green; |
100 | | - Console.WriteLine(fileName + "'s answer matched the correct solution!"); |
101 | | - nrCorrectAnswers++; |
102 | | - } |
103 | | - else |
104 | | - { |
105 | | - Console.ForegroundColor = ConsoleColor.Red; |
106 | | - Console.Error.WriteLine(fileName + "'s failed!"); |
107 | | - nrErrors++; |
108 | | - } |
109 | | - } |
110 | | - } |
111 | | - else |
112 | | - { |
113 | | - Console.Error.WriteLine("No test answers found in path: " + answerPath); |
114 | | - return false; |
115 | | - } |
116 | | - |
117 | | - // Clean up common folders if it went well |
118 | | - CleanupTestFiles(problemPath); |
119 | | - CleanupTestFiles(solutionPath); |
120 | | - |
121 | | - return true; |
122 | | - } |
123 | | - |
124 | | - private static void CopyCommonTestFiles(string fileExtension, string targetRootPath, string commonRootPath) |
125 | | - { |
126 | | - // First clear target problem path |
127 | | - CleanupTestFiles(targetRootPath); |
128 | | - |
129 | | - var targetCommonPath = Path.Combine(targetRootPath, "Common"); |
130 | | - foreach (var commonProblemFilePath in Directory.EnumerateFiles(commonRootPath, "*.*", SearchOption.AllDirectories)) |
131 | | - { |
132 | | - var targetPath = Path.Combine(targetCommonPath, Path.GetRelativePath(commonRootPath, Path.ChangeExtension(commonProblemFilePath, fileExtension))); |
133 | | - var targetDirectory = Path.GetDirectoryName(targetPath); |
134 | | - if (targetDirectory != null) |
135 | | - { |
136 | | - Directory.CreateDirectory(targetDirectory); |
137 | | - File.Copy(commonProblemFilePath, targetPath); |
138 | | - } |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - private static void CleanupTestFiles(string targetRootPath) |
143 | | - { |
144 | | - var targetCommonPath = Path.Combine(targetRootPath, "Common"); |
145 | | - if (Directory.Exists(targetCommonPath)) |
146 | | - { |
147 | | - Directory.Delete(targetCommonPath, true); |
148 | | - } |
| 10 | + private static string TestProblemPath = @"Problems"; |
| 11 | + private static string TestAnswerPath = @"Answers"; |
| 12 | + |
| 13 | + public static int Main(string[] args) |
| 14 | + { |
| 15 | + int numTestFailures = 0; |
| 16 | + int numTestCases = 0; |
| 17 | + |
| 18 | + try |
| 19 | + { |
| 20 | + // Run for Angelscript |
| 21 | + var angelscriptResult = RunExciserIntegrationTests( |
| 22 | + ".as", |
| 23 | + Path.Combine(Environment.CurrentDirectory, TestProblemPath, "Angelscript"), |
| 24 | + Path.Combine(Environment.CurrentDirectory, TestAnswerPath, "Angelscript"), |
| 25 | + ref numTestFailures, |
| 26 | + ref numTestCases); |
| 27 | + |
| 28 | + // Run for "common" |
| 29 | + var commonResult = RunExciserIntegrationTests( |
| 30 | + ".common", |
| 31 | + Path.Combine(Environment.CurrentDirectory, TestProblemPath, "Common"), |
| 32 | + Path.Combine(Environment.CurrentDirectory, TestAnswerPath, "Common"), |
| 33 | + ref numTestFailures, |
| 34 | + ref numTestCases); |
| 35 | + |
| 36 | + Console.WriteLine("----------------------------"); |
| 37 | + Console.WriteLine($"{numTestCases - numTestFailures} test(s) passed."); |
| 38 | + Console.WriteLine($"{numTestFailures} test(s) failed."); |
| 39 | + } |
| 40 | + catch (Exception e) |
| 41 | + { |
| 42 | + AnsiConsole.WriteException(e); |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + return numTestFailures == 0 ? 0 : 1; |
| 47 | + } |
| 48 | + |
| 49 | + private static EExciserReturnValues RunExciserIntegrationTests(string fileExtension, string inputPath, string outputPath, ref int numTestFailures, ref int numTestCases) |
| 50 | + { |
| 51 | + // Clean up earlier answers. |
| 52 | + if (Directory.Exists(outputPath)) |
| 53 | + { |
| 54 | + Directory.Delete(outputPath, true); |
| 55 | + } |
| 56 | + |
| 57 | + string searchPattern = "*" + fileExtension.TrimStart('.'); |
| 58 | + |
| 59 | + EExciserReturnValues returnCode; |
| 60 | + try |
| 61 | + { |
| 62 | + var excisionParams = new ServerCodeExcisionParameters |
| 63 | + { |
| 64 | + OutputPath = outputPath, |
| 65 | + ShouldOutputUntouchedFiles = true, |
| 66 | + FullExcisionRegexString = @"FullExcise1/.*", |
| 67 | + ExciseAllFunctionsRegexString = @"AllFunctionExcise1/.*|||AllFunctionExcise2/.*", |
| 68 | + }; |
| 69 | + excisionParams.InputPaths.Add(inputPath); |
| 70 | + |
| 71 | + var angelscriptServerCodeExciser = new ServerCodeExcisionProcessor(excisionParams); |
| 72 | + returnCode = angelscriptServerCodeExciser.ExciseServerCode(searchPattern, new UnrealAngelscriptServerCodeExcisionLanguage()); |
| 73 | + Console.WriteLine($"ExciseServerCode for {fileExtension} files returned: {returnCode}"); |
| 74 | + } |
| 75 | + catch (Exception e) |
| 76 | + { |
| 77 | + AnsiConsole.WriteException(e); |
| 78 | + return EExciserReturnValues.InternalExcisionError; |
| 79 | + } |
| 80 | + |
| 81 | + foreach (var answerFilePath in Directory.EnumerateFiles(outputPath, searchPattern, SearchOption.AllDirectories)) |
| 82 | + { |
| 83 | + numTestCases++; |
| 84 | + |
| 85 | + var solutionFilePath = Path.Combine(inputPath, Path.GetRelativePath(outputPath, answerFilePath)) + ".solution"; |
| 86 | + |
| 87 | + var fileName = Path.GetFileName(answerFilePath); |
| 88 | + var answer = File.ReadAllText(answerFilePath); |
| 89 | + var solution = File.ReadAllText(solutionFilePath); |
| 90 | + |
| 91 | + if (answer == solution) |
| 92 | + { |
| 93 | + Console.ForegroundColor = ConsoleColor.Green; |
| 94 | + Console.WriteLine(fileName + " passed!"); |
| 95 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + Console.ForegroundColor = ConsoleColor.Red; |
| 100 | + Console.Error.WriteLine(fileName + " failed!"); |
| 101 | + Console.ForegroundColor = ConsoleColor.Gray; |
| 102 | + Console.WriteLine("--- Expected: ---"); |
| 103 | + Console.WriteLine(solution); |
| 104 | + Console.WriteLine("--- Actual: ---"); |
| 105 | + Console.WriteLine(answer); |
| 106 | + numTestFailures++; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return returnCode; |
149 | 111 | } |
150 | 112 | } |
0 commit comments