Skip to content

Commit

Permalink
[test] adding unit test for custom csharp namespace testing which val…
Browse files Browse the repository at this point in the history
…idates correct namespace generation for complex schema dependencies supported with commit: 0209ab4
  • Loading branch information
ivan committed Oct 1, 2019
1 parent f5d0c3a commit 48a8473
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
30 changes: 16 additions & 14 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
uris.AddRange(expandedGlob);
}

var namespaceMap = namespaces.Select(n => ParseNamespace(n, namespacePrefix)).ToNamespaceProvider(key =>
var namespaceMap = namespaces.Select(n => Utility.ParseNamespace(n, namespacePrefix)).ToNamespaceProvider(key =>
{
var xn = key.XmlSchemaNamespace;
var name = string.Join(".", xn.Split('/').Where(p => p != "schema" && GeneratorConfiguration.IdentifierRegex.IsMatch(p))
Expand Down Expand Up @@ -170,7 +170,21 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
generator.Generate(uris);
}

static KeyValuePair<NamespaceKey, string> ParseNamespace(string nsArg, string namespacePrefix)
static void ShowHelp(OptionSet p)
{
System.Console.WriteLine("Usage: dotnet xscgen [OPTIONS]+ xsdFile...");
System.Console.WriteLine("Generate C# classes from XML Schema files.");
System.Console.WriteLine("Version " + typeof(Generator).Assembly.GetName().Version);
System.Console.WriteLine(@"xsdFiles may contain globs, e.g. ""content\{schema,xsd}\**\*.xsd"", and URLs.");
System.Console.WriteLine(@"Append - to option to disable it, e.g. --interface-.");
System.Console.WriteLine();
System.Console.WriteLine("Options:");
p.WriteOptionDescriptions(System.Console.Out);
}
}
public static class Utility
{
public static KeyValuePair<NamespaceKey, string> ParseNamespace(string nsArg, string namespacePrefix)
{
var parts = nsArg.Split(new[] { '=' }, 2);
var xmlNs = parts[0];
Expand All @@ -184,17 +198,5 @@ static KeyValuePair<NamespaceKey, string> ParseNamespace(string nsArg, string na
}
return new KeyValuePair<NamespaceKey, string>(new NamespaceKey(source, xmlNs), netNs);
}

static void ShowHelp(OptionSet p)
{
System.Console.WriteLine("Usage: dotnet xscgen [OPTIONS]+ xsdFile...");
System.Console.WriteLine("Generate C# classes from XML Schema files.");
System.Console.WriteLine("Version " + typeof(Generator).Assembly.GetName().Version);
System.Console.WriteLine(@"xsdFiles may contain globs, e.g. ""content\{schema,xsd}\**\*.xsd"", and URLs.");
System.Console.WriteLine(@"Append - to option to disable it, e.g. --interface-.");
System.Console.WriteLine();
System.Console.WriteLine("Options:");
p.WriteOptionDescriptions(System.Console.Out);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\XmlSampleGenerator\XmlSampleGenerator.csproj" />
<ProjectReference Include="..\XmlSchemaClassGenerator.Console\XmlSchemaClassGenerator.Console.csproj" />
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
66 changes: 64 additions & 2 deletions XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Ganss.IO;
using Microsoft.CodeAnalysis;
using Microsoft.Xml.XMLGen;
using XmlSchemaClassGenerator.Console;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -370,16 +371,77 @@ void TestCompareToXsd(Type t1, Type t2, string file)

[Fact, TestPriority(1)]
[UseCulture("en-US")]
public void TestBpmn()
public void TestCustomNamespaces()
{
string customNsPattern = "|{0}={1}";
string bpmnXsd = "BPMN20.xsd";
string semantXsd = "Semantic.xsd";
string bpmndiXsd = "BPMNDI.xsd";
string dcXsd = "DC.xsd";
string diXsd = "DI.xsd";

Dictionary<string, string> xsdToCsharpNsMap = new Dictionary<string, string>
{
{ bpmnXsd, "Namespace1" },
{ semantXsd, "Namespace1" },
{ bpmndiXsd, "Namespace2" },
{ dcXsd, "Namespace3" },
{ diXsd, "Namespace4" }
};

Dictionary<string, string> xsdToCsharpTypeMap = new Dictionary<string, string>
{
{ bpmnXsd, "TDefinitions" },
{ semantXsd, "TActivity" },
{ bpmndiXsd, "BPMNDiagram" },
{ dcXsd, "Font" },
{ diXsd, "DiagramElement" }
};

List<string> customNamespaceConfig = new List<string>();

foreach (var ns in xsdToCsharpNsMap)
customNamespaceConfig.Add(string.Format(customNsPattern, ns.Key, ns.Value));

var assembly = Compiler.Generate("Bpmn", BpmnPattern, new Generator
{
DataAnnotationMode = DataAnnotationMode.All,
GenerateNullables = true,
MemberVisitor = (member, model) => { }
MemberVisitor = (member, model) => { },
NamespaceProvider = customNamespaceConfig.Select(n => Utility.ParseNamespace(n, null)).ToNamespaceProvider()
});
Assert.NotNull(assembly);

Type type = null;

type = assembly.GetTypes().SingleOrDefault(t => t.Name == xsdToCsharpTypeMap[bpmnXsd]);
Assert.NotNull(type);
Assert.Equal(xsdToCsharpNsMap[bpmnXsd], type.Namespace);

type = assembly.GetTypes().SingleOrDefault(t => t.Name == xsdToCsharpTypeMap[semantXsd]);
Assert.NotNull(type);
Assert.Equal(xsdToCsharpNsMap[semantXsd], type.Namespace);

type = assembly.GetTypes().SingleOrDefault(t => t.Name == xsdToCsharpTypeMap[bpmndiXsd]);
Assert.NotNull(type);
Assert.Equal(xsdToCsharpNsMap[bpmndiXsd], type.Namespace);

type = assembly.GetTypes().SingleOrDefault(t => t.Name == xsdToCsharpTypeMap[dcXsd]);
Assert.NotNull(type);
Assert.Equal(xsdToCsharpNsMap[dcXsd], type.Namespace);

type = assembly.GetTypes().SingleOrDefault(t => t.Name == xsdToCsharpTypeMap[diXsd]);
Assert.NotNull(type);
Assert.Equal(xsdToCsharpNsMap[diXsd], type.Namespace);
}

[Fact, TestPriority(2)]
[UseCulture("en-US")]
public void TestBpmn()
{
var assembly = Compiler.Generate("Bpmn", BpmnPattern);
Assert.NotNull(assembly);

var type = assembly.GetTypes().SingleOrDefault(t => t.Name == "TDefinitions");
Assert.NotNull(type);

Expand Down

0 comments on commit 48a8473

Please sign in to comment.