Skip to content

Commit

Permalink
#65 - added some initial domain model classes and updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Sep 10, 2020
1 parent 99396ad commit 43acd3e
Show file tree
Hide file tree
Showing 16 changed files with 954 additions and 138 deletions.
38 changes: 23 additions & 15 deletions src/Kingsland.MofParser.Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using Kingsland.MofParser;
using Kingsland.MofParser.CodeGen;
using Kingsland.MofParser.Model;
using Kingsland.MofParser.Parsing;
using System;
using System.Linq;

namespace Kingsland.FileFormat.Mof.Tests
{
Expand All @@ -10,28 +14,32 @@ class Program
static void Main(string[] args)
{

const string filename = "dsc\\MyServer.mof";
const string mof = @"
instance of MSFT_RoleResource as $MSFT_RoleResource1ref
{
ResourceID = ""[WindowsFeature]IIS"";
Ensure = ""Present"";
SourceInfo = ""D:\\dsc\\MyServerConfig.ps1::6::9::WindowsFeature"";
Name = ""Web-Server"";
ModuleName = ""PSDesiredStateConfiguration"";
ModuleVersion = ""1.0"";
};";

// parse the mof file
var instances = PowerShellDscHelper.ParseMofFileInstances(filename);
var module = Parser.ParseText(mof);

// display the instances
foreach (var instance in instances)
foreach (var instance in module.Instances)
{
Console.WriteLine("--------------------------");
if (string.IsNullOrEmpty(instance.Alias))
{
Console.WriteLine(string.Format("instance of {0}", instance.ClassName));
}
else
{
Console.WriteLine(string.Format("instance of {0} as ${1}", instance.ClassName, instance.Alias));
}
foreach(var property in instance.Properties)
Console.WriteLine($"----------------------------------");
Console.WriteLine($"typename = {instance.TypeName}");
Console.WriteLine($"alias = {instance.Alias}");
Console.WriteLine($"properties:");
foreach (var property in instance.Properties)
{
Console.WriteLine(" {0} = {1}", property.Key.PadRight(14), property.Value.ToString());
Console.WriteLine(" {0} = {1}", property.Name.PadRight(13), property.Value);
}
Console.WriteLine("--------------------------");
Console.WriteLine($"----------------------------------");
}

}
Expand Down
47 changes: 47 additions & 0 deletions src/Kingsland.MofParser.UnitTests/Helpers/ModelAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Kingsland.MofParser.Model;
using NUnit.Framework;
using System.Linq;

namespace Kingsland.MofParser.UnitTests.Helpers
{

internal static class ModelAssert
{

public static void AreEqual(Module obj1, Module obj2)
{
Assert.IsNotNull(obj1);
Assert.IsNotNull(obj2);
Assert.AreEqual(obj1.Instances.Count, obj2.Instances.Count);
foreach(var pair in obj1.Instances
.Zip(obj2.Instances, (i1, i2) => (i1, i2)))
{
ModelAssert.AreEqual(pair.i1, pair.i2);
}
}

public static void AreEqual(Instance obj1, Instance obj2)
{
Assert.IsNotNull(obj1);
Assert.IsNotNull(obj2);
Assert.AreEqual(obj1.TypeName, obj2.TypeName);
Assert.AreEqual(obj1.Alias, obj2.Alias);
Assert.AreEqual(obj1.Properties.Count, obj2.Properties.Count);
foreach (var pair in obj1.Properties
.Zip(obj2.Properties, (p1, p2) => (p1, p2)))
{
ModelAssert.AreEqual(pair.p1, pair.p2);
}
}

public static void AreEqual(Property obj1, Property obj2)
{
Assert.IsNotNull(obj1);
Assert.IsNotNull(obj2);
Assert.AreEqual(obj1.Name, obj2.Name);
Assert.AreEqual(obj1.Value, obj2.Value);
}

}

}
2 changes: 0 additions & 2 deletions src/Kingsland.MofParser.UnitTests/Helpers/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Xml;

namespace Kingsland.MofParser.UnitTests.Helpers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CodeGen\MofGeneratorTests.cs" />
<Compile Include="Helpers\ModelAssert.cs" />
<Compile Include="Helpers\TestUtils.cs" />
<Compile Include="Helpers\TokenComparer.cs" />
<Compile Include="Lexer\LexerHelper.cs" />
Expand Down
42 changes: 21 additions & 21 deletions src/Kingsland.MofParser.UnitTests/Lexer/LexerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,127 +68,127 @@ private static void AssertAreEqualInternal(SyntaxToken expectedToken, SyntaxToke
LexerHelper.GetAssertErrorMessage($"actual Text does not match expected value", index));
switch (expectedToken)
{
case AliasIdentifierToken token:
case AliasIdentifierToken _:
Assert.IsTrue(
TokenComparer.AreEqual((AliasIdentifierToken)expectedToken, (AliasIdentifierToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case AttributeCloseToken token:
case AttributeCloseToken _:
Assert.IsTrue(
TokenComparer.AreEqual((AttributeCloseToken)expectedToken, (AttributeCloseToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case AttributeOpenToken token:
case AttributeOpenToken _:
Assert.IsTrue(
TokenComparer.AreEqual((AttributeOpenToken)expectedToken, (AttributeOpenToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case BlockCloseToken token:
case BlockCloseToken _:
Assert.IsTrue(
TokenComparer.AreEqual((BlockCloseToken)expectedToken, (BlockCloseToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case BlockOpenToken token:
case BlockOpenToken _:
Assert.IsTrue(
TokenComparer.AreEqual((BlockOpenToken)expectedToken, (BlockOpenToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case BooleanLiteralToken token:
case BooleanLiteralToken _:
Assert.IsTrue(
TokenComparer.AreEqual((BooleanLiteralToken)expectedToken, (BooleanLiteralToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case ColonToken token:
case ColonToken _:
Assert.IsTrue(
TokenComparer.AreEqual((ColonToken)expectedToken, (ColonToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case CommaToken token:
case CommaToken _:
Assert.IsTrue(
TokenComparer.AreEqual((CommaToken)expectedToken, (CommaToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case CommentToken token:
case CommentToken _:
Assert.IsTrue(
TokenComparer.AreEqual((CommentToken)expectedToken, (CommentToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case DotOperatorToken token:
case DotOperatorToken _:
Assert.IsTrue(
TokenComparer.AreEqual((DotOperatorToken)expectedToken, (DotOperatorToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case EqualsOperatorToken token:
case EqualsOperatorToken _:
Assert.IsTrue(
TokenComparer.AreEqual((EqualsOperatorToken)expectedToken, (EqualsOperatorToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case IdentifierToken token:
case IdentifierToken _:
Assert.IsTrue(
TokenComparer.AreEqual((IdentifierToken)expectedToken, (IdentifierToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case IntegerLiteralToken token:
case IntegerLiteralToken _:
Assert.IsTrue(
TokenComparer.AreEqual((IntegerLiteralToken)expectedToken, (IntegerLiteralToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case NullLiteralToken token:
case NullLiteralToken _:
Assert.IsTrue(
TokenComparer.AreEqual((NullLiteralToken)expectedToken, (NullLiteralToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case ParenthesisCloseToken token:
case ParenthesisCloseToken _:
Assert.IsTrue(
TokenComparer.AreEqual((ParenthesisCloseToken)expectedToken, (ParenthesisCloseToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case ParenthesisOpenToken token:
case ParenthesisOpenToken _:
Assert.IsTrue(
TokenComparer.AreEqual((ParenthesisOpenToken)expectedToken, (ParenthesisOpenToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case PragmaToken token:
case PragmaToken _:
Assert.IsTrue(
TokenComparer.AreEqual((PragmaToken)expectedToken, (PragmaToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case RealLiteralToken token:
case RealLiteralToken _:
Assert.IsTrue(
TokenComparer.AreEqual((RealLiteralToken)expectedToken, (RealLiteralToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case StatementEndToken token:
case StatementEndToken _:
Assert.IsTrue(
TokenComparer.AreEqual((StatementEndToken)expectedToken, (StatementEndToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case StringLiteralToken token:
case StringLiteralToken _:
Assert.IsTrue(
TokenComparer.AreEqual((StringLiteralToken)expectedToken, (StringLiteralToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
);
break;
case WhitespaceToken token:
case WhitespaceToken _:
Assert.IsTrue(
TokenComparer.AreEqual((WhitespaceToken)expectedToken, (WhitespaceToken)actualToken),
LexerHelper.GetAssertErrorMessage($"actual token does not match expected token", index)
Expand Down
3 changes: 1 addition & 2 deletions src/Kingsland.MofParser.UnitTests/Lexer/LexerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2063,5 +2063,4 @@ private static void LexMethodTest(string mofFilename)

}

}
///
}
Loading

0 comments on commit 43acd3e

Please sign in to comment.