Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparison to return -1, 0, 1 and Improve xml element content comparison #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions sortxml.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sortxml", "sortxml.csproj", "{F015ED1D-89EF-4251-9788-D581536501F2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sortxmlXUnitProject", "sortxmlXUnitProject\sortxmlXUnitProject.csproj", "{292ABDF4-3CA6-499F-9EF5-A758D71DE929}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sortxml", "sortxml\sortxml.csproj", "{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,20 +17,35 @@ Global
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|x64.ActiveCfg = Debug|x64
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|x64.Build.0 = Debug|x64
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|x86.ActiveCfg = Debug|x86
{F015ED1D-89EF-4251-9788-D581536501F2}.Debug|x86.Build.0 = Debug|x86
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|Any CPU.Build.0 = Release|Any CPU
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|x64.ActiveCfg = Release|x64
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|x64.Build.0 = Release|x64
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|x86.ActiveCfg = Release|x86
{F015ED1D-89EF-4251-9788-D581536501F2}.Release|x86.Build.0 = Release|x86
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|Any CPU.Build.0 = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|x64.ActiveCfg = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|x64.Build.0 = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|x86.ActiveCfg = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Debug|x86.Build.0 = Debug|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|Any CPU.ActiveCfg = Release|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|Any CPU.Build.0 = Release|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|x64.ActiveCfg = Release|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|x64.Build.0 = Release|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|x86.ActiveCfg = Release|Any CPU
{292ABDF4-3CA6-499F-9EF5-A758D71DE929}.Release|x86.Build.0 = Release|Any CPU
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|x64.ActiveCfg = Debug|x64
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|x64.Build.0 = Debug|x64
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|x86.ActiveCfg = Debug|x86
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Debug|x86.Build.0 = Debug|x86
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|Any CPU.Build.0 = Release|Any CPU
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|x64.ActiveCfg = Release|x64
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|x64.Build.0 = Release|x64
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|x86.ActiveCfg = Release|x86
{20D5AD14-A78A-441F-8EA8-E99F58F7B6F9}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6AB24862-E35C-4F40-A1F7-A77F74667605}
EndGlobalSection
EndGlobal
17 changes: 13 additions & 4 deletions Program.cs → sortxml/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DEALINGS IN THE SOFTWARE.

namespace sortxml
{
class Program
public class Program
{
static bool sort_node = true;
static bool sort_attr = true;
Expand All @@ -43,7 +43,7 @@ class Program

static string primary_attr = "";

static int Main( string[] arguments )
public static int Main( string[] arguments )
{
var inf = "";
var outf = "";
Expand Down Expand Up @@ -187,8 +187,9 @@ static int SortDelegate( XmlNode a, XmlNode b )
// Sorting attributes, if specified, is done before node sorting happens..

if (result == 0) {
var col1 = (a.Attributes.Count >= b.Attributes.Count) ? a.Attributes : b.Attributes;
var col2 = (a.Attributes.Count >= b.Attributes.Count) ? b.Attributes : a.Attributes;
// We need to respect return values of -1, 0 and 1, see test file h
var col1 = a.Attributes;
var col2 = b.Attributes;

for (var i = 0; i < col1.Count; i++) {
if (i < col2.Count) {
Expand All @@ -209,6 +210,14 @@ static int SortDelegate( XmlNode a, XmlNode b )
}
}

// We need to respect return values of -1, 0 and 1, see test file h
if (result == 0 && col1.Count < col2.Count) {
return -1;
}

if (result == 0) {
result = string.Compare(a.InnerXml, b.InnerXml, sort_node_comp);
}
// If we get here, that means that the node's attributes (and values) all match..
// TODO: Should we go down into the child node collections for sorting?
// See example `c.xml`..
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions sortxmlXUnitProject/UnitTestAll.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Xunit;
using sortxml;
using System.Reflection;
using System.IO;
using System.Linq;

namespace sortxmlXUnitProject
{
public class UnitTestAll
{
static string GetTestFilesPath()
{
var codeBaseUrl = new Uri(Assembly.GetExecutingAssembly().CodeBase);
var codeBasePath = Uri.UnescapeDataString(codeBaseUrl.AbsolutePath);
var dirPath = Path.GetFullPath(Path.GetDirectoryName(codeBasePath) + @"/../../../test_files/");
return dirPath;
}

static bool CompareFiles(string baseFilePath, string generatedTestFilePath)
{
var testData = File.ReadAllBytes(generatedTestFilePath);
var baseData = File.ReadAllBytes(baseFilePath);
return testData.SequenceEqual(baseData);
}

[Fact]
public void TestAllFiles()
{
var testFilesPath = GetTestFilesPath();
foreach(var file in Directory.GetFiles(testFilesPath, "*.xml"))
{
if (!file.Contains("_handsorted.xml") && !file.Contains("_sorted.xml") && !file.Contains("_test.xml"))
{
var name = Path.GetFileNameWithoutExtension(file);
var resultFile = testFilesPath + name + "_test.xml";
var baseFile = testFilesPath + name + "_sorted.xml";
sortxml.Program.Main(new string[] { "--sort", file, resultFile});
Assert.True(CompareFiles(baseFile, resultFile), "Comparing " + file);
File.Delete(resultFile);
}
}
}
}
}
26 changes: 26 additions & 0 deletions sortxmlXUnitProject/sortxmlXUnitProject.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\sortxml\sortxml.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Outputs: return false</Description>
<Description>Outputs: Loc.GetText("")</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>rf</Shortcut>
<Shortcut>ll</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
<Title>return false</Title>
<Title>locgettext</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[return false;$end$]]></Code>
<Code Language="csharp"><![CDATA[Loc.GetText("$end$")]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Outputs: Loc.GetText("")</Description>
<Description>Outputs: return false</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>ll</Shortcut>
<Shortcut>rf</Shortcut>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>locgettext</Title>
<Title>return false</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[Loc.GetText("$end$")]]></Code>
<Code Language="csharp"><![CDATA[return false;$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Check if object is null (if stmt)</Description>
<Shortcut>isnull</Shortcut>
<Description>Check if object is not null (if stmt)</Description>
<Shortcut>notnull</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Check if object is null (if stmt)</Title>
<Title>Check if object is not null (if stmt)</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[if (null == $obj$) {
<Code Language="csharp"><![CDATA[if (null != $obj$) {
$end$
}]]></Code>
<Declarations>
Expand All @@ -26,15 +26,15 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Check if object is not null (if stmt)</Description>
<Shortcut>notnull</Shortcut>
<Description>Check if object is null (if stmt)</Description>
<Shortcut>isnull</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Check if object is not null (if stmt)</Title>
<Title>Check if object is null (if stmt)</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[if (null != $obj$) {
<Code Language="csharp"><![CDATA[if (null == $obj$) {
$end$
}]]></Code>
<Declarations>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Check if string is null or empty</Description>
<Shortcut>isnull</Shortcut>
<Description>Check if string is non-null and not empty</Description>
<Shortcut>notnull</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Check if string is null or empty</Title>
<Title>Check if string is not null and not empty</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[null == $str$ || 0 == $str$.Length$end$]]></Code>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < $str$.Length$end$]]></Code>
<Declarations>
<Literal>
<Default>strValue</Default>
Expand All @@ -24,15 +24,15 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Check if string is non-null and not empty</Description>
<Description>Check if string is not null and not empty (trimmed)</Description>
<Shortcut>notnull</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Check if string is not null and not empty</Title>
<Title>Check if string is not null and not empty (trimmed)</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < $str$.Length$end$]]></Code>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < ($str$ = $str$.Trim()).Length$end$]]></Code>
<Declarations>
<Literal>
<Default>strValue</Default>
Expand Down Expand Up @@ -66,15 +66,15 @@
<CodeSnippet Format="1.0.0">
<Header>
<Author>Kody Brown</Author>
<Description>Check if string is not null and not empty (trimmed)</Description>
<Shortcut>notnull</Shortcut>
<Description>Check if string is null or empty</Description>
<Shortcut>isnull</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Check if string is not null and not empty (trimmed)</Title>
<Title>Check if string is null or empty</Title>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < ($str$ = $str$.Trim()).Length$end$]]></Code>
<Code Language="csharp"><![CDATA[null == $str$ || 0 == $str$.Length$end$]]></Code>
<Declarations>
<Literal>
<Default>strValue</Default>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<First1>
<Snippet>
<Code Language="csharp"><![CDATA[null == $str$ || 0 == $str$.Length$end$]]></Code>
</Snippet>
<Tag1>
<Title>Check if string is null or empty</Title>
</Tag1>
</First1>
<First5>
<Snippet>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < $str$.Length$end$]]></Code>
</Snippet>
<Tag1>
<Title>Check if string is not null and not empty</Title>
</Tag1>
</First5>
<First>
<Snippet>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < ($str$ = $str$.Trim()).Length$end$]]></Code>
Expand All @@ -32,4 +16,20 @@
<Title>Check if string is null or empty (trimmed)</Title>
</Tag1>
</First>
<First1>
<Snippet>
<Code Language="csharp"><![CDATA[null == $str$ || 0 == $str$.Length$end$]]></Code>
</Snippet>
<Tag1>
<Title>Check if string is null or empty</Title>
</Tag1>
</First1>
<First5>
<Snippet>
<Code Language="csharp"><![CDATA[null != $str$ && 0 < $str$.Length$end$]]></Code>
</Snippet>
<Tag1>
<Title>Check if string is not null and not empty</Title>
</Tag1>
</First5>
</CodeSnippets>
29 changes: 29 additions & 0 deletions sortxmlXUnitProject/test_files/h.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<tests xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<test b="2">4</test>
<test b="2">1</test>
<test b="1">2</test>
<test b="2">3</test>
<test b="2">
<hello>
yes
</hello>
</test>
<test b="2">
<hello>
yes2
</hello>
<hello>
yes
</hello>
</test>
<test b="2">
<hello>
no
</hello>
</test>
<test>1</test>
<test>3</test>
<test>2</test>
<test a="2">3</test>
</tests>
Loading