Skip to content

Commit

Permalink
Add compiler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMagzuz committed Apr 25, 2024
1 parent 136f5b9 commit e5342a4
Show file tree
Hide file tree
Showing 31 changed files with 331 additions and 4 deletions.
2 changes: 1 addition & 1 deletion SocietalConstructionTool/SctRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static List<CompilerError> RunStaticChecks(string[] filenames)
* <param name="outputStream">The stream to output into</param>
* <returns>The result of emitting</returns>
*/
private static EmitResult Emit(string sourceText, Stream outputStream)
public static EmitResult Emit(string sourceText, Stream outputStream)
{
string generatedAssemblyName = "sctGenerated";
var tree = SyntaxFactory.ParseSyntaxTree(sourceText);
Expand Down
4 changes: 1 addition & 3 deletions SocietalConstructionToolTests/AbstractSnapshotTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ namespace SocietalConstructionToolTests
public abstract class AbstractSnapshotTests : VerifyBase
{
// yield all files in some subdirectory of the TestFiles directory
protected static IEnumerable<string[]> GetTestFiles(string dir) =>
Directory.GetFiles(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", dir))
.Select(f => new[] { f });
protected static IEnumerable<string[]> GetTestFiles(string dir) => TestFileUtils.GetTestFiles(dir);


[ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
Expand Down
26 changes: 26 additions & 0 deletions SocietalConstructionToolTests/CompilerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Sct;

namespace SocietalConstructionToolTests
{
[TestClass]
public class CompilerTests
{
private static IEnumerable<string[]> Files => TestFileUtils.GetTestFiles("CompilerTests");

[DataTestMethod]
[DynamicData(nameof(Files), DynamicDataSourceType.Property)]
public void ParseFile(string testFile)
{
var (outputText, errors) = SctRunner.CompileSct([testFile]);

Assert.IsFalse(errors.Any(), "SCT compiled with errors:\n" + string.Join('\n', errors));
Assert.IsNotNull(outputText);

// Discard the generated assembly
var stream = Stream.Null;

var result = SctRunner.Emit(outputText, stream);
Assert.IsTrue(result.Success, $"Could not compile generated C#\nCompilation diagnostics: {string.Join('\n', result.Diagnostics)}\n\nCode:\n" + outputText);
}
}
}
10 changes: 10 additions & 0 deletions SocietalConstructionToolTests/TestFileUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SocietalConstructionToolTests
{
public static class TestFileUtils
{
// yield all files in some subdirectory of the TestFiles directory
public static IEnumerable<string[]> GetTestFiles(string dir) =>
Directory.GetFiles(Path.Join(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", dir))
.Select(f => new[] { f });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Setup() -> void {
int a = (2 + 3 * 4) / (2 - 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
float pi = 3.14159;
float a = 1.5;
float b = 2.0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Setup() -> void {
int a = 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function Setup() -> void {
int a = 1 + 1;
float b = 0.4 + 2;
float c = 2 + 0.4;
float d = 0.4 + 0.4;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Add behavour which tests if stuff is equal
class Foo(int a, int b) {
state Bar {
enter End;
}

state Baz {
enter End;
}

state End {
exit;
}
}

class Output() {
state Equal { enter Equal; }
state Unequal { enter Equal; }
}

function Setup() -> void {
// Arrange
Predicate fullEqual = Foo::Bar(a: 1, b: 2);
Predicate fullEqual2 = Foo::Bar(a: 1, b: 2);
Predicate fullDifferent = Foo::Bar(a: 2, b: 3);

Predicate partialEqual = Foo::Bar(a: 1);
Predicate partialEqual2 = Foo::Bar(a: 1);
Predicate partialDifferent = Foo::Bar(a: 2);

Predicate wildcardEqual = Foo::?(a: 1, b: 2);
Predicate wildcardEqual2 = Foo::?(a: 1, b: 2);
Predicate wildcardDifferent = Foo::?(a: 2, b: 3);

// Assert
int equal = fullEqual == fullEqual2;

if (equal) {
create Output::Equal();
} else {
create Output::Unequal();
}

equal = partialEqual == partialEqual2;

if (equal) {
create Output::Equal();
} else {
create Output::Unequal();
}

equal = wildcardEqual == wildcardEqual2;

if (equal) {
create Output::Equal();
} else {
create Output::Unequal();
}

int unequal = fullEqual == fullDifferent;

if (!unequal) {
create Output::Equal();
} else {
create Output::Unequal();
}

unequal = partialEqual == partialDifferent;

if (!unequal) {
create Output::Equal();
} else {
create Output::Unequal();
}

unequal = wildcardEqual == wildcardDifferent;

if (!unequal) {
create Output::Equal();
} else {
create Output::Unequal();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
while (1) {
break;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class C() {
decorator D {
enter S;
}

state S {
enter S;
}
}

function Setup() -> void {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Setup() -> void {
int a = 1 && 1;
a = 1 || 0;

a = 2 == 2;
a = 3 != 4;

a = 3 > 2;
a = 4 >= 3;
a = 1 < 5;
a = 2 <= 5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Setup() -> void {
int a = !2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function Setup() -> void {
int a = 4;
a = -a;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function Setup() -> void {
float x = 7;
x = 9;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function Setup() -> void {
int x = 2;
x = x + 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Test(int id) {
state Exiting {
exit;
}
}

function Setup() -> void {
int a = (2-1);
create Test::Exiting(id: a);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Citizen(int townId) {
state Child {
enter Child;
}
}

function Setup() -> void {
int x = count(Citizen::Child(townId: 5));
x = exists(Citizen::Child(townId: 5));
float y = rand();
seed(12345);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Citizen(int townId) {
state Child {
enter Adult;
}

state Adult {
enter Adult;
}
}

function duplicate(Predicate p) -> int{
if (count(p) > 1) {
return 1;
}
return 0;
}

function Setup() -> void {
Predicate x = Citizen::Child(townId:1);
if (duplicate(x)) {
create Exiter::Exiting();
}
}

class Exiter() {
state Exiting {
exit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function Setup() -> void {
int a = count(TestAgent::A());
int b = count(TestAgent::?());
int c = exists(TestAgent::A());
int d = exists(TestAgent::?());
}

class TestAgent() {
state A {
exit;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Foo(int a, int b) {
state Bar {
enter Bar;
}
}

function Setup() -> void {
Predicate x = Foo::Bar(a:5);
if(exists(x)){
int y = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Citizen(int townId) {
state Child {
enter Adult;
}

state Adult {
enter Adult;
}
}

function getChildPredicate(int id) -> Predicate {
return Citizen::Child(townId: id);
}

function Setup() -> void {
int townId = 5;
int childrenCount = count(getChildPredicate(townId));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
while (1) {
break;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
while (1) {
continue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
if (1) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Setup() -> void {
if (1) {

} else {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Setup() -> void {
if (1) {

} else if (3 < 2) {

}

if (1) {

} else if (4 > 5) {

} else {

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Setup() -> void {
return;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Setup() -> void {
while (1) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Setup() -> void {
int x = (int) (2 / 2.0);
float y = 2 / 2.0;
int z = 1 / 2;
float u = 1.0 / 2;
int v = (int) (1.0 / 2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Setup() -> void {
float x = 2.0;
}

0 comments on commit e5342a4

Please sign in to comment.