More tests

main
Paul Schneider 11 months ago
parent e18dc6e084
commit 96804f8531
3 changed files with 112 additions and 49 deletions

@ -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<ScriptException>(()=>
Parse("B+A"));
Assert.Equal(0, ex.Location.Line);
Assert.Equal(1, ex.Location.Column);
}
}

@ -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;
}
}
}

@ -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;
}
}
}
Loading…