diff --git a/test/test.core/ClauseTests.cs b/test/test.core/ClauseTests.cs new file mode 100644 index 0000000..2c733b8 --- /dev/null +++ b/test/test.core/ClauseTests.cs @@ -0,0 +1,58 @@ +using Irony.Interpreter; +using irules.core; + +namespace test.core; + +public class ClauseTests : GrammarTester +{ + public ClauseTests() : base(new Clause()) + { + } + + [Fact] + public void TestA() + { + Parse("A"); + } + [Fact] + public void TestNotA() + { + Parse("^A"); + } + + [Fact] + public void TestAandB() + { + Parse("A&B"); + } + [Fact] + public void TestAorB() + { + Parse("A|B"); + } + [Fact] + public void TestAorNotB() + { + Parse("A|^B"); + } + [Fact] + public void TestAandNotB() + { + Parse("A&^B"); + } + [Fact] + public void TestNotAandNotB() + { + Parse("^A&^B"); + } + + + [Fact] + public void TestSyntactError() + { + var ex = Assert.Throws(()=> + Parse("B+A")); + Assert.Equal(0, ex.Location.Line); + Assert.Equal(1, ex.Location.Column); + } +} \ No newline at end of file diff --git a/test/test.core/GrammarTester.cs b/test/test.core/GrammarTester.cs new file mode 100644 index 0000000..531f4a3 --- /dev/null +++ b/test/test.core/GrammarTester.cs @@ -0,0 +1,54 @@ +using Irony.Interpreter; +using Irony.Parsing; +using Xunit.Sdk; + +namespace test.core; + +public class GrammarTester +{ + private Grammar grammar; + private LanguageData language; + private Parser parser; + + public GrammarTester(Grammar grammar) + { + this.grammar = grammar; + + language = new LanguageData(grammar); + + parser = new Parser(language); + } + + public Exception LastException { get; private set; } + public AppStatus Status { get; private set; } + public ParseTree LastScript { get; private set; } + + public object Parse(string script) + { + try + { + var parsedScript = parser.Parse(script); + if (parsedScript.HasErrors()) + { + Status = AppStatus.SyntaxError; + var ex = new ScriptException("Syntax errors found: " + + string.Join(", ", parsedScript.ParserMessages.Select(m => $"{m.Message} at line {m.Location.Line}, column {m.Location.Column}" ))); + ex.Location = parsedScript.ParserMessages.First().Location; + throw ex; + } + + if (parser.Context.Status == ParserStatus.AcceptedPartial) + { + throw new Exception("AcceptedPartial"); + } + LastScript = parsedScript; + return parsedScript; + } + catch (Exception ex) + { + this.LastException = ex; + this.Status = AppStatus.Crash; + throw; + } + } +} diff --git a/test/test.core/UnitTest1.cs b/test/test.core/UnitTest1.cs deleted file mode 100644 index 7a55757..0000000 --- a/test/test.core/UnitTest1.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Irony.Interpreter; -using Irony.Parsing; -using irules.core; -using Xunit.Sdk; - -namespace test.core; - -public class UnitTest1 -{ - public AppStatus Status { get; private set; } - public ParseTree LastScript { get; private set; } - public Exception LastException { get; private set; } - - [Fact] - public void Test1() - { - Parse("A"); - } - - - public object Parse(string script) { - - var grammar = new Clause(); - - var language = new LanguageData(grammar); - - var parser = new Parser(language); - try { - var parsedScript = parser.Parse(script); - if (parsedScript.HasErrors()) { - Status = AppStatus.SyntaxError; - throw new ScriptException("Syntax errors found: " - + string.Join(", ",parsedScript.ParserMessages)); - } - - if (parser.Context.Status == ParserStatus.AcceptedPartial) { - throw new Exception ("AcceptedPartial"); - } - LastScript = parsedScript; - return parsedScript; - } catch (ScriptException) { - throw; - } catch (Exception ex) { - this.LastException = ex; - this.Status = AppStatus.Crash; - return null; - } - } -} \ No newline at end of file