Skip to content

Commit

Permalink
Refactored display path management logic.
Browse files Browse the repository at this point in the history
Extracted business logic.
Added unit tests.
  • Loading branch information
DarkDaskin committed Jun 14, 2019
1 parent 31b1ce6 commit 342679c
Show file tree
Hide file tree
Showing 15 changed files with 485 additions and 117 deletions.
162 changes: 162 additions & 0 deletions VSTabPath.Tests/DisplayPathResolverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using VSTabPath.Models;

namespace VSTabPath.Tests
{
[TestClass]
public class DisplayPathResolverTests
{
[TestMethod]
public void WhenNoDuplicateFileNames_DoNotShowPaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\3.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
models[2],
};

Assert.AreEqual(null, models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual(null, models[2].DisplayPath);
}

[TestMethod]
public void WhenDuplicateFileNames_ShowPaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\1.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
models[2],
};

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory2", models[2].DisplayPath);
}

[TestMethod]
public void WhenNewTabIsAdded_UpdatePaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\1.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
};

Assert.AreEqual(null, models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);

resolver.Add(models[2]);

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory2", models[2].DisplayPath);
}

[TestMethod]
public void WhenNewTabIsRemoved_UpdatePaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\1.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
models[2],
};

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory2", models[2].DisplayPath);

resolver.Remove(models[2]);

Assert.AreEqual(null, models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
}

[TestMethod]
public void WhenNewTabIsRenamed_UpdatePaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\3.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
models[2],
};

Assert.AreEqual(null, models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual(null, models[2].DisplayPath);

models[2].FullPath = @"c:\Directory2\1.txt";

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory2", models[2].DisplayPath);
}

[TestMethod]
public void WhenNewTabIsMoved_UpdatePaths()
{
var models = new[]
{
new TabModel(@"c:\Directory1\1.txt"),
new TabModel(@"c:\Directory1\2.txt"),
new TabModel(@"c:\Directory2\1.txt"),
};

var resolver = new DisplayPathResolver
{
models[0],
models[1],
models[2],
};

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory2", models[2].DisplayPath);

models[2].FullPath = @"c:\Directory3\1.txt";

Assert.AreEqual("Directory1", models[0].DisplayPath);
Assert.AreEqual(null, models[1].DisplayPath);
Assert.AreEqual("Directory3", models[2].DisplayPath);
}
}
}
20 changes: 20 additions & 0 deletions VSTabPath.Tests/Properties/AssemblyInfo.cs
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("VSTabPath.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VSTabPath.Tests")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("13611075-94b6-4ea9-b777-d56a83db16d6")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
75 changes: 75 additions & 0 deletions VSTabPath.Tests/VSTabPath.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="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" />
<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>{13611075-94B6-4EA9-B777-D56A83DB16D6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>VSTabPath.Tests</RootNamespace>
<AssemblyName>VSTabPath.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</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>
<TargetFrameworkProfile />
</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.3.2\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.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="DisplayPathResolverTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\VSTabPath\VSTabPath.csproj">
<Project>{e2f60519-d34f-4411-9ab7-7916fc004e9f}</Project>
<Name>VSTabPath</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>Данный проект ссылается на пакеты NuGet, отсутствующие на этом компьютере. Используйте восстановление пакетов NuGet, чтобы скачать их. Дополнительную информацию см. по адресу: http://go.microsoft.com/fwlink/?LinkID=322105. Отсутствует следующий файл: {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.3.2\build\net45\MSTest.TestAdapter.targets')" />
</Project>
5 changes: 5 additions & 0 deletions VSTabPath.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="MSTest.TestAdapter" version="1.3.2" targetFramework="net472" />
<package id="MSTest.TestFramework" version="1.3.2" targetFramework="net472" />
</packages>
10 changes: 8 additions & 2 deletions VSTabPath.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27428.2005
# Visual Studio Version 16
VisualStudioVersion = 16.0.29009.5
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSTabPath", "VSTabPath\VSTabPath.csproj", "{E2F60519-D34F-4411-9AB7-7916FC004E9F}"
EndProject
Expand All @@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VSTabPath.Tests", "VSTabPath.Tests\VSTabPath.Tests.csproj", "{13611075-94B6-4EA9-B777-D56A83DB16D6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -20,6 +22,10 @@ Global
{E2F60519-D34F-4411-9AB7-7916FC004E9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2F60519-D34F-4411-9AB7-7916FC004E9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2F60519-D34F-4411-9AB7-7916FC004E9F}.Release|Any CPU.Build.0 = Release|Any CPU
{13611075-94B6-4EA9-B777-D56A83DB16D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13611075-94B6-4EA9-B777-D56A83DB16D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13611075-94B6-4EA9-B777-D56A83DB16D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13611075-94B6-4EA9-B777-D56A83DB16D6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
81 changes: 81 additions & 0 deletions VSTabPath/Models/DisplayPathResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace VSTabPath.Models
{
public class DisplayPathResolver : IEnumerable<TabModel>
{
private readonly List<TabModel> _models = new List<TabModel>();

public void Add(TabModel model)
{
_models.Add(model);

model.PropertyChanged += OnModelPropertyChanged;

UpdateModels(model.FileName);
}

public void Remove(TabModel model)
{
model.PropertyChanged -= OnModelPropertyChanged;

_models.Remove(model);

UpdateModels(model.FileName);
}

public IEnumerator<TabModel> GetEnumerator()
{
return _models.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

private void OnModelPropertyChanged(object sender, PropertyChangedEventArgs args)
{
if(args.PropertyName == nameof(TabModel.FullPath))
UpdateModels();
}

private void UpdateModels(string fileName)
{
var modelsToUpdate = _models.Where(m => m.FileName == fileName).ToList();
if (modelsToUpdate.Count == 1)
modelsToUpdate[0].DisplayPath = null;
else
{
foreach (var model in modelsToUpdate)
model.DisplayPath = GetParentDirectoryName(model);
}
}

private void UpdateModels()
{
var modelsByFileName = _models.ToLookup(m => m.FileName);

var modelsWithDuplicateFileName = modelsByFileName
.Where(g => g.Count() > 1)
.SelectMany(g => g);
foreach (var model in modelsWithDuplicateFileName)
model.DisplayPath = GetParentDirectoryName(model);

var modelsWithUniqueFileName = modelsByFileName
.Where(g => g.Count() == 1)
.SelectMany(g => g);
foreach (var model in modelsWithUniqueFileName)
model.DisplayPath = null;
}

private static string GetParentDirectoryName(TabModel model)
{
return Path.GetFileName(Path.GetDirectoryName(model.FullPath));
}
}
}
17 changes: 17 additions & 0 deletions VSTabPath/Models/ObservableModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;

namespace VSTabPath.Models
{
public abstract class ObservableModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Loading

0 comments on commit 342679c

Please sign in to comment.