|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using Antlr4.Runtime; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using UnrealAngelscriptServerCodeExcision; |
| 6 | + |
| 7 | +namespace ServerCodeExciser.Tests |
| 8 | +{ |
| 9 | + [TestClass] |
| 10 | + public class AngelscriptSyntaxTests |
| 11 | + { |
| 12 | + private sealed class ThrowingErrorListener : BaseErrorListener |
| 13 | + { |
| 14 | + public override void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e) |
| 15 | + { |
| 16 | + throw new InvalidOperationException($"line {line}:{charPositionInLine} {msg}"); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + [TestMethod] |
| 21 | + [DataRow("mynamespace")] |
| 22 | + [DataRow("Nested::mynamespace")] |
| 23 | + public void Namespace(string @namespace) |
| 24 | + { |
| 25 | + var script = $"namespace {@namespace}\r\n{{\r\n}}\r\n"; |
| 26 | + var lexer = new UnrealAngelscriptLexer(new AntlrInputStream(script)); |
| 27 | + var tokenStream = new CommonTokenStream(lexer); |
| 28 | + var parser = new UnrealAngelscriptParser(tokenStream); |
| 29 | + parser.RemoveErrorListeners(); |
| 30 | + parser.AddErrorListener(new ThrowingErrorListener()); |
| 31 | + new UnrealAngelscriptSimpleVisitor(script).VisitChildren(parser.script()); |
| 32 | + } |
| 33 | + |
| 34 | + [TestMethod] |
| 35 | + [DataRow("")] |
| 36 | + [DataRow("final")] |
| 37 | + [DataRow("override")] |
| 38 | + [DataRow("no_discard")] // UnrealAngelscript |
| 39 | + [DataRow("allow_discard")] // UnrealAngelscript |
| 40 | + [DataRow("accept_temporary_this")] // UnrealAngelscript |
| 41 | + public void FunctionModifier(string modifier) |
| 42 | + { |
| 43 | + var script = $"protected bool CanActivate_Client() {modifier}\r\n{{\r\nreturn true;\r\n}}"; |
| 44 | + var lexer = new UnrealAngelscriptLexer(new AntlrInputStream(script)); |
| 45 | + var tokenStream = new CommonTokenStream(lexer); |
| 46 | + var parser = new UnrealAngelscriptParser(tokenStream); |
| 47 | + parser.RemoveErrorListeners(); |
| 48 | + parser.AddErrorListener(new ThrowingErrorListener()); |
| 49 | + new UnrealAngelscriptSimpleVisitor(script).VisitChildren(parser.script()); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + [DataRow("int", "0")] |
| 54 | + [DataRow("int8", "0")] |
| 55 | + [DataRow("int16", "0")] |
| 56 | + [DataRow("int32", "0")] |
| 57 | + [DataRow("int64", "0")] |
| 58 | + [DataRow("uint", "0")] |
| 59 | + [DataRow("uint8", "0")] |
| 60 | + [DataRow("uint16", "0")] |
| 61 | + [DataRow("uint32", "0")] |
| 62 | + [DataRow("uint64", "0")] |
| 63 | + [DataRow("float", "0.0f")] |
| 64 | + [DataRow("float32", "0.0f")] |
| 65 | + [DataRow("float64", "0.0")] |
| 66 | + [DataRow("doublt", "0.0")] |
| 67 | + [DataRow("bool", "false")] |
| 68 | + [DataRow("FName", "n\"MyName\"")] // https://angelscript.hazelight.se/scripting/fname-literals/ |
| 69 | + [DataRow("string", "f\"Formatted String: {L:0.1f}\\u00B0\"")] // https://angelscript.hazelight.se/scripting/format-strings/ |
| 70 | + public void DataType(string type, string value) |
| 71 | + { |
| 72 | + var script = $"{type} VAR = {value};"; |
| 73 | + var lexer = new UnrealAngelscriptLexer(new AntlrInputStream(script)); |
| 74 | + var tokenStream = new CommonTokenStream(lexer); |
| 75 | + var parser = new UnrealAngelscriptParser(tokenStream); |
| 76 | + parser.RemoveErrorListeners(); |
| 77 | + parser.AddErrorListener(new ThrowingErrorListener()); |
| 78 | + new UnrealAngelscriptSimpleVisitor(script).VisitChildren(parser.script()); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments