Skip to content

Commit

Permalink
Azure Property dismiss
Browse files Browse the repository at this point in the history
  • Loading branch information
byt3n33dl3 committed Nov 25, 2024
1 parent 0a7bb3c commit 6fdbf35
Show file tree
Hide file tree
Showing 6,509 changed files with 1,411,068 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
21 changes: 21 additions & 0 deletions internal/AutoRest.CSharp.UnitTests/AutoRest.CSharp.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>annotations</Nullable>
<NoWarn>SA1649;SA1633;CS0618;CS1591;</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Core" />
<PackageReference Include="Moq" />
<PackageReference Include="nunit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Azure.ResourceManager" />

<ProjectReference Include="../../src/AutoRest.CSharp/AutoRest.CSharp.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Azure;
using NUnit.Framework;

namespace AutoRest.CSharp.Generation.Types.Tests
{
public class CSharpTypeTests
{
[TestCase(typeof(int))]
[TestCase(typeof(IList<>))]
[TestCase(typeof(IList<int>))]
[TestCase(typeof(IDictionary<,>))]
[TestCase(typeof(IDictionary<int, int>))]
[TestCase(typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>))]
public void TypesAreEqual(Type type)
{
var cst1 = new CSharpType(type);
var cst2 = new CSharpType(type);
Assert.IsTrue(cst1.Equals(cst2));
}

[TestCase(typeof(int))]
[TestCase(typeof(IList<>))]
[TestCase(typeof(IList<int>))]
[TestCase(typeof(IDictionary<,>))]
[TestCase(typeof(IDictionary<int, int>))]
[TestCase(typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>))]
public void EqualToFrameworkType(Type type)
{
var cst = new CSharpType(type);
Assert.IsTrue(cst.Equals(type));
}

[TestCase(typeof(int))]
[TestCase(typeof(IList<>))]
[TestCase(typeof(IList<int>))]
[TestCase(typeof(IDictionary<,>))]
[TestCase(typeof(IDictionary<int, int>))]
[TestCase(typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>))]
public void HashCodesAreEqual(Type type)
{
var cst1 = new CSharpType(type);
var cst2 = new CSharpType(type);
Assert.AreEqual(cst1.GetHashCode(), cst2.GetHashCode());
}

[TestCase(typeof(IList<>), new[] { typeof(int) })]
[TestCase(typeof(IReadOnlyList<>), new[] { typeof(string) })]
[TestCase(typeof(IDictionary<,>), new[] { typeof(string), typeof(string) })]
[TestCase(typeof(IReadOnlyDictionary<,>), new[] { typeof(string), typeof(int) })]
[TestCase(typeof(IDictionary<,>), new[] { typeof(string), typeof(IDictionary<string, int>) })]
public void TypesAreEqualForGenericTypes(Type type, Type[] arguments)
{
var cstArguments = arguments.Select(t => (CSharpType)t);
// pass the arguments in as an array
var cst1 = new CSharpType(type, cstArguments.ToArray());
// pass the arguments in as an `List<CSharpType>`
var cst2 = new CSharpType(type, cstArguments.ToList());
// pass the arguments in as an `ImmutableArray`
var cst3 = new CSharpType(type, cstArguments.ToImmutableArray());
// pass the arguments in as an `ImmutableList`
var cst4 = new CSharpType(type, cstArguments.ToImmutableList());

Assert.IsTrue(cst1.Equals(cst2));
Assert.IsTrue(cst2.Equals(cst3));
Assert.IsTrue(cst3.Equals(cst4));

Assert.AreEqual(cst1.GetHashCode(), cst2.GetHashCode());
Assert.AreEqual(cst2.GetHashCode(), cst3.GetHashCode());
Assert.AreEqual(cst3.GetHashCode(), cst4.GetHashCode());
}

[TestCase(typeof(int), typeof(string))]
[TestCase(typeof(int), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(int))]
[TestCase(typeof(int), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(int))]
[TestCase(typeof(IList<int>), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(IList<string>))]
[TestCase(typeof(IList<int>), typeof(ICollection<int>))]
[TestCase(typeof(Tuple<>), typeof(Tuple<,>))]
[TestCase(typeof(IDictionary<int, string>), typeof(Dictionary<,>))]
[TestCase(typeof(IDictionary<int, string>), typeof(Dictionary<int, int>))]
[TestCase(typeof(IDictionary<int, string>), typeof(Dictionary<int, string>))]
[TestCase(typeof(IDictionary<int, string>), typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>), typeof(IDictionary<IDictionary<string, int>, IDictionary<string, int>>))]
public void TypesAreNotEqual(Type type1, Type type2)
{
var cst1 = new CSharpType(type1);
var cst2 = new CSharpType(type2);
Assert.IsFalse(cst1.Equals(cst2));
}

[TestCase(typeof(int))]
[TestCase(typeof(int))]
[TestCase(typeof(IList<>))]
[TestCase(typeof(IList<int>))]
[TestCase(typeof(IList<int?>))]
[TestCase(typeof(IList<string>))]
[TestCase(typeof(IDictionary<int, string>))]
[TestCase(typeof(IDictionary<string, string>))]
[TestCase(typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<string, int?>))]
[TestCase(typeof(IDictionary<int?, string>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>))]
public void TypesAreNotEqualWhenNullibilityIsDifferent(Type type)
{
var cst = new CSharpType(type);
var nullableCst = new CSharpType(type, isNullable: true);

Assert.IsFalse(cst.Equals(nullableCst));
Assert.AreNotEqual(cst.GetHashCode(), nullableCst.GetHashCode());
}

[TestCase(typeof(int), typeof(string))]
[TestCase(typeof(int), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(int))]
[TestCase(typeof(int), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(int))]
[TestCase(typeof(IList<int>), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(IList<string>))]
[TestCase(typeof(IList<int>), typeof(ICollection<int>))]
[TestCase(typeof(IDictionary<int, string>), typeof(Dictionary<int, string>))]
[TestCase(typeof(IDictionary<int, string>), typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>), typeof(IDictionary<IDictionary<string, int>, IDictionary<string, int>>))]
public void NotEqualToFrameworkType(Type type1, Type type2)
{
var cst = new CSharpType(type1);
Assert.IsFalse(cst.Equals(type2));
}

[TestCase(typeof(int), typeof(string))]
[TestCase(typeof(int), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(int))]
[TestCase(typeof(int), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(int))]
[TestCase(typeof(IList<int>), typeof(IList<>))]
[TestCase(typeof(IList<>), typeof(IList<int>))]
[TestCase(typeof(IList<int>), typeof(IList<string>))]
[TestCase(typeof(IList<int>), typeof(ICollection<int>))]
[TestCase(typeof(IDictionary<int, string>), typeof(Dictionary<int, string>))]
[TestCase(typeof(IDictionary<int, string>), typeof(IDictionary<string, int>))]
[TestCase(typeof(IDictionary<IDictionary<int, string>, IDictionary<string, int>>), typeof(IDictionary<IDictionary<string, int>, IDictionary<string, int>>))]
public void HashCodesAreNotEqual(Type type1, Type type2)
{
var cst1 = new CSharpType(type1);
var cst2 = new CSharpType(type2);
Assert.AreNotEqual(cst1.GetHashCode(), cst2.GetHashCode());
}

[TestCase(typeof(IDictionary<int, string>), typeof(IDictionary<,>), false)]
[TestCase(typeof(IDictionary<int, IList<string>>), typeof(IDictionary<,>), false)]
[TestCase(typeof(KeyValuePair<int, string>), typeof(KeyValuePair<,>), false)]
[TestCase(typeof(KeyValuePair<int, string>), typeof(KeyValuePair<,>), true)]
public void GetGenericTypeDefinition(Type input, Type expected, bool isNullable)
{
var actual = new CSharpType(input, isNullable).GetGenericTypeDefinition();
Assert.AreEqual(new CSharpType(expected, isNullable), actual);
CollectionAssert.AreEqual(actual.Arguments, input.GetGenericTypeDefinition().GetGenericArguments().Select(p => new CSharpType(p)));
}

[TestCase]
public void GetGenericTypeDefinitionForConstructedType()
{
var actual = new CSharpType(typeof(List<>), typeof(string)).GetGenericTypeDefinition();
Assert.AreEqual(new CSharpType(typeof(List<>)), actual);
}
[TestCase(typeof(int), false)]
[TestCase(typeof(AsyncPageable<>), true)]
[TestCase(typeof(AsyncPageable<int>), true)]
[TestCase(typeof(TestAsyncPageable<>), false)]
[TestCase(typeof(TestAsyncPageable<int>), false)]
[TestCase(typeof(Pageable<>), false)]
[TestCase(typeof(Pageable<int>), false)]
[TestCase(typeof(TestPageable<>), false)]
[TestCase(typeof(TestPageable<int>), false)]
public void IsAsyncPageable(Type type, bool expected)
=> Assert.AreEqual(new CSharpType(type).IsAsyncPageable, expected);

[TestCase(typeof(int), false)]
[TestCase(typeof(AsyncPageable<>), false)]
[TestCase(typeof(AsyncPageable<int>), false)]
[TestCase(typeof(TestAsyncPageable<>), false)]
[TestCase(typeof(TestAsyncPageable<int>), false)]
[TestCase(typeof(Pageable<>), true)]
[TestCase(typeof(Pageable<int>), true)]
[TestCase(typeof(TestPageable<>), false)]
[TestCase(typeof(TestPageable<int>), false)]
public void IsPageable(Type type, bool expected)
=> Assert.AreEqual(new CSharpType(type).IsPageable, expected);

private class TestPageable<T> : Pageable<T> where T : notnull
{
private readonly IEnumerable<Page<T>> _enumerable;
public TestPageable(IEnumerable<Page<T>> enumerable) => _enumerable = enumerable;
public override IEnumerable<Page<T>> AsPages(string? continuationToken = null, int? pageSizeHint = null) => _enumerable;
}

private class TestAsyncPageable<T> : AsyncPageable<T> where T : notnull
{
private readonly IAsyncEnumerable<Page<T>> _enumerable;
public TestAsyncPageable(IAsyncEnumerable<Page<T>> enumerable) => _enumerable = enumerable;
public override IAsyncEnumerable<Page<T>> AsPages(string? continuationToken = null, int? pageSizeHint = null) => _enumerable;
}

[TestCaseSource(nameof(ValidateNullableTypesData))]
public void ValidateNullableTypes(Type type, IReadOnlyList<CSharpType> expectedArguments, bool expectedIsNullable)
{
var csharpType = new CSharpType(type);

CollectionAssert.AreEqual(expectedArguments, csharpType.Arguments);
Assert.AreEqual(expectedIsNullable, csharpType.IsNullable);
}

private static object[] ValidateNullableTypesData = new[]
{
new object[]
{
typeof(int), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(int?), Array.Empty<CSharpType>(), true
},
new object[]
{
typeof(Uri), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(Guid), Array.Empty<CSharpType>(), false
},
new object[]
{
typeof(Guid?), Array.Empty<CSharpType>(), true
},
new object[]
{
typeof(TestStruct<int>), new CSharpType[] { typeof(int) }, false
},
new object[]
{
typeof(TestStruct<int>?), new CSharpType[] { typeof(int) }, true
},
new object[]
{
typeof(TestStruct<int?>), new CSharpType[] { typeof(int?) }, false
},
new object[]
{
typeof(TestStruct<int?>?), new CSharpType[] { typeof(int?) }, true
},
new object[]
{
typeof(TestStruct<TestStruct<int>>), new CSharpType[] { typeof(TestStruct<int>) }, false
},
new object[]
{
typeof(TestStruct<TestStruct<int>>?), new CSharpType[] { typeof(TestStruct<int>) }, true
},
new object[]
{
typeof(TestStruct<TestStruct<int>?>), new CSharpType[] { typeof(TestStruct<int>?) }, false
},
new object[]
{
typeof(TestStruct<TestStruct<int>?>?), new CSharpType[] { typeof(TestStruct<int>?) }, true
},
};

internal struct TestStruct<T> { }
}
}
Loading

0 comments on commit 6fdbf35

Please sign in to comment.