-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement transform tool (closes #10)
- Loading branch information
Showing
9 changed files
with
512 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Transform.UnitTests | ||
{ | ||
using System.Linq; | ||
using System.Xml; | ||
|
||
[TestClass] | ||
public class Program_Tests | ||
{ | ||
[TestMethod] | ||
public void ParseRole() | ||
{ | ||
// arrange | ||
var webConfig = ParseXml(@" | ||
<configuration> | ||
<appSettings> | ||
<add key='role:define' value='abc' /> | ||
</appSettings> | ||
</configuration>"); | ||
|
||
// act | ||
var role = Program.ParseRole(webConfig); | ||
|
||
// assert | ||
Assert.AreEqual("abc", role); | ||
} | ||
|
||
[TestMethod] | ||
public void ProcessFile_Partial() | ||
{ | ||
// arrange | ||
var xml = ParseXml(@" | ||
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'> | ||
<sitecore xmlns:role='http://www.sitecore.net/xmlconfig/role/'> | ||
<element role:require='Delivery'> | ||
<delivery1 /> | ||
</element> | ||
<element role:require='Authoring'> | ||
<authoring xmlns:role2='http://www.sitecore.net/xmlconfig/role/' /> | ||
<element role:require='Delivery'> | ||
<delivery2 /> | ||
</element> | ||
</element> | ||
</sitecore> | ||
</configuration>"); | ||
|
||
var expected = ParseXml(@" | ||
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'> | ||
<sitecore> | ||
<element> | ||
<authoring /> | ||
</element> | ||
</sitecore> | ||
</configuration>"); | ||
|
||
// act | ||
var result = Program.ProcessFile(xml.DocumentElement, "Authoring".Split()); | ||
|
||
// assert | ||
CompareElements(expected.DocumentElement, xml.DocumentElement, "/configuration", 0); | ||
Assert.IsTrue(result); | ||
} | ||
|
||
[TestMethod] | ||
public void ProcessFile_Empty() | ||
{ | ||
// arrange | ||
var xml = ParseXml(@" | ||
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/' xmlns:role='http://www.sitecore.net/xmlconfig/role/'> | ||
<sitecore role:require='Delivery'> | ||
<delivery /> | ||
</sitecore> | ||
</configuration>"); | ||
|
||
var expected = ParseXml(@" | ||
<configuration xmlns:patch='http://www.sitecore.net/xmlconfig/'> | ||
</configuration>"); | ||
|
||
// act | ||
var result = Program.ProcessFile(xml.DocumentElement, "Authoring".Split()); | ||
|
||
// assert | ||
CompareElements(expected.DocumentElement, xml.DocumentElement, "/configuration", 0); | ||
Assert.IsFalse(result); | ||
} | ||
|
||
private void CompareElements(XmlElement expected, XmlElement actual, string path, int childNumber) | ||
{ | ||
CompareAttributes(expected.Attributes, actual.Attributes, path, childNumber); | ||
for (var i = 0; i < Math.Max(expected.ChildNodes.Count, actual.ChildNodes.Count); ++i) | ||
{ | ||
var exp = i < expected.ChildNodes.Count ? (XmlElement)expected.ChildNodes[i] : null; | ||
var act = i < actual.ChildNodes.Count ? (XmlElement)actual.ChildNodes[i] : null; | ||
if (exp == null) | ||
{ | ||
if (act == null) | ||
{ | ||
throw new NotImplementedException("This cannot be"); | ||
} | ||
|
||
Assert.Fail($"Unexpected xml element by path {path}[{childNumber}]: {act}"); | ||
} | ||
else if (act == null) | ||
{ | ||
Assert.Fail($"Missing xml element by path {path}[{childNumber}]: {exp}"); | ||
} | ||
else | ||
{ | ||
CompareElements(exp, act, path + "/" + exp.Name, i); | ||
} | ||
} | ||
} | ||
|
||
private void CompareAttributes(XmlAttributeCollection expected1, XmlAttributeCollection actual1, string path, int childNumber) | ||
{ | ||
var expected = expected1.OfType<XmlAttribute>().ToList(); | ||
var actual = actual1.OfType<XmlAttribute>().ToList(); | ||
foreach (XmlAttribute exp in expected1) | ||
{ | ||
expected.Remove(exp); | ||
var act = actual.FirstOrDefault(x => x.Name == exp.Name); | ||
Assert.IsNotNull(act, $"{path}[{childNumber}][@{exp.Name}]"); | ||
|
||
Assert.AreEqual(exp.Value, act.Value); | ||
actual.Remove(act); | ||
} | ||
|
||
foreach (var act in actual) | ||
{ | ||
Assert.Fail($"Unexpected attr {path}[{childNumber}][@{act.Name}='{act.Value}']"); | ||
} | ||
} | ||
|
||
private static XmlDocument ParseXml(string xml) | ||
{ | ||
var webConfig = new XmlDocument(); | ||
webConfig.LoadXml(xml.Replace("'", "\"").Trim()); | ||
return webConfig; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: AssemblyTitle("Transform.UnitTests")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Transform.UnitTests")] | ||
[assembly: AssemblyCopyright("Copyright © 2017")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
[assembly: ComVisible(false)] | ||
|
||
[assembly: Guid("9f9f65c7-9816-4e51-8a0b-4f5509682708")] | ||
|
||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{9F9F65C7-9816-4E51-8A0B-4F5509682708}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>Transform.UnitTests</RootNamespace> | ||
<AssemblyName>Transform.UnitTests</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> | ||
<IsCodedUITest>False</IsCodedUITest> | ||
<TestProjectType>UnitTest</TestProjectType> | ||
<NuGetPackageImportStamp> | ||
</NuGetPackageImportStamp> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program_Tests.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Transform\Transform.csproj"> | ||
<Project>{D0C52BE8-194D-400C-BC15-6600315BA39E}</Project> | ||
<Name>Transform</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | ||
<PropertyGroup> | ||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> | ||
</PropertyGroup> | ||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props'))" /> | ||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets'))" /> | ||
</Target> | ||
<Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net45" /> | ||
<package id="MSTest.TestFramework" version="1.1.11" targetFramework="net45" /> | ||
</packages> |
Oops, something went wrong.