-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored display path management logic.
Extracted business logic. Added unit tests.
- Loading branch information
1 parent
31b1ce6
commit 342679c
Showing
15 changed files
with
485 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.