Skip to content

Commit

Permalink
Add IsIndexable
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Sep 18, 2023
1 parent 2fc21f1 commit fc0b685
Show file tree
Hide file tree
Showing 38 changed files with 788 additions and 694 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NetFabric.CodeAnalysis.CSharp.UnitTests;
public partial class ITypeSymbolExtensionsTests
{
[Theory]
[MemberData(nameof(DataSets.InstanceMethods), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.InstanceMethods), MemberType = typeof(EnumerableDataSets))]
public void GetMethod_Should_ReturnMethod(string methodName, Type[] parameters)
{
// Arrange
Expand All @@ -28,7 +28,7 @@ public void GetMethod_Should_ReturnMethod(string methodName, Type[] parameters)
}

[Theory]
[MemberData(nameof(DataSets.ExplicitInstanceMethods), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.ExplicitInstanceMethods), MemberType = typeof(EnumerableDataSets))]
public void GetMethod_With_ExplicitOrStaticMethods_Should_ReturnNull(string methodName, Type[] parameters)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace NetFabric.CodeAnalysis.CSharp.UnitTests;
public partial class ITypeSymbolExtensionsTests
{
[Theory]
[MemberData(nameof(DataSets.InstanceProperties), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.InstanceProperties), MemberType = typeof(EnumerableDataSets))]
public void GetProperty_Should_ReturnProperty(string propertyName, Type propertyType)
{
// Arrange
Expand All @@ -24,7 +24,7 @@ public void GetProperty_Should_ReturnProperty(string propertyName, Type property
}

[Theory]
[MemberData(nameof(DataSets.ExplicitInstanceProperties), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.ExplicitInstanceProperties), MemberType = typeof(EnumerableDataSets))]
public void GetProperty_With_ExplicitOrStaticProperties_Should_ReturnNull(string propertyName)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace NetFabric.CodeAnalysis.CSharp.UnitTests;
public partial class ITypeSymbolExtensionsTests
{
[Theory]
[MemberData(nameof(DataSets.AsyncEnumerables), MemberType = typeof(DataSets))]
[MemberData(nameof(DataSets.CodeAnalysisAsyncEnumerables), MemberType = typeof(DataSets))]
public void IsAsyncEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.AsyncEnumerableTestData enumerableTestData)
[MemberData(nameof(EnumerableDataSets.AsyncEnumerables), MemberType = typeof(EnumerableDataSets))]
[MemberData(nameof(EnumerableDataSets.CodeAnalysisAsyncEnumerables), MemberType = typeof(EnumerableDataSets))]
public void IsAsyncEnumerable_Should_ReturnTrue(Type enumerableType, EnumerableDataSets.AsyncEnumerableTestData enumerableTestData)
{
// Arrange
var compilation = Utils.Compile(
Expand All @@ -20,12 +20,12 @@ public void IsAsyncEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.As
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsAsyncEnumerable(compilation, out var enumerableSymbols, out var errors);
var result = typeSymbol.IsAsyncEnumerable(compilation, out var enumerableSymbols, out var error);

// Assert
Assert.True(result, errors.ToString());
Assert.True(result, error.ToString());
Assert.NotNull(enumerableSymbols);
Assert.Equal(Errors.None, errors);
Assert.Equal(IsAsyncEnumerableError.None, error);

var getAsyncEnumerator = enumerableSymbols!.GetAsyncEnumerator;
Assert.NotNull(getAsyncEnumerator);
Expand Down Expand Up @@ -66,8 +66,8 @@ public void IsAsyncEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.As
}

[Theory]
[MemberData(nameof(DataSets.InvalidAsyncEnumerables), MemberType = typeof(DataSets))]
public void IsAsyncEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enumerableType, Type? getAsyncEnumeratorDeclaringType, int getAsyncEnumeratorParametersCount, Type? currentDeclaringType, Type? moveNextAsyncDeclaringType, Type? itemType)
[MemberData(nameof(EnumerableDataSets.InvalidAsyncEnumerables), MemberType = typeof(EnumerableDataSets))]
public void IsAsyncEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enumerableType, IsAsyncEnumerableError expectedError)
{
// Arrange
var compilation = Utils.Compile(
Expand All @@ -76,28 +76,10 @@ public void IsAsyncEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enume
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsAsyncEnumerable(compilation, out _, out var errors);
var result = typeSymbol.IsAsyncEnumerable(compilation, out _, out var error);

// Assert
Assert.False(result);

if (getAsyncEnumeratorDeclaringType is null)
{
Assert.True(errors.HasFlag(Errors.MissingGetEnumerator));
}
else
{
Assert.False(errors.HasFlag(Errors.MissingGetEnumerator));

if (currentDeclaringType is null)
Assert.True(errors.HasFlag(Errors.MissingCurrent));
else
Assert.False(errors.HasFlag(Errors.MissingCurrent));

if (moveNextAsyncDeclaringType is null)
Assert.True(errors.HasFlag(Errors.MissingMoveNext));
else
Assert.False(errors.HasFlag(Errors.MissingMoveNext));
}
Assert.Equal(expectedError, error);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using NetFabric.CSharp.TestData;
using System;
using System.Collections;
using Xunit;

namespace NetFabric.CodeAnalysis.CSharp.UnitTests;

public partial class ITypeSymbolExtensionsTests
{
[Theory]
[MemberData(nameof(DataSets.Arrays), MemberType = typeof(DataSets))]
[MemberData(nameof(DataSets.Enumerables), MemberType = typeof(DataSets))]
[MemberData(nameof(DataSets.CodeAnalysisEnumerables), MemberType = typeof(DataSets))]
public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.EnumerableTestData enumerableTestData)
[MemberData(nameof(EnumerableDataSets.Arrays), MemberType = typeof(EnumerableDataSets))]
[MemberData(nameof(EnumerableDataSets.Enumerables), MemberType = typeof(EnumerableDataSets))]
[MemberData(nameof(EnumerableDataSets.CodeAnalysisEnumerables), MemberType = typeof(EnumerableDataSets))]
public void IsEnumerable_Should_ReturnTrue(Type enumerableType, EnumerableDataSets.EnumerableTestData enumerableTestData)
{
// Arrange
var compilation = Utils.Compile(
Expand All @@ -22,16 +21,16 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsEnumerable(compilation, out var enumerableSymbols, out var errors);
var result = typeSymbol.IsEnumerable(compilation, out var enumerableSymbols, out var error);

// Assert
Assert.True(result, errors.ToString());
Assert.True(result, error.ToString());
Assert.NotNull(enumerableSymbols);
Assert.Equal(Errors.None, errors);
Assert.Equal(IsEnumerableError.None, error);

var getEnumerator = enumerableSymbols!.GetEnumerator;
Assert.NotNull(getEnumerator);
Assert.Equal(nameof(IEnumerable.GetEnumerator), getEnumerator.Name);
Assert.Equal(NameOf.GetEnumerator, getEnumerator.Name);
Assert.Equal(enumerableTestData.GetEnumeratorDeclaringType.Name, getEnumerator.ContainingType.MetadataName);

var enumeratorSymbols = enumerableSymbols.EnumeratorSymbols;
Expand All @@ -41,7 +40,7 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera

var current = enumeratorSymbols.Current;
Assert.NotNull(current);
Assert.Equal(nameof(IEnumerator.Current), current.Name);
Assert.Equal(NameOf.Current, current.Name);
Assert.Equal(enumeratorTestData.CurrentDeclaringType.Name, current.ContainingType.MetadataName);
if (current is {ReturnsByRef:true} or {ReturnsByRefReadonly:true})
Assert.Equal(enumeratorTestData.ItemType.Name, current.Type.MetadataName + '&');
Expand All @@ -50,7 +49,7 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera

var moveNext = enumeratorSymbols.MoveNext;
Assert.NotNull(moveNext);
Assert.Equal(nameof(IEnumerator.MoveNext), moveNext.Name);
Assert.Equal(NameOf.MoveNext, moveNext.Name);
Assert.Equal(enumeratorTestData.MoveNextDeclaringType.Name, moveNext.ContainingType.MetadataName);
Assert.Empty(enumeratorSymbols.MoveNext.Parameters);

Expand All @@ -62,7 +61,7 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera
else
{
Assert.NotNull(reset);
Assert.Equal(nameof(IEnumerator.Reset), reset!.Name);
Assert.Equal(NameOf.Reset, reset!.Name);
Assert.Equal(enumeratorTestData.ResetDeclaringType.Name, reset.ContainingType.MetadataName);
Assert.Empty(reset.Parameters);
}
Expand All @@ -75,7 +74,7 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera
else
{
Assert.NotNull(dispose);
Assert.Equal(nameof(IDisposable.Dispose), dispose!.Name);
Assert.Equal(NameOf.Dispose, dispose!.Name);
Assert.Equal(enumeratorTestData.DisposeDeclaringType.Name, dispose.ContainingType.MetadataName);
Assert.Empty(dispose.Parameters);
}
Expand All @@ -85,8 +84,8 @@ public void IsEnumerable_Should_ReturnTrue(Type enumerableType, DataSets.Enumera
}

[Theory]
[MemberData(nameof(DataSets.InvalidEnumerables), MemberType = typeof(DataSets))]
public void IsEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enumerableType, Type? getEnumeratorDeclaringType, Type? currentDeclaringType, Type moveNextDeclaringType, Type itemType)
[MemberData(nameof(EnumerableDataSets.InvalidEnumerables), MemberType = typeof(EnumerableDataSets))]
public void IsEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enumerableType, IsEnumerableError expectedError)
{
// Arrange
var compilation = Utils.Compile(
Expand All @@ -95,28 +94,10 @@ public void IsEnumerable_With_MissingFeatures_Should_ReturnFalse(Type enumerable
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsEnumerable(compilation, out _, out var errors);
var result = typeSymbol.IsEnumerable(compilation, out _, out var error);

// Assert
Assert.False(result);

if (getEnumeratorDeclaringType is null)
{
Assert.True(errors.HasFlag(Errors.MissingGetEnumerator));
}
else
{
Assert.False(errors.HasFlag(Errors.MissingGetEnumerator));

if (currentDeclaringType is null)
Assert.True(errors.HasFlag(Errors.MissingCurrent));
else
Assert.False(errors.HasFlag(Errors.MissingCurrent));

if (moveNextDeclaringType is null)
Assert.True(errors.HasFlag(Errors.MissingMoveNext));
else
Assert.False(errors.HasFlag(Errors.MissingMoveNext));
}
Assert.Equal(expectedError, error);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using NetFabric.CSharp.TestData;
using System;
using Xunit;

namespace NetFabric.CodeAnalysis.CSharp.UnitTests;

public partial class ITypeSymbolExtensionsTests
{
[Theory]
[MemberData(nameof(IndexableDataSets.Indexables), MemberType = typeof(IndexableDataSets))]
public void IsIndexable_Should_ReturnTrue(Type enumerableType, IndexableDataSets.IndexableTestData testData)
{
// Arrange
var compilation = Utils.Compile(
@"TestData/Indexables.cs"
);
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsIndexable(out var indexableSymbols, out var error);

// Assert
Assert.True(result, error.ToString());
Assert.NotNull(indexableSymbols);
Assert.Equal(IsIndexableError.None, error);

var indexer = indexableSymbols!.Indexer;
Assert.NotNull(indexer);
Assert.Equal(NameOf.Indexer, indexer.Name);
Assert.Equal(testData.IndexerDeclaringType.Name, indexer.ContainingType.MetadataName);

var countOrLength = indexableSymbols!.CountOrLength;
Assert.NotNull(countOrLength);
Assert.True(countOrLength.Name == NameOf.Count || countOrLength.Name == NameOf.Length);
Assert.Equal(testData.CountOrLengthDeclaringType.Name, countOrLength.ContainingType.MetadataName);
}

[Theory]
[MemberData(nameof(IndexableDataSets.InvalidIndexables), MemberType = typeof(IndexableDataSets))]
public void IsIndexable_With_MissingFeatures_Should_ReturnFalse(Type enumerableType, IsIndexableError expectedError)
{
// Arrange
var compilation = Utils.Compile(
@"TestData/Indexables.cs"
);
var typeSymbol = compilation.GetTypeSymbol(enumerableType);

// Act
var result = typeSymbol.IsIndexable(out _, out var error);

// Assert
Assert.False(result);
Assert.Equal(expectedError, error);
}
}
3 changes: 0 additions & 3 deletions NetFabric.CSharp.UnitTests/NetFabric.CSharp.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<Compile Update="TestData\AsyncEnumerables.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="TestData\AsyncEnumerators.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="TestData\AsyncDataSets.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace NetFabric.Reflection.CSharp.UnitTests;
public partial class TypeExtensionsTests
{
[Theory]
[MemberData(nameof(DataSets.InstanceMethods), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.InstanceMethods), MemberType = typeof(EnumerableDataSets))]
public void GetPublicMethod_Should_ReturnMethod(string methodName, Type[] parameters)
{
// Arrange
Expand All @@ -26,7 +26,7 @@ public void GetPublicMethod_Should_ReturnMethod(string methodName, Type[] parame
}

[Theory]
[MemberData(nameof(DataSets.ExplicitInstanceMethods), MemberType = typeof(DataSets))]
[MemberData(nameof(EnumerableDataSets.ExplicitInstanceMethods), MemberType = typeof(EnumerableDataSets))]
public void GetPublicMethod_With_ExplicitOrStaticMethods_Should_ReturnNull(string methodName, Type[] parameters)
{
// Arrange
Expand Down
Loading

0 comments on commit fc0b685

Please sign in to comment.