Skip to content

Commit

Permalink
Fixes #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcb committed Sep 19, 2015
1 parent 0eb2376 commit 67df40a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Configuration FromXDocument(XDocument document)
.XPathSelectElements("./Interfaces")
.Single()
.Nodes()
.Cast<XElement>()
.OfType<XElement>()
.Select(x => new Filter(ParseFilterType(x.Name.LocalName), x.Element("Pattern").Value));
return new Configuration(namespaceTransformations, nameTransformations, interfaceFilters);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Kent.Boogaart.PCLMock.UnitTests.CodeGeneration.Models
{
using System.Xml.Linq;
using Kent.Boogaart.PCLMock.CodeGeneration.Models;
using Xunit;

public sealed class ConfigurationFixture
{
[Fact]
public void from_xdocument_works_as_expected()
{
var inputResourceName = "Kent.Boogaart.PCLMock.UnitTests.CodeGeneration.Models.ConfigurationFixtureResources.Input.txt";

using (var inputStream = this.GetType().Assembly.GetManifestResourceStream(inputResourceName))
{
var document = XDocument.Load(inputStream);
var configuration = Configuration.FromXDocument(document);

Assert.Equal(2, configuration.NamespaceTransformations.Count);
Assert.Equal("(?<name>.+)", configuration.NamespaceTransformations[0].Pattern);
Assert.Equal("${name}.Mocks", configuration.NamespaceTransformations[0].Replacement);
Assert.Equal("Up", configuration.NamespaceTransformations[1].Pattern);
Assert.Equal("Down", configuration.NamespaceTransformations[1].Replacement);

Assert.Equal(3, configuration.NameTransformations.Count);
Assert.Equal("I(?<name>[A-Z].*)", configuration.NameTransformations[0].Pattern);
Assert.Equal("${name}", configuration.NameTransformations[0].Replacement);
Assert.Equal("(?<name>[A-Z].*)\\<.*\\>", configuration.NameTransformations[1].Pattern);
Assert.Equal("${name}", configuration.NameTransformations[1].Replacement);
Assert.Equal("(?<name>.+)", configuration.NameTransformations[2].Pattern);
Assert.Equal("${name}Mock", configuration.NameTransformations[2].Replacement);

Assert.Equal(2, configuration.InterfaceFilters.Count);
Assert.Equal(FilterType.Include, configuration.InterfaceFilters[0].Type);
Assert.Equal(".*", configuration.InterfaceFilters[0].Pattern);
Assert.Equal(FilterType.Exclude, configuration.InterfaceFilters[1].Type);
Assert.Equal("FooBar", configuration.InterfaceFilters[1].Pattern);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8" ?>

<!--
This input file to PCLMock's mock generation T4 template allows you to control which mocks are generated, and how they are named.
All patterns specified in this file are .NET regular expressions.
-->
<Mocks>
<!--
Namespace transformations allow you to transform the original namespace into a new namespace in which the mock will be placed.
Each transformation receives the output from the previous transformation, and the first transformation receives the original namespace.
-->
<NamespaceTransformations>
<!--
append ".Mocks" onto the namespace
-->
<Transformation>
<Pattern><![CDATA[(?<name>.+)]]></Pattern>
<Replacement>${name}.Mocks</Replacement>
</Transformation>

<!--
<Transformation>
<Pattern><![CDATA[(?<name>.+)]]></Pattern>
<Replacement>${name}.Mocks</Replacement>
</Transformation>
-->

<Transformation>
<!-- hello! -->
<Pattern>Up</Pattern>
<Replacement>Down</Replacement>
</Transformation>
</NamespaceTransformations>

<!--
Name transformations allow you to transform the original type name into a new name for the generated mock class.
Each transformation receives the output from the previous transformation, and the first transformation receives the original type name.
-->
<NameTransformations>
<!--
if the name is prefixed with "I", strip it off
-->
<Transformation>
<Pattern><![CDATA[I(?<name>[A-Z].*)]]></Pattern>
<Replacement>${name}</Replacement>
</Transformation>
<!--
if the name includes generic arguments, strip them out
-->
<Transformation>
<Pattern><![CDATA[(?<name>[A-Z].*)\<.*\>]]></Pattern>
<Replacement>${name}</Replacement>
</Transformation>
<!--
append "Mock" onto the name
-->
<Transformation>
<Pattern><![CDATA[(?<name>.+)]]></Pattern>
<Replacement>${name}Mock</Replacement>
</Transformation>

<!--
<Transformation>
<Pattern><![CDATA[(?<name>.+)]]></Pattern>
<Replacement>${name}Mock</Replacement>
</Transformation>
-->
</NameTransformations>

<!--
To determine whether a mock should be generated for a given interface, its assembly-qualified name (e.g. "Name.Space.TypeName, AssemblyName") is matched
against the filters specified below. Filters either Include or Exclude interfaces that match their pattern. Filters are executed in the order
they appear, so latter filters can override the result of an earlier filter.
-->
<Interfaces>
<Include>
<Pattern>.*</Pattern>
</Include>

<Exclude>
<!-- I don't like foo bar! -->
<Pattern>FooBar</Pattern>
</Exclude>

<!--
<Include>
<Pattern>Whatever</Pattern>
</Include>
-->
</Interfaces>
</Mocks>
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<Compile Include="ArgumentFilters\LogicalNotArgumentFilterFixture.cs" />
<Compile Include="ArgumentFilters\MatchesArgumentFilterFixture.cs" />
<Compile Include="CodeGeneration\GeneratorFixture.cs" />
<Compile Include="CodeGeneration\Models\ConfigurationFixture.cs" />
<Compile Include="MockBaseFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\ObjectExtensionsFixture.cs" />
Expand Down Expand Up @@ -216,6 +217,9 @@
<ItemGroup>
<EmbeddedResource Include="CodeGeneration\GeneratorFixtureResources\OutAndRefOutput_CSharp.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="CodeGeneration\Models\ConfigurationFixtureResources\Input.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down

0 comments on commit 67df40a

Please sign in to comment.