Skip to content

Commit

Permalink
Add cadl-ranch tests (Azure#3120)
Browse files Browse the repository at this point in the history
  • Loading branch information
pshao25 authored Feb 16, 2023
1 parent 22af400 commit 8493942
Show file tree
Hide file tree
Showing 17 changed files with 1,430 additions and 3 deletions.
16 changes: 13 additions & 3 deletions eng/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ function Add-TestServer-Swagger ([string]$testName, [string]$projectSuffix, [str
Add-Swagger "$testName$projectSuffix" $projectDirectory "--require=$configurationPath --try-require=$inputReadme --input-file=$inputFile $additionalArgs"
}

function Add-CadlRanch-Cadl([string]$testName, [string]$projectPrefix, [string]$cadlRanchProjectsDirectory) {
function Add-CadlRanch-Cadl([string]$testName, [string]$projectPrefix, [string]$cadlRanchProjectsDirectory, [boolean]$generateConvenience) {
$projectDirectory = Join-Path $cadlRanchProjectsDirectory $testName
$cadlMain = Join-Path $cadlRanchFilePath $testName "main.cadl"
Add-Cadl "$projectPrefix$testName" $projectDirectory $cadlMain "--option @azure-tools/cadl-csharp.unreferenced-types-handling=keepAll"
$convenienceOption = If ($generateConvenience) {""} Else {" --option @azure-tools/cadl-csharp.generate-convenience-methods=false"}
Add-Cadl "$projectPrefix$testName" $projectDirectory $cadlMain "--option @azure-tools/cadl-csharp.unreferenced-types-handling=keepAll$convenienceOption"
}

$testNames =
Expand Down Expand Up @@ -252,6 +253,10 @@ if (!($Exclude -contains "Samples"))

# Cadl projects
$cadlRanchProjectDirectory = Join-Path $repoRoot 'test' 'CadlRanchProjects'
$cadlRanchProjectPathsWithoutConvenience = # Needs justification to add item
'enums/extensible', # https://github.com/Azure/autorest.csharp/issues/3079
'hello' # https://github.com/Azure/autorest.csharp/issues/3110

$cadlRanchProjectPaths =
'arrays/item-types',
'authentication/api-key',
Expand All @@ -265,7 +270,12 @@ if (!($Exclude -contains "CadlRanchProjects"))
{
foreach ($testPath in $cadlRanchProjectPaths)
{
Add-CadlRanch-Cadl $testPath "cadl-" $cadlRanchProjectDirectory
Add-CadlRanch-Cadl $testPath "cadl-" $cadlRanchProjectDirectory $TRUE
}

foreach ($testPath in $cadlRanchProjectPathsWithoutConvenience)
{
Add-CadlRanch-Cadl $testPath "cadl-" $cadlRanchProjectDirectory $FALSE
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/AutoRest.CSharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\authentication\\union\\Generated"
},
"cadl-enums/extensible": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\enums\\extensible\\Generated"
},
"cadl-hello": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\hello\\Generated"
},
"cadl-models/property-optional": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\models\\property-optional\\Generated"
Expand Down
46 changes: 46 additions & 0 deletions test/CadlRanchProjects.Tests/enums-extensible.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using Azure;
using Azure.Core;
using Enums.Extensible;
using Enums.Extensible.Models;
using NUnit.Framework;

namespace CadlRanchProjects.Tests
{
public class EnumsExtensibleTests : CadlRanchTestBase
{
[Test]
public Task Enums_Extensible_String_getKnownValue() => Test(async (host) =>
{
Response response = await new ExtensibleClient(host, null).GetKnownValueAsync();
JsonData result = JsonData.FromBytes(response.Content.ToMemory());
Assert.AreEqual(DaysOfWeekExtensibleEnum.Monday, new DaysOfWeekExtensibleEnum((string)result));
});

[Test]
public Task Enums_Extensible_String_putKnownValue() => Test(async (host) =>
{
Response response = await new ExtensibleClient(host, null).GetUnknownValueAsync();
JsonData result = JsonData.FromBytes(response.Content.ToMemory());
Assert.AreEqual(new DaysOfWeekExtensibleEnum("Weekend"), new DaysOfWeekExtensibleEnum((string)result));
});

[Test]
public Task Enums_Extensible_String_getUnknownValue() => Test(async (host) =>
{
Response response = await new ExtensibleClient(host, null).PutKnownValueAsync(RequestContent.Create(new JsonData(DaysOfWeekExtensibleEnum.Monday.ToString())));
Assert.AreEqual(204, response.Status);
});

[Test]
public Task Enums_Extensible_String_putUnknownValue() => Test(async (host) =>
{
Response response = await new ExtensibleClient(host, null).PutUnknownValueAsync(RequestContent.Create(new JsonData(new DaysOfWeekExtensibleEnum("Weekend").ToString())));
Assert.AreEqual(204, response.Status);
});
}
}
25 changes: 25 additions & 0 deletions test/CadlRanchProjects.Tests/hello.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using AutoRest.TestServer.Tests.Infrastructure;
using Azure;
using Azure.Core;
using Hello;
using NUnit.Framework;

namespace CadlRanchProjects.Tests
{
public class HelloTests : CadlRanchTestBase
{
[Test]
public Task Hello_world() => Test(async (host) =>
{
Response response = await new HelloClient(host, null).WorldAsync();
Assert.AreEqual(200, response.Status);
Assert.AreEqual("application/json; charset=utf-8", response.Headers.ContentType);
JsonData responseBody = JsonData.FromBytes(response.Content.ToMemory());
Assert.AreEqual("Hello World!", (string)responseBody);
});
}
}
20 changes: 20 additions & 0 deletions test/CadlRanchProjects/enums/extensible/Extensible.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>annotations</Nullable>
</PropertyGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);EXPERIMENTAL</DefineConstants>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Core.Experimental" Version="0.1.0-preview.18" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Core" Version="1.28.0" />
</ItemGroup>

</Project>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?xml version="1.0" encoding="utf-8"?>
<doc>
<members>
<member name="GetKnownValueAsync(RequestContext)">
<example>
This sample shows how to call GetKnownValueAsync and parse the result.
<code><![CDATA[
var client = new ExtensibleClient();
Response response = await client.GetKnownValueAsync();
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.ToString());
]]></code>
</example>
</member>
<member name="GetKnownValue(RequestContext)">
<example>
This sample shows how to call GetKnownValue and parse the result.
<code><![CDATA[
var client = new ExtensibleClient();
Response response = client.GetKnownValue();
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.ToString());
]]></code>
</example>
</member>
<member name="GetUnknownValueAsync(RequestContext)">
<example>
This sample shows how to call GetUnknownValueAsync and parse the result.
<code><![CDATA[
var client = new ExtensibleClient();
Response response = await client.GetUnknownValueAsync();
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.ToString());
]]></code>
</example>
</member>
<member name="GetUnknownValue(RequestContext)">
<example>
This sample shows how to call GetUnknownValue and parse the result.
<code><![CDATA[
var client = new ExtensibleClient();
Response response = client.GetUnknownValue();
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.ToString());
]]></code>
</example>
</member>
<member name="PutKnownValueAsync(RequestContent,RequestContext)">
<example>
This sample shows how to call PutKnownValueAsync with required request content.
<code><![CDATA[
var client = new ExtensibleClient();
var data = "Monday";
Response response = await client.PutKnownValueAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
<member name="PutKnownValue(RequestContent,RequestContext)">
<example>
This sample shows how to call PutKnownValue with required request content.
<code><![CDATA[
var client = new ExtensibleClient();
var data = "Monday";
Response response = client.PutKnownValue(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
<member name="PutUnknownValueAsync(RequestContent,RequestContext)">
<example>
This sample shows how to call PutUnknownValueAsync with required request content.
<code><![CDATA[
var client = new ExtensibleClient();
var data = "Monday";
Response response = await client.PutUnknownValueAsync(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
<member name="PutUnknownValue(RequestContent,RequestContext)">
<example>
This sample shows how to call PutUnknownValue with required request content.
<code><![CDATA[
var client = new ExtensibleClient();
var data = "Monday";
Response response = client.PutUnknownValue(RequestContent.Create(data));
Console.WriteLine(response.Status);
]]></code>
</example>
</member>
</members>
</doc>
Loading

0 comments on commit 8493942

Please sign in to comment.