Skip to content

Commit

Permalink
[GH-54] - proper way of finding InternalsVisibleTo attribute (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak authored and dtchepak committed Nov 22, 2018
1 parent e0edb93 commit cbeafd3
Show file tree
Hide file tree
Showing 20 changed files with 297 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/NSubstitute.Analyzers.Shared/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ public static bool CanBeSetuped(this ISymbol symbol)

public static bool InternalsVisibleToProxyGenerator(this ISymbol typeSymbol)
{
var internalsVisibleToAttribute = typeSymbol.ContainingAssembly.GetAttributes()
.FirstOrDefault(att =>
att.AttributeClass.ToString() == MetadataNames.InternalsVisibleToAttributeFullTypeName);

return internalsVisibleToAttribute != null &&
internalsVisibleToAttribute.ConstructorArguments.Any(arg =>
arg.Value.ToString() == MetadataNames.CastleDynamicProxyGenAssembly2Name);
return typeSymbol.ContainingAssembly != null &&
typeSymbol.ContainingAssembly.GetAttributes()
.Any(att => att.AttributeClass.ToString() == MetadataNames.InternalsVisibleToAttributeFullTypeName &&
att.ConstructorArguments.Any(arg => arg.Value.ToString() == MetadataNames.CastleDynamicProxyGenAssembly2Name));
}

public static string ToMinimalMethodString(this ISymbol symbol, SemanticModel semanticModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,36 @@ public void Test()
}";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"using NSubstitute;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
{
internal class Bar
{
}
}
public class FooTests
{
public void Test()
{
var substitute = Substitute.For<Foo>();
}
}
}";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,36 @@ public void Test()
}";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"using NSubstitute;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
{
internal class Bar
{
}
}
public class FooTests
{
public void Test()
{
var substitute = Substitute.For(new[] {typeof(Foo)}, null);
}
}
}";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,36 @@ public void Test()
}";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"using NSubstitute;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
{
internal class Bar
{
}
}
public class FooTests
{
public void Test()
{
var substitute = Substitute.ForPartsOf<Foo>();
}
}
}";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,36 @@ public void Test()
}";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"using NSubstitute.Core;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
{
internal class Bar
{
}
}
public class FooTests
{
public void Test()
{
var substitute = SubstitutionContext.Current.SubstituteFactory.Create(new[] {typeof(Foo)}, null);
}
}
}";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,36 @@ public void Test()
}";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"using NSubstitute.Core;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
{
internal class Bar
{
}
}
public class FooTests
{
public void Test()
{
var substitute = SubstitutionContext.Current.SubstituteFactory.CreatePartial(new[] {typeof(Foo)}, null);
}
}
}";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public abstract class SubstituteForInternalMemberCodeFixVerifier : CSharpCodeFix

public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenUsedWithPublicClass();

public abstract Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2();

protected override DiagnosticAnalyzer GetDiagnosticAnalyzer()
{
return new SubstituteAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,9 @@ public override async Task
{
var source = @"using NSubstitute;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,9 @@ public override async Task ReturnsNoDiagnostic_WhenUsedWithInternalClass_AndInte
{
var source = @"using NSubstitute;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ public override async Task ReturnsNoDiagnostic_WhenUsedWithInternalClass_AndInte
{
var source = @"using NSubstitute;
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo(""OtherFirstAssembly"")]
[assembly: InternalsVisibleTo(""DynamicProxyGenAssembly2"")]
[assembly: InternalsVisibleTo(""OtherSecondAssembly"")]
namespace MyNamespace
{
internal class Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface ISubstituteForInternalMemberCodeFixVerifier
Task AppendsInternalsVisibleTo_WhenUsedWithNestedInternalClass();

Task DoesNot_AppendsInternalsVisibleTo_WhenUsedWithPublicClass();

Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,29 @@ End Namespace
";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"Imports NSubstitute.Core
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")>
Namespace MyNamespace
Friend Class Foo
Friend Class Bar
End Class
End Class
Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.For(Of Foo)()
End Sub
End Class
End Namespace";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,29 @@ End Namespace
";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"Imports NSubstitute.Core
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")>
Namespace MyNamespace
Friend Class Foo
Friend Class Bar
End Class
End Class
Public Class FooTests
Public Sub Test()
Dim substitute = NSubstitute.Substitute.[For]({GetType(Foo)}, Nothing)
End Sub
End Class
End Namespace";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,29 @@ End Namespace
";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"Imports NSubstitute.Core
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")>
Namespace MyNamespace
Friend Class Foo
Friend Class Bar
End Class
End Class
Public Class FooTests
Public Sub Test()
NSubstitute.Substitute.ForPartsOf(Of Foo)()
End Sub
End Class
End Namespace";

await VerifyFix(oldSource, oldSource);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,29 @@ End Namespace
";
await VerifyFix(oldSource, oldSource);
}

[Fact]
public override async Task DoesNot_AppendsInternalsVisibleTo_WhenInternalsVisibleToAppliedToDynamicProxyGenAssembly2()
{
var oldSource = @"Imports NSubstitute.Core
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherFirstAssembly"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""DynamicProxyGenAssembly2"")>
<Assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""OtherSecondAssembly"")>
Namespace MyNamespace
Friend Class Foo
Friend Class Bar
End Class
End Class
Public Class FooTests
Public Sub Test()
Dim substitute = SubstitutionContext.Current.SubstituteFactory.Create({GetType(Foo)}, Nothing)
End Sub
End Class
End Namespace";

await VerifyFix(oldSource, oldSource);
}
}
}
Loading

0 comments on commit cbeafd3

Please sign in to comment.