Skip to content

Commit

Permalink
Changed logic for .IsSdk() extension method:
Browse files Browse the repository at this point in the history
For null or empty ProjectItem.fullPath it will only use information from ProjectType.

Also added .IsVc()
  • Loading branch information
3F committed Jan 31, 2022
1 parent 7d32e79 commit 0ec9602
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
18 changes: 15 additions & 3 deletions MvsSln/Extensions/ProjectItemExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,32 @@ public static class ProjectItemExtension
/// </summary>
public static bool IsVb(this ProjectItem prj) => prj.EpType == ProjectType.Vb || prj.EpType == ProjectType.VbSdk;

/// <summary>
/// Is it C++ ? Checking <see cref="ProjectType.Vc"/> type.
/// </summary>
public static bool IsVc(this ProjectItem prj) => prj.EpType == ProjectType.Vc;

/// <summary>
/// While <see cref="ProjectType"/> cannot inform the actual use of the modern Sdk style in projects,
/// current method will try to detect this by using the extended logic:
/// https://github.com/dotnet/project-system/blob/master/docs/opening-with-new-project-system.md
/// </summary>
/// <returns>Returns false if this is a legacy style or if <see cref="ProjectItem.fullPath"/> is null or empty or path is not accessible. Otherwise true.</returns>
/// <returns>Returns false if this is a legacy style or if <see cref="ProjectItem.fullPath"/> is not accessible. Otherwise true.</returns>
/// <remarks>For null or empty <see cref="ProjectItem.fullPath"/> it will only use information from <see cref="ProjectType"/>.</remarks>
/// <exception cref="System.Xml.XmlException">Requires valid XML data.</exception>
public static bool IsSdk(this ProjectItem prj)
{
if(string.IsNullOrWhiteSpace(prj.fullPath) || !File.Exists(prj.fullPath))
if(string.IsNullOrEmpty(prj.fullPath))
{
return false;
return prj.EpType switch
{
ProjectType.CsSdk or ProjectType.FsSdk or ProjectType.VbSdk => true,
_ => false
};
}

if(!File.Exists(prj.fullPath)) return false;

/*
Sdk-style, if:
Expand Down
69 changes: 69 additions & 0 deletions MvsSlnTest/Extensions/ProjectItemExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using net.r_eg.MvsSln.Core;
using net.r_eg.MvsSln.Extensions;
using Xunit;

namespace MvsSlnTest.Extensions
{
public class ProjectItemExtensionTest
{
[Theory]
[InlineData(true, ProjectType.CsSdk)]
[InlineData(true, ProjectType.FsSdk)]
[InlineData(true, ProjectType.VbSdk)]
[InlineData(false, ProjectType.Cs)]
[InlineData(false, ProjectType.Fs)]
[InlineData(false, ProjectType.Vb)]
[InlineData(false, ProjectType.Vc)]
public void CheckProjectTypesTest1(bool flag, ProjectType type)
{
ProjectItem prj = new("", type);
Assert.True(prj.IsSdk() == flag);
}

[Theory]
[InlineData(ProjectType.Cs)]
[InlineData(ProjectType.CsSdk)]
public void CheckProjectTypeCsTest(ProjectType type)
{
ProjectItem prj = new("", type);
Assert.False(prj.IsVc());
Assert.True(prj.IsCs());
Assert.False(prj.IsFs());
Assert.False(prj.IsVb());
}

[Theory]
[InlineData(ProjectType.Fs)]
[InlineData(ProjectType.FsSdk)]
public void CheckProjectTypeFsTest(ProjectType type)
{
ProjectItem prj = new("", type);
Assert.False(prj.IsVc());
Assert.False(prj.IsCs());
Assert.True(prj.IsFs());
Assert.False(prj.IsVb());
}

[Theory]
[InlineData(ProjectType.Vb)]
[InlineData(ProjectType.VbSdk)]
public void CheckProjectTypeVbTest(ProjectType type)
{
ProjectItem prj = new("", type);
Assert.False(prj.IsVc());
Assert.False(prj.IsCs());
Assert.False(prj.IsFs());
Assert.True(prj.IsVb());
}

[Fact]
public void CheckProjectTypeVcTest()
{
ProjectItem prj = new("", ProjectType.Vc);
Assert.True(prj.IsVc());
Assert.False(prj.IsCs());
Assert.False(prj.IsFs());
Assert.False(prj.IsVb());
}
}
}

0 comments on commit 0ec9602

Please sign in to comment.