diff --git a/src/Kingsland.MofParser.HtmlReport/Resources/DscResource.cs b/src/Kingsland.MofParser.HtmlReport/Resources/DscResource.cs
index 166f173..7fe2cd5 100644
--- a/src/Kingsland.MofParser.HtmlReport/Resources/DscResource.cs
+++ b/src/Kingsland.MofParser.HtmlReport/Resources/DscResource.cs
@@ -1,4 +1,6 @@
using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
using System.Collections.ObjectModel;
namespace Kingsland.MofParser.HtmlReport.Resources;
@@ -53,13 +55,11 @@ public Instance Instance
: DscResource.GetResourceNameFromResourceId(this.ResourceId);
public ReadOnlyCollection DependsOn =>
- new(
- new List(
- this.Instance.Properties.Single(
- property => property.Name == "ResourceID"
- ).Value as string[] ?? Enumerable.Empty()
- )
- );
+ this.Instance.Properties
+ .Where(property => property.Name == "ResourceID")
+ .SelectMany(property => ((LiteralValueArray)property.Value).Values)
+ .Select(literalValue => ((StringValue)literalValue).Value)
+ .ToList().AsReadOnly();
public string? ModuleName =>
this.GetStringProperty(nameof(this.ModuleName));
@@ -98,9 +98,14 @@ public static DscResource FromInstance(string filename, string computerName, Ins
protected string? GetStringProperty(string propertyName)
{
- return this.Instance.Properties.SingleOrDefault(
- property => property.Name == propertyName
- )?.Value as string;
+ var property = this.Instance.Properties
+ .SingleOrDefault(property => property.Name == propertyName);
+ if (property is null)
+ {
+ return null;
+ }
+ var value = ((StringValue)property.Value).Value;
+ return value;
}
#endregion
diff --git a/src/Kingsland.MofParser.HtmlReport/Resources/ScriptResource.cs b/src/Kingsland.MofParser.HtmlReport/Resources/ScriptResource.cs
index af8f0c3..32338be 100644
--- a/src/Kingsland.MofParser.HtmlReport/Resources/ScriptResource.cs
+++ b/src/Kingsland.MofParser.HtmlReport/Resources/ScriptResource.cs
@@ -1,4 +1,5 @@
using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Types;
namespace Kingsland.MofParser.HtmlReport.Resources;
diff --git a/src/Kingsland.MofParser.HtmlReport/Wrappers/ResourceGroup.cs b/src/Kingsland.MofParser.HtmlReport/Wrappers/ResourceGroup.cs
index 3a15073..bf71c5e 100644
--- a/src/Kingsland.MofParser.HtmlReport/Wrappers/ResourceGroup.cs
+++ b/src/Kingsland.MofParser.HtmlReport/Wrappers/ResourceGroup.cs
@@ -12,11 +12,9 @@ public ResourceGroup(
{
this.Filename = filename ?? throw new ArgumentNullException(nameof(filename));
this.ComputerName = computerName ?? throw new ArgumentNullException(nameof(computerName));
- this.Wrappers = new(
- new List(
- wrappers ?? throw new ArgumentNullException(nameof(wrappers))
- )
- );
+ this.Wrappers = new List(
+ wrappers ?? throw new ArgumentNullException(nameof(wrappers))
+ ).AsReadOnly();
}
public string Filename
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests.cs
index 33974b1..cbede08 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests.cs
@@ -1,7 +1,8 @@
using Kingsland.MofParser.Ast;
using Kingsland.MofParser.CodeGen;
using Kingsland.MofParser.Lexing;
-using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Converter;
+using Kingsland.MofParser.Models.Types;
using Kingsland.MofParser.Parsing;
using Kingsland.MofParser.UnitTests.Helpers;
using Kingsland.ParseFx.Parsing;
@@ -80,6 +81,12 @@ private static void AssertRoundtrip(
)
);
Assert.That(actualAstText, Is.EqualTo(sourceText));
+ // check the model converter works
+ var actualModule = ModelConverter.ConvertMofSpecificationAst(actualAst);
+ if (expectedModule is not null)
+ {
+ ModelAssert.AreDeepEqual(actualModule, expectedModule);
+ }
}
private static void AssertRoundtripException(string sourceText, string expectedMessage, ParserQuirks parserQuirks = ParserQuirks.None)
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ClassDeclaration.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ClassDeclaration.cs
index 85e805a..554fcf6 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ClassDeclaration.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ClassDeclaration.cs
@@ -36,12 +36,12 @@ class GOLF_Base
.StatementEndToken()
.ToList();
var expectedAst = new MofSpecificationAst.Builder {
- Productions = new List {
+ Productions = [
new ClassDeclarationAst.Builder {
ClassName = new IdentifierToken("GOLF_Base"),
ClassFeatures = []
}.Build()
- }
+ ]
}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst);
}
@@ -73,12 +73,12 @@ class GOLF_Base : GOLF_Superclass
.StatementEndToken()
.ToList();
var expectedAst = new MofSpecificationAst.Builder {
- Productions = new List {
+ Productions = [
new ClassDeclarationAst.Builder {
ClassName = new IdentifierToken("GOLF_Base"),
SuperClass = new IdentifierToken("GOLF_Superclass"),
}.Build()
- }
+ ]
}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst);
}
@@ -125,10 +125,10 @@ class GOLF_Base
.StatementEndToken()
.ToList();
var expectedAst = new MofSpecificationAst.Builder {
- Productions = new List {
+ Productions = [
new ClassDeclarationAst.Builder {
ClassName = new IdentifierToken("GOLF_Base"),
- ClassFeatures = new List {
+ ClassFeatures = [
new PropertyDeclarationAst.Builder {
ReturnType = new IdentifierToken("string"),
PropertyName = new IdentifierToken("InstanceID"),
@@ -140,9 +140,9 @@ class GOLF_Base
new NullLiteralToken("Null")
)
}.Build()
- }
+ ]
}.Build()
- }
+ ]
}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst);
}
@@ -227,61 +227,57 @@ class GOLF_Base
.StatementEndToken()
.ToList();
var expectedAst = new MofSpecificationAst.Builder {
- Productions = new List {
+ Productions = [
new ClassDeclarationAst.Builder {
ClassName = new IdentifierToken("GOLF_Base"),
- ClassFeatures = new List {
+ ClassFeatures = [
new PropertyDeclarationAst.Builder {
- QualifierList = new QualifierListAst(
- new List {
- new QualifierValueAst.Builder {
- QualifierName = new IdentifierToken("Description"),
- Initializer = new QualifierValueInitializerAst(
- new StringValueAst(
- new StringLiteralToken("an instance of a class that derives from the GOLF_Base class. "),
- "an instance of a class that derives from the GOLF_Base class. "
- )
+ QualifierList = new QualifierListAst([
+ new QualifierValueAst.Builder {
+ QualifierName = new IdentifierToken("Description"),
+ Initializer = new QualifierValueInitializerAst(
+ new StringValueAst(
+ new StringLiteralToken("an instance of a class that derives from the GOLF_Base class. "),
+ "an instance of a class that derives from the GOLF_Base class. "
)
- }.Build(),
- new QualifierValueAst.Builder {
- QualifierName = new IdentifierToken("Key"),
- }.Build()
- }
- ),
+ )
+ }.Build(),
+ new QualifierValueAst.Builder {
+ QualifierName = new IdentifierToken("Key"),
+ }.Build()
+ ]),
ReturnType = new IdentifierToken("string"),
PropertyName = new IdentifierToken("InstanceID")
}.Build(),
new PropertyDeclarationAst.Builder {
- QualifierList = new QualifierListAst(
- new List {
- new QualifierValueAst.Builder {
- QualifierName = new IdentifierToken("Description"),
- Initializer = new QualifierValueInitializerAst(
- new StringValueAst(
- new StringLiteralToken("A short textual description (one- line string) of the"),
- "an instance of a class that derives from the GOLF_Base class. "
- )
+ QualifierList = new QualifierListAst([
+ new QualifierValueAst.Builder {
+ QualifierName = new IdentifierToken("Description"),
+ Initializer = new QualifierValueInitializerAst(
+ new StringValueAst(
+ new StringLiteralToken("A short textual description (one- line string) of the"),
+ "an instance of a class that derives from the GOLF_Base class. "
)
- }.Build(),
- new QualifierValueAst.Builder {
- QualifierName = new IdentifierToken("MaxLen"),
- Initializer = new QualifierValueInitializerAst(
- new IntegerValueAst(
- new IntegerLiteralToken(IntegerKind.DecimalValue, 64)
- )
+ )
+ }.Build(),
+ new QualifierValueAst.Builder {
+ QualifierName = new IdentifierToken("MaxLen"),
+ Initializer = new QualifierValueInitializerAst(
+ new IntegerValueAst(
+ new IntegerLiteralToken(IntegerKind.DecimalValue, 64)
)
- }.Build()
- }
- ),
+ )
+ }.Build()
+ ]),
ReturnType = new IdentifierToken("string"),
PropertyName = new IdentifierToken("Caption"),
Initializer = new NullValueAst(
new NullLiteralToken("Null")
)
}.Build()
- },
+ ],
}.Build()
- }
+ ]
}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst);
}
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ComplexValue.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ComplexValue.cs
index 69593d8..7a01a36 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ComplexValue.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_ComplexValue.cs
@@ -1,4 +1,7 @@
using Kingsland.MofParser.Ast;
+using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
using Kingsland.MofParser.Tokens;
using Kingsland.MofParser.UnitTests.Extensions;
using NUnit.Framework;
@@ -48,25 +51,23 @@ instance of GOLF_ClubMember
.StatementEndToken()
.ToList();
var expectedAst = new MofSpecificationAst.Builder {
- Productions = new List {
+ Productions = [
new InstanceValueDeclarationAst.Builder {
Instance = new IdentifierToken("instance"),
Of = new IdentifierToken("of"),
TypeName = new IdentifierToken("GOLF_ClubMember"),
- PropertyValues = new PropertyValueListAst(
- new List {
- new PropertySlotAst.Builder {
- PropertyName = new IdentifierToken("LastPaymentDate"),
- PropertyValue = new ComplexValueAst.Builder {
- Alias = new AliasIdentifierToken("MyAliasIdentifier")
- }.Build()
+ PropertyValues = new PropertyValueListAst([
+ new PropertySlotAst.Builder {
+ PropertyName = new IdentifierToken("LastPaymentDate"),
+ PropertyValue = new ComplexValueAst.Builder {
+ Alias = new AliasIdentifierToken("MyAliasIdentifier")
+ }.Build()
- }.Build()
- }
- ),
+ }.Build()
+ ]),
StatementEnd = new StatementEndToken()
}.Build()
- }
+ ]
}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst);
}
@@ -126,7 +127,52 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst.Builder
+ {
+ Productions = [
+ new InstanceValueDeclarationAst.Builder {
+ Instance = new IdentifierToken("instance"),
+ Of = new IdentifierToken("of"),
+ TypeName = new IdentifierToken("GOLF_ClubMember"),
+ PropertyValues = new PropertyValueListAst([
+ new PropertySlotAst.Builder {
+ PropertyName = new IdentifierToken("LastPaymentDate"),
+ PropertyValue = new ComplexValueAst.Builder {
+ Value = new IdentifierToken("value"),
+ Of = new IdentifierToken("of"),
+ TypeName = new IdentifierToken("GOLF_Date"),
+ PropertyValues = new PropertyValueListAst([
+ new(
+ new("Month"),
+ new EnumValueAst(
+ new("July")
+ )
+ )
+ ])
+ }.Build()
+ }.Build()
+ ]),
+ StatementEnd = new StatementEndToken()
+ }.Build()
+ ]
+ }.Build();
+ var expectedModule = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new Property(
+ "LastPaymentDate",
+ new ComplexValueObject(
+ "GOLF_Date",
+ [
+ new("Month", new EnumValue("July"))
+ ]
+ )
+ )
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModule);
}
}
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_InstanceValueDeclaration.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_InstanceValueDeclaration.cs
index c5b80ed..4b16151 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_InstanceValueDeclaration.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_InstanceValueDeclaration.cs
@@ -1,5 +1,6 @@
using Kingsland.MofParser.Ast;
-using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
using Kingsland.MofParser.Tokens;
using Kingsland.MofParser.UnitTests.Extensions;
using NUnit.Framework;
@@ -40,20 +41,18 @@ instance of GOLF_ClubMember
.ToList();
var expectedAst = new MofSpecificationAst([
new InstanceValueDeclarationAst(
- new IdentifierToken("instance"),
- new IdentifierToken("of"),
- new IdentifierToken("GOLF_ClubMember"),
- new PropertyValueListAst(),
- new StatementEndToken()
+ new("instance"), new("of"), new("GOLF_ClubMember"),
+ new([]),
+ new()
)
]);
- var expectedModule = new Module([]);
- RoundtripTests.AssertRoundtrip(
- sourceText,
- expectedTokens,
- expectedAst,
- expectedModule
- );
+ var expectedModule = new Module([
+ new(
+ "GOLF_ClubMember",
+ []
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModule);
}
[Test]
@@ -101,39 +100,24 @@ instance of GOLF_ClubMember
.ToList();
var expectedAst = new MofSpecificationAst([
new InstanceValueDeclarationAst(
- new IdentifierToken("instance"),
- new IdentifierToken("of"),
- new IdentifierToken("GOLF_ClubMember"),
- new PropertyValueListAst([
- new(
- new("FirstName"),
- new StringValueAst(
- [
- new StringLiteralToken("John")
- ],
- "John"
- )
- ),
- new(
- new("LastName"),
- new StringValueAst(
- [
- new StringLiteralToken("Doe")
- ],
- "Doe"
- )
- )
+ new("instance"), new("of"), new("GOLF_ClubMember"),
+ new([
+ new(new("FirstName"), new StringValueAst(new StringLiteralToken("John"), "John")),
+ new(new("LastName"), new StringValueAst(new StringLiteralToken("Doe"), "Doe"))
]),
- new StatementEndToken()
+ new()
+ )
+ ]);
+ var expectedModule = new Module([
+ new(
+ "GOLF_ClubMember",
+ [
+ new("FirstName", "John"),
+ new("LastName", "Doe")
+ ]
)
]);
- var expectedModule = new Module([]);
- RoundtripTests.AssertRoundtrip(
- sourceText,
- expectedTokens,
- expectedAst,
- expectedModule
- );
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModule);
}
[Test]
@@ -166,22 +150,18 @@ instance of GOLF_ClubMember as $MyAliasIdentifier
.ToList();
var expectedAst = new MofSpecificationAst([
new InstanceValueDeclarationAst(
- new IdentifierToken("instance"),
- new IdentifierToken("of"),
- new IdentifierToken("GOLF_ClubMember"),
- new IdentifierToken("as"),
- new AliasIdentifierToken("MyAliasIdentifier"),
- new PropertyValueListAst(),
- new StatementEndToken()
+ new("instance"), new("of"), new("GOLF_ClubMember"), new("as"), new("MyAliasIdentifier"),
+ new([]),
+ new()
+ )
+ ]);
+ var expectedModule = new Module([
+ new(
+ "GOLF_ClubMember", "MyAliasIdentifier",
+ []
)
]);
- var expectedModule = new Module([]);
- RoundtripTests.AssertRoundtrip(
- sourceText,
- expectedTokens,
- expectedAst,
- expectedModule
- );
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModule);
}
//[Test]
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValue.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValue.cs
index c5f2484..f5d640b 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValue.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValue.cs
@@ -1,4 +1,7 @@
-using Kingsland.MofParser.Tokens;
+using Kingsland.MofParser.Ast;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
+using Kingsland.MofParser.Tokens;
using Kingsland.MofParser.UnitTests.Extensions;
using NUnit.Framework;
@@ -46,7 +49,24 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new IntegerValueAst(new(IntegerKind.DecimalValue, 1)))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", 1)
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
[Test]
@@ -83,7 +103,24 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new RealValueAst(new(0.5)))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", 0.5)
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
[Test]
@@ -120,7 +157,24 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new BooleanValueAst(new(true)))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", true)
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
[Test]
@@ -157,7 +211,24 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new NullValueAst(new()))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", NullValue.Null)
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
[Test]
@@ -194,7 +265,24 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new StringValueAst(new StringLiteralToken("aaa"), "aaa"))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", "aaa")
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
}
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValueArray.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValueArray.cs
index 512b3d8..6e7a6a8 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValueArray.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_LiteralValueArray.cs
@@ -1,4 +1,7 @@
-using Kingsland.MofParser.Tokens;
+using Kingsland.MofParser.Ast;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
+using Kingsland.MofParser.Tokens;
using Kingsland.MofParser.UnitTests.Extensions;
using NUnit.Framework;
@@ -12,6 +15,61 @@ public static partial class RoundtripTests
public static class LiteralValueArrayTests
{
+ [Test]
+ public static void LiteralValueArrayWithNoItemsShouldRoundtrip()
+ {
+ var newline = Environment.NewLine;
+ var indent = " ";
+ var sourceText = @"
+ instance of GOLF_ClubMember
+ {
+ LastPaymentDate = {};
+ };
+ ".TrimIndent(newline).TrimString(newline);
+ var expectedTokens = new TokenBuilder()
+ // instance of GOLF_ClubMember
+ .IdentifierToken("instance")
+ .WhitespaceToken(" ")
+ .IdentifierToken("of")
+ .WhitespaceToken(" ")
+ .IdentifierToken("GOLF_ClubMember")
+ .WhitespaceToken(newline)
+ // {
+ .BlockOpenToken()
+ .WhitespaceToken(newline + indent)
+ // LastPaymentDate = { };
+ .IdentifierToken("LastPaymentDate")
+ .WhitespaceToken(" ")
+ .EqualsOperatorToken()
+ .WhitespaceToken(" ")
+ .BlockOpenToken()
+ .BlockCloseToken()
+ .StatementEndToken()
+ .WhitespaceToken(newline)
+ // };
+ .BlockCloseToken()
+ .StatementEndToken()
+ .ToList();
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new LiteralValueArrayAst([]))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new("LastPaymentDate", new LiteralValueArray())
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
+ }
+
[Test]
public static void LiteralValueArrayWithOneItemShouldRoundtrip()
{
@@ -48,7 +106,31 @@ instance of GOLF_ClubMember
.BlockCloseToken()
.StatementEndToken()
.ToList();
- RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
+ var expectedAst = new MofSpecificationAst([
+ new InstanceValueDeclarationAst(
+ new("instance"), new("of"), new("GOLF_ClubMember"), null, null,
+ new([
+ new(new("LastPaymentDate"), new LiteralValueArrayAst(
+ new IntegerValueAst(new(IntegerKind.DecimalValue, 1))
+ ))
+ ]),
+ new()
+ )
+ ]);
+ var expectedModel = new Module([
+ new Instance(
+ "GOLF_ClubMember",
+ [
+ new(
+ "LastPaymentDate",
+ new LiteralValueArray([
+ 1
+ ])
+ )
+ ]
+ )
+ ]);
+ RoundtripTests.AssertRoundtrip(sourceText, expectedTokens, expectedAst, expectedModel);
}
[Test]
diff --git a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_MethodDeclaration.cs b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_MethodDeclaration.cs
index fffa226..b9fff2f 100644
--- a/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_MethodDeclaration.cs
+++ b/src/Kingsland.MofParser.UnitTests/CodeGen/RoundtripTests_MethodDeclaration.cs
@@ -1,4 +1,5 @@
-using Kingsland.MofParser.Tokens;
+using Kingsland.MofParser.Ast;
+using Kingsland.MofParser.Tokens;
using Kingsland.MofParser.UnitTests.Extensions;
using NUnit.Framework;
@@ -250,7 +251,7 @@ class GOLF_Professional : GOLF_ClubMember
.ToList();
//var expectedAst = new MofSpecificationAst.Builder
//{
- // Productions = new List {
+ // Productions = [
// new ClassDeclarationAst.Builder {
// ClassName = new IdentifierToken("GOLF_Professional"),
// SuperClass = new IdentifierToken("GOLF_ClubMember"),
@@ -268,7 +269,7 @@ class GOLF_Professional : GOLF_ClubMember
// }.Build(),
// }
// }.Build()
- // }
+ // ]
//}.Build();
RoundtripTests.AssertRoundtrip(sourceText, expectedTokens);
}
diff --git a/src/Kingsland.MofParser.UnitTests/Helpers/AstAssert.cs b/src/Kingsland.MofParser.UnitTests/Helpers/AstAssert.cs
index f7c4cfc..9362d70 100644
--- a/src/Kingsland.MofParser.UnitTests/Helpers/AstAssert.cs
+++ b/src/Kingsland.MofParser.UnitTests/Helpers/AstAssert.cs
@@ -308,18 +308,27 @@ private static void AreEqual(PropertyValueAst? expected, PropertyValueAst? actua
case ComplexTypeValueAst ast:
AstAssert.AreEqual(ast, (ComplexTypeValueAst)actual, ignoreExtent);
return;
+ case EnumTypeValueAst ast:
+ AstAssert.AreEqual(ast, (EnumTypeValueAst)actual, ignoreExtent);
+ return;
case BooleanValueAst ast:
AstAssert.AreEqual(ast, (BooleanValueAst)actual, ignoreExtent);
return;
case IntegerValueAst ast:
AstAssert.AreEqual(ast, (IntegerValueAst)actual, ignoreExtent);
return;
+ case RealValueAst ast:
+ AstAssert.AreEqual(ast, (RealValueAst)actual, ignoreExtent);
+ return;
case StringValueAst ast:
AstAssert.AreEqual(ast, (StringValueAst)actual, ignoreExtent);
return;
case NullValueAst ast:
AstAssert.AreEqual(ast, (NullValueAst)actual, ignoreExtent);
return;
+ case LiteralValueArrayAst ast:
+ AstAssert.AreEqual(ast, (LiteralValueArrayAst)actual, ignoreExtent);
+ return;
default:
throw new NotImplementedException($"unhandled node type {expected.GetType().Name}");
}
@@ -387,6 +396,16 @@ private static void AreEqual(IntegerValueAst? expected, IntegerValueAst? actual,
Assert.That(actual.Value, Is.EqualTo(expected.Value));
}
+ private static void AreEqual(RealValueAst? expected, RealValueAst? actual, bool ignoreExtent)
+ {
+ if ((expected == null) || (actual == null))
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ Assert.That(actual.Value, Is.EqualTo(expected.Value));
+ }
+
private static void AreEqual(StringValueAst? expected, StringValueAst? actual, bool ignoreExtent)
{
if ((expected == null) || (actual == null))
@@ -401,7 +420,8 @@ private static void AreEqual(StringValueAst? expected, StringValueAst? actual, b
}
}
- private static void AreEqual(NullValueAst? expected, NullValueAst? actual, bool ignoreExtent) {
+ private static void AreEqual(NullValueAst? expected, NullValueAst? actual, bool ignoreExtent)
+ {
if ((expected == null) || (actual == null))
{
Assert.That(actual, Is.EqualTo(expected));
@@ -410,6 +430,66 @@ private static void AreEqual(NullValueAst? expected, NullValueAst? actual, bool
TokenAssert.AreEqual(expected.Token, actual.Token, ignoreExtent);
}
+ private static void AreEqual(LiteralValueArrayAst? expected, LiteralValueArrayAst? actual, bool ignoreExtent)
+ {
+ if ((expected == null) || (actual == null))
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ Assert.That(actual.Values.Count, Is.EqualTo(expected.Values.Count));
+ for (var i = 0; i < expected.Values.Count; i++)
+ {
+ AstAssert.AreEqual(expected.Values[i], actual.Values[i], ignoreExtent);
+ }
+ }
+
+ private static void AreEqual(EnumTypeValueAst? expected, EnumTypeValueAst? actual, bool ignoreExtent)
+ {
+ if ((expected == null) || (actual == null))
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ Assert.That(actual, Is.InstanceOf(expected.GetType()));
+ switch (expected)
+ {
+ case EnumValueAst ast:
+ AstAssert.AreEqual(ast, (EnumValueAst)actual, ignoreExtent);
+ return;
+ case EnumValueArrayAst ast:
+ AstAssert.AreEqual(ast, (EnumValueArrayAst)actual, ignoreExtent);
+ return;
+ default:
+ throw new NotImplementedException($"unhandled node type {expected.GetType().Name}");
+ }
+ }
+
+ private static void AreEqual(EnumValueAst? expected, EnumValueAst? actual, bool ignoreExtent)
+ {
+ if ((expected == null) || (actual == null))
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ TokenAssert.AreEqual(expected.EnumName, actual.EnumName, ignoreExtent);
+ TokenAssert.AreEqual(expected.EnumLiteral, actual.EnumLiteral, ignoreExtent);
+ }
+
+ private static void AreEqual(EnumValueArrayAst? expected, EnumValueArrayAst? actual, bool ignoreExtent)
+ {
+ if ((expected == null) || (actual == null))
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ Assert.That(actual.Values.Count, Is.EqualTo(expected.Values.Count));
+ for (var i = 0; i < expected.Values.Count; i++)
+ {
+ AstAssert.AreEqual(expected.Values[i], actual.Values[i], ignoreExtent);
+ }
+ }
+
#endregion
}
diff --git a/src/Kingsland.MofParser.UnitTests/Helpers/ModelAssert.cs b/src/Kingsland.MofParser.UnitTests/Helpers/ModelAssert.cs
index 7a9ba6c..c4536e2 100644
--- a/src/Kingsland.MofParser.UnitTests/Helpers/ModelAssert.cs
+++ b/src/Kingsland.MofParser.UnitTests/Helpers/ModelAssert.cs
@@ -1,4 +1,5 @@
-using Kingsland.MofParser.Models;
+using Kingsland.MofParser.Models.Types;
+using Kingsland.MofParser.Models.Values;
using NUnit.Framework;
namespace Kingsland.MofParser.UnitTests.Helpers;
@@ -6,36 +7,257 @@ namespace Kingsland.MofParser.UnitTests.Helpers;
internal static class ModelAssert
{
- public static void AreEqual(Module expected, Module actual)
+ public static void AreDeepEqual(object? actual, object? expected)
{
- Assert.That(expected, Is.Not.Null);
- Assert.That(actual, Is.Not.Null);
- Assert.That(actual.Instances.Count, Is.EqualTo(expected.Instances.Count));
- for (var i = 0; i < expected.Instances.Count; i++)
+ // handle null values
+ if ((actual is null) || (expected is null))
{
- ModelAssert.AreEqual(expected.Instances[i], actual.Instances[i]);
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
}
- }
-
- public static void AreEqual(Instance expected, Instance actual)
- {
- Assert.That(expected, Is.Not.Null);
- Assert.That(actual, Is.Not.Null);
- Assert.That(actual.TypeName, Is.EqualTo(expected.TypeName));
- Assert.That(actual.Alias, Is.EqualTo(expected.Alias));
- Assert.That(actual.Properties.Count, Is.EqualTo(expected.Properties.Count));
- for (var i = 0; i < expected.Properties.Count; i++)
+ // make sure types match
+ var expectedType = expected.GetType();
+ Assert.That(actual, Is.TypeOf(expectedType));
+ // handle value types
+ if (expectedType.IsValueType)
+ {
+ Assert.That(actual, Is.EqualTo(expected));
+ return;
+ }
+ // handle strings first, otherwise they get detected as IEnumerable
+ if (expected is string expectedString)
+ {
+ Assert.That((string)actual, Is.EqualTo(expectedString));
+ return;
+ }
+ // handle collections
+ if (expected is System.Collections.IEnumerable expectedEnumerable)
{
- ModelAssert.AreEqual(expected.Properties[i], actual.Properties[i]);
+ var actualItems = ((System.Collections.IEnumerable)actual).Cast