Added checks to tests for method parameters signature.

This commit is contained in:
Michael Yanni 2020-04-10 15:00:39 -07:00
Родитель c5bc1ea50b
Коммит 4d7fc1ec8b
1 изменённых файлов: 117 добавлений и 16 удалений

Просмотреть файл

@ -1,6 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using NUnit.Framework;
@ -12,49 +15,147 @@ namespace AutoRest.TestServer.Tests
{
public RequiredOptionalTest(TestServerVersion version) : base(version, "reqopt") { }
[Test]
public Task OptionalArrayHeader() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayHeaderAsync());
private void TestDefaultNullParameter(Type clientType, string methodName, string parameterName)
{
var parameters = clientType.GetMethod(methodName)?.GetParameters() ?? Array.Empty<ParameterInfo>();
var parameter = parameters.FirstOrDefault(p => p.Name == parameterName);
Assert.NotNull(parameter);
Assert.IsTrue(parameter.HasDefaultValue);
Assert.Null(parameter.DefaultValue);
}
[Test]
public Task OptionalArrayParameter() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayParameterAsync());
public Task OptionalArrayHeader() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayHeaderAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalArrayHeaderAsync), "headerParameter");
});
[Test]
public Task OptionalArrayProperty() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayPropertyAsync());
public Task OptionalArrayParameter() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayParameterAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalArrayParameterAsync), "bodyParameter");
});
[Test]
public Task OptionalClassParameter() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalClassParameterAsync());
public Task OptionalArrayProperty() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalArrayPropertyAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalArrayPropertyAsync), "bodyParameter");
});
[Test]
public Task OptionalClassProperty() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalClassPropertyAsync());
public Task OptionalClassParameter() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalClassParameterAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalClassParameterAsync), "bodyParameter");
});
[Test]
public Task OptionalGlobalQuery() => TestStatus(async (host, pipeline) => await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.GetOptionalGlobalQueryAsync());
public Task OptionalClassProperty() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalClassPropertyAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalClassPropertyAsync), "bodyParameter");
});
private void TestImplicitClientConstructor()
{
var constructorParameters = typeof(ImplicitRestClient).GetConstructors().FirstOrDefault(c => c.GetParameters().Any())?.GetParameters() ?? Array.Empty<ParameterInfo>();
var pathParameter = constructorParameters.FirstOrDefault(p => p.Name == "requiredGlobalPath");
var queryParameter = constructorParameters.FirstOrDefault(p => p.Name == "requiredGlobalQuery");
Assert.NotNull(pathParameter);
Assert.NotNull(queryParameter);
Assert.IsFalse(pathParameter.HasDefaultValue);
Assert.IsFalse(queryParameter.HasDefaultValue);
}
[Test]
public Task OptionalImplicitBody() => TestStatus(async (host, pipeline) => await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalBodyAsync());
public Task OptionalGlobalQuery() => Test(async (host, pipeline) =>
{
var result = await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.GetOptionalGlobalQueryAsync();
Assert.AreEqual(200, result.Status);
TestImplicitClientConstructor();
});
[Test]
public Task OptionalImplicitHeader() => TestStatus(async (host, pipeline) => await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalHeaderAsync());
public Task OptionalImplicitBody() => Test(async (host, pipeline) =>
{
var result = await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalBodyAsync();
Assert.AreEqual(200, result.Status);
TestImplicitClientConstructor();
TestDefaultNullParameter(typeof(ImplicitRestClient), nameof(ImplicitRestClient.PutOptionalBodyAsync), "bodyParameter");
});
[Test]
public Task OptionalImplicitQuery() => TestStatus(async (host, pipeline) => await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalQueryAsync());
public Task OptionalImplicitHeader() => Test(async (host, pipeline) =>
{
var result = await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalHeaderAsync();
Assert.AreEqual(200, result.Status);
TestImplicitClientConstructor();
TestDefaultNullParameter(typeof(ImplicitRestClient), nameof(ImplicitRestClient.PutOptionalHeaderAsync), "queryParameter");
});
[Test]
public Task OptionalIntegerHeader() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerHeaderAsync());
public Task OptionalImplicitQuery() => Test(async (host, pipeline) =>
{
var result = await new ImplicitClient(ClientDiagnostics, pipeline, string.Empty, string.Empty, host).RestClient.PutOptionalQueryAsync();
Assert.AreEqual(200, result.Status);
TestImplicitClientConstructor();
TestDefaultNullParameter(typeof(ImplicitRestClient), nameof(ImplicitRestClient.PutOptionalQueryAsync), "queryParameter");
});
[Test]
public Task OptionalIntegerParameter() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerParameterAsync());
public Task OptionalIntegerHeader() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerHeaderAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalIntegerHeaderAsync), "headerParameter");
});
[Test]
public Task OptionalIntegerProperty() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerPropertyAsync());
public Task OptionalIntegerParameter() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerParameterAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalIntegerParameterAsync), "bodyParameter");
});
[Test]
public Task OptionalStringHeader() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringHeaderAsync());
public Task OptionalIntegerProperty() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalIntegerPropertyAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalIntegerPropertyAsync), "bodyParameter");
});
[Test]
public Task OptionalStringParameter() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringParameterAsync());
public Task OptionalStringHeader() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringHeaderAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalStringHeaderAsync), "bodyParameter");
});
[Test]
public Task OptionalStringProperty() => TestStatus(async (host, pipeline) => await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringPropertyAsync());
public Task OptionalStringParameter() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringParameterAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalStringParameterAsync), "bodyParameter");
});
[Test]
public Task OptionalStringProperty() => Test(async (host, pipeline) =>
{
var result = await new ExplicitClient(ClientDiagnostics, pipeline, host).RestClient.PostOptionalStringPropertyAsync();
Assert.AreEqual(200, result.Status);
TestDefaultNullParameter(typeof(ExplicitRestClient), nameof(ExplicitRestClient.PostOptionalStringParameterAsync), "bodyParameter");
});
}
}