Skip to content

Commit

Permalink
inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cvietor committed Jun 15, 2017
1 parent e2d2bae commit 8e674d9
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ _TeamCity*
*.coveragexml

# NCrunch
*.ncrunch*
_NCrunch_*
.*crunch*.local.xml
*.crunch.xml
*.ncrunchsolution*
nCrunchTemp_*

# MightyMoose
Expand Down
75 changes: 75 additions & 0 deletions PathsToTree.Tests/PathsToTree.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PathsToTree.Tests</RootNamespace>
<AssemblyName>PathsToTree.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</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="FluentAssertions, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions.Core, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ProgramTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PathsToTree\PathsToTree.csproj">
<Project>{A59510C3-7DB7-44A1-BD63-BD9731576048}</Project>
<Name>PathsToTree</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
77 changes: 77 additions & 0 deletions PathsToTree.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using NUnit.Framework;

namespace PathsToTree.Tests
{
[TestFixture]
public class ProgramTests
{
[Test]
public void Convert_Should_Throw_Exception_When_Any_Path_Starts_With_Delimiter_Symbol()
{
var paths = new[]
{
"/subFolder1",
"subFolder1/subsubfolder1a",
"subFolder2",
"/subFolder2/subsubfolder1b",
};

var sut = new PathToTreeConverter();
Assert.Throws<ArgumentException>(() => sut.Convert(paths));
}

[Test]
public void Set_Delimiter_Symbol_Should_Succeed()
{
char delimiterSymbol = '%';

var sut = new PathToTreeConverter();
sut.SetDelimiterSymbol(delimiterSymbol);

Assert.That(sut.DelimiterSymbol, Is.EqualTo('%'));
}

[Test]
public void Convert_Should_Have_Correct_Root_Count()
{
var paths = new[]
{
"subFolder1",
"subFolder1/subsubfolder1a",
"subFolder2",
"subFolder2/subsubfolder1b",
};

var sut = new PathToTreeConverter();

var result = sut.Convert(paths);

Assert.That(result, Has.Count.EqualTo(2));
}

[Test]
public void Convert_Should_Have_Correct_Children_Count()
{
var paths = new[]
{
"subFolder1",
"subFolder1/subsubfolder1a",
"subFolder2",
"subFolder2/subsubfolder1b",
"subFolder2/subsubfolder2b",
"subfolder3",
"subfolder4/",
};

var sut = new PathToTreeConverter();

var result = sut.Convert(paths);

Assert.That(result[0].Children, Has.Count.EqualTo(1));
Assert.That(result[1].Children, Has.Count.EqualTo(2));
Assert.That(result[2].Children, Has.Count.EqualTo(0));
Assert.That(result[3].Children, Has.Count.EqualTo(0));
}
}
}
36 changes: 36 additions & 0 deletions PathsToTree.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PathsToTree.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PathsToTree.Tests")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0cc9efe0-6b00-4d73-b926-c6caa9dae4c6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions PathsToTree.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentAssertions" version="4.19.2" targetFramework="net452" />
<package id="NUnit" version="3.7.1" targetFramework="net452" />
</packages>
28 changes: 28 additions & 0 deletions PathsToTree.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PathsToTree", "PathsToTree\PathsToTree.csproj", "{A59510C3-7DB7-44A1-BD63-BD9731576048}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PathsToTree.Tests", "PathsToTree.Tests\PathsToTree.Tests.csproj", "{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A59510C3-7DB7-44A1-BD63-BD9731576048}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A59510C3-7DB7-44A1-BD63-BD9731576048}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A59510C3-7DB7-44A1-BD63-BD9731576048}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A59510C3-7DB7-44A1-BD63-BD9731576048}.Release|Any CPU.Build.0 = Release|Any CPU
{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CC9EFE0-6B00-4D73-B926-C6CAA9DAE4C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions PathsToTree/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
72 changes: 72 additions & 0 deletions PathsToTree/PathToTreeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace PathsToTree
{
public class PathToTreeConverter
{
public PathToTreeConverter()
{
DelimiterSymbol = System.Convert.ToChar("/");
}

public char DelimiterSymbol { get; private set; }

public IList<TreeElement> Convert(string[] paths)
{
return BuildTree(paths).ToList();
}

public void SetDelimiterSymbol(char symbol)
{
DelimiterSymbol = symbol;
}

private IList<TreeElement> BuildTree(IList<string> paths)
{
Validate(paths);

var list = new List<TreeElement>();

var rootPaths = GroupByRootPaths(paths);

foreach (var rootPath in rootPaths)
{
var childPaths = GetChildPaths(rootPath);
var childElements = BuildTree(childPaths);

list.Add(new TreeElement
{
Name = rootPath.Key,
Children = childElements
});
}
return list;
}

private void Validate(IList<string> paths)
{
if (paths == null) throw new ArgumentNullException(nameof(paths));

if (paths.Any(path => path.StartsWith(DelimiterSymbol.ToString())))
throw new ArgumentException($"paths are not allowed to begin with delimiter symbol: {DelimiterSymbol}");
}

private IList<string> GetChildPaths(IGrouping<string, string> rootPath)
{
var result = rootPath
.Where(path => !path.Equals(rootPath.Key))
.Where(path => !path.Equals(rootPath.Key + DelimiterSymbol))
.Select(path => path.Replace(rootPath.Key + DelimiterSymbol, string.Empty))
.ToList();

return result;
}

private IEnumerable<IGrouping<string, string>> GroupByRootPaths(IEnumerable<string> paths)
{
return paths.GroupBy(path => path.Split(DelimiterSymbol)[0]);
}
}
}
62 changes: 62 additions & 0 deletions PathsToTree/PathsToTree.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A59510C3-7DB7-44A1-BD63-BD9731576048}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>PathsToTree</RootNamespace>
<AssemblyName>PathsToTree</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<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' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="PathToTreeConverter.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TreeElement.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit 8e674d9

Please sign in to comment.