Skip to content

Commit

Permalink
Add converage for http/client/naming (#4162)
Browse files Browse the repository at this point in the history
Fixes #3966
  • Loading branch information
m-nash authored Aug 13, 2024
1 parent 5cdf871 commit deca55d
Show file tree
Hide file tree
Showing 27 changed files with 1,685 additions and 3 deletions.
1 change: 0 additions & 1 deletion packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function IsSpecDir {

$failingSpecs = @(
Join-Path 'http' 'special-words'
Join-Path 'http' 'client' 'naming'
Join-Path 'http' 'client' 'structure' 'default'
Join-Path 'http' 'client' 'structure' 'multi-client'
Join-Path 'http' 'client' 'structure' 'renamed-operation'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ private IReadOnlyList<ValueExpression> GetParamConversions(IReadOnlyList<Paramet
{
if (param.Type.IsEnum)
{
conversions.Add(param.Type.ToSerial(param));
if (param.IsBodyParameter)
{
conversions.Add(BinaryContentSnippets.Create(BinaryDataSnippets.FromObjectAsJson(param.Type.ToSerial(param))));
}
else
{
conversions.Add(param.Type.ToSerial(param));
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void Write(Utf8JsonWriter writer, InputClient value, JsonSeriali
}

client.Name = name ?? throw new JsonException("InputClient must have name");
client.Description = description ?? throw new JsonException("InputClient must have description");
client.Description = description ?? string.Empty;
client.Operations = operations ?? Array.Empty<InputOperation>();
client.Parameters = parameters ?? Array.Empty<InputParameter>();
client.Parent = parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-client-naming": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/client/naming -p StubLibraryPlugin",
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"Unbranded-TypeSpec": {
"commandLineArgs": "$(SolutionDir)/TestProjects/Local/Unbranded-TypeSpec -p ClientModelPlugin",
"commandName": "Executable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public sealed class ParameterProvider : IEquatable<ParameterProvider>
public bool IsOut { get; }
internal IReadOnlyList<AttributeStatement> Attributes { get; } = [];
public WireInformation WireInfo { get; }
public bool IsBodyParameter { get; }

/// <summary>
/// This property tracks which property this parameter is constructed from.
Expand All @@ -52,6 +53,7 @@ public ParameterProvider(InputParameter inputParameter)
Type = CodeModelPlugin.Instance.TypeFactory.CreateCSharpType(inputParameter.Type) ?? throw new InvalidOperationException($"Failed to create CSharpType for {inputParameter.Type}");
Validation = inputParameter.IsRequired && !Type.IsValueType ? ParameterValidationType.AssertNotNull : ParameterValidationType.None;
WireInfo = new WireInformation(CodeModelPlugin.Instance.TypeFactory.GetSerializationFormat(inputParameter.Type), inputParameter.NameInRequest);
IsBodyParameter = inputParameter.Location == RequestLocation.Body;
}

public ParameterProvider(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Client.Naming;
using Client.Naming.Models;
using NUnit.Framework;
using ClientModel = Client.Naming.Models.ClientModel;

namespace TestProjects.CadlRanch.Tests.Http.Client.Naming
{
public class ClientNamingTests : CadlRanchTestBase
{
[CadlRanchTest]
public Task Client() => Test(async (host) =>
{
var response = await new NamingClient(host, null).ClientAsync(new ClientNameModel(true));
Assert.AreEqual(204, response.GetRawResponse().Status);
Assert.NotNull(typeof(ClientNameModel).GetProperty("ClientName"));
Assert.IsNull(typeof(ClientNameModel).GetProperty("DefaultName"));
});

[CadlRanchTest]
public Task Language() => Test(async (host) =>
{
var response = await new NamingClient(host, null).LanguageAsync(new LanguageClientNameModel(true));
Assert.AreEqual(204, response.GetRawResponse().Status);
Assert.NotNull(typeof(LanguageClientNameModel).GetProperty("CSName"));
Assert.IsNull(typeof(LanguageClientNameModel).GetProperty("DefaultName"));
});

[CadlRanchTest]
public Task CompatibleWithEncodedName() => Test(async (host) =>
{
var response = await new NamingClient(host, null).CompatibleWithEncodedNameAsync(new ClientNameAndJsonEncodedNameModel(true));
Assert.AreEqual(204, response.GetRawResponse().Status);
Assert.NotNull(typeof(ClientNameModel).GetProperty("ClientName"));
Assert.IsNull(typeof(ClientNameModel).GetProperty("DefaultName"));
});

[CadlRanchTest]
public Task Operation() => Test(async (host) =>
{
var response = await new NamingClient(host, null).ClientNameAsync();
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task Parameter() => Test(async (host) =>
{
var response = await new NamingClient(host, null).ParameterAsync(clientName: "true");
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task Request() => Test(async (host) =>
{
var response = await new NamingClient(host, null).RequestAsync(clientName: "true");
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task Response() => Test(async (host) =>
{
var response = await new NamingClient(host, null).ResponseAsync();
Assert.IsTrue(response.GetRawResponse().Headers.TryGetValue("default-name", out _));
foreach (var header in response.GetRawResponse().Headers)
{
var key = header.Key;
if (key == "default-name")
{
var value = header.Value;
Assert.AreEqual("true", value);
}
}
});

[CadlRanchTest]
public Task ModelClient() => Test(async (host) =>
{
var response = await new NamingClient(host, null).GetClientModelClient().ClientAsync(new ClientModel(true));
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task ModelLanguage() => Test(async (host) =>
{
var response = await new NamingClient(host, null).GetClientModelClient().LanguageAsync(new CSModel(true));
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task UnionEnumName() => Test(async (host) =>
{
var response = await new NamingClient(host, null).GetUnionEnumClient().UnionEnumNameAsync(ClientExtensibleEnum.EnumValue1);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task UnionEnumMemberName() => Test(async (host) =>
{
var response = await new NamingClient(host, null).GetUnionEnumClient().UnionEnumMemberNameAsync(ExtensibleEnum.ClientEnumValue1);
Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Naming", "src\Client.Naming.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output-folder": ".",
"namespace": "Client.Naming",
"library-name": "Client.Naming",
"use-model-reader-writer": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>This is the Client.Naming client library for developing .NET applications with rich experience.</Description>
<AssemblyTitle>SDK Code Generation Client.Naming</AssemblyTitle>
<Version>1.0.0-beta.1</Version>
<PackageTags>Client.Naming</PackageTags>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.ClientModel" Version="1.1.0-beta.4" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// <auto-generated/>

#nullable disable

using System.ClientModel;
using System.ClientModel.Primitives;
using System.Threading.Tasks;
using Client.Naming.Models;

namespace Client.Naming
{
public partial class ClientModel
{
protected ClientModel() => throw null;

public ClientPipeline Pipeline => throw null;

public virtual ClientResult Client(BinaryContent content, RequestOptions options) => throw null;

public virtual Task<ClientResult> ClientAsync(BinaryContent content, RequestOptions options) => throw null;

public virtual ClientResult Client(Models.ClientModel body) => throw null;

public virtual Task<ClientResult> ClientAsync(Models.ClientModel body) => throw null;

public virtual ClientResult Language(BinaryContent content, RequestOptions options) => throw null;

public virtual Task<ClientResult> LanguageAsync(BinaryContent content, RequestOptions options) => throw null;

public virtual ClientResult Language(CSModel body) => throw null;

public virtual Task<ClientResult> LanguageAsync(CSModel body) => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// <auto-generated/>

#nullable disable

namespace Client.Naming.Models
{
public static partial class ClientNamingModelFactory
{
public static ClientNameModel ClientNameModel(bool clientName = default) => throw null;

public static LanguageClientNameModel LanguageClientNameModel(bool csName = default) => throw null;

public static ClientNameAndJsonEncodedNameModel ClientNameAndJsonEncodedNameModel(bool clientName = default) => throw null;

public static ClientModel ClientModel(bool defaultName = default) => throw null;

public static CSModel CSModel(bool defaultName = default) => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated/>

#nullable disable

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text.Json;

namespace Client.Naming.Models
{
public partial class CSModel : IJsonModel<CSModel>
{
void IJsonModel<CSModel>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) => throw null;

protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) => throw null;

CSModel IJsonModel<CSModel>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => throw null;

protected virtual CSModel JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => throw null;

BinaryData IPersistableModel<CSModel>.Write(ModelReaderWriterOptions options) => throw null;

protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) => throw null;

CSModel IPersistableModel<CSModel>.Create(BinaryData data, ModelReaderWriterOptions options) => throw null;

protected virtual CSModel PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) => throw null;

string IPersistableModel<CSModel>.GetFormatFromOptions(ModelReaderWriterOptions options) => throw null;

public static implicit operator BinaryContent(CSModel csModel) => throw null;

public static explicit operator CSModel(ClientResult result) => throw null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <auto-generated/>

#nullable disable

namespace Client.Naming.Models
{
public partial class CSModel
{
public CSModel(bool defaultName) => throw null;

public bool DefaultName
{
get => throw null;
set => throw null;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <auto-generated/>

#nullable disable

using System;
using System.ComponentModel;

namespace Client.Naming.Models
{
public readonly partial struct ClientExtensibleEnum : IEquatable<ClientExtensibleEnum>
{
public ClientExtensibleEnum(string value) => throw null;

public static ClientExtensibleEnum EnumValue1 => throw null;

public static bool operator ==(ClientExtensibleEnum left, ClientExtensibleEnum right) => throw null;

public static bool operator !=(ClientExtensibleEnum left, ClientExtensibleEnum right) => throw null;

public static implicit operator ClientExtensibleEnum(string value) => throw null;

[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => throw null;

public bool Equals(ClientExtensibleEnum other) => throw null;

public override int GetHashCode() => throw null;

public override string ToString() => throw null;
}
}
Loading

0 comments on commit deca55d

Please sign in to comment.