Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using NSwag.CodeGeneration.OperationNameGenerators;
using NSwag.Generation.WebApi;
using System.ComponentModel.DataAnnotations;
using Xunit;
using NSwag.CodeGeneration.Tests;

namespace NSwag.CodeGeneration.CSharp.Tests
{
Expand Down Expand Up @@ -52,7 +52,8 @@ public async Task TestNoGuardForOptionalBodyParameter()
var code = codeGen.GenerateFile();

// Assert
Assert.DoesNotContain("throw new System.ArgumentNullException(\"body\")", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -65,8 +66,7 @@ public async Task TestNullableBodyWithAllowNullableBodyParameters()
var code = generator.GenerateFile();

// Assert
Assert.Contains("throw new System.ArgumentNullException(\"requiredBody\")", code);
Assert.DoesNotContain("throw new System.ArgumentNullException(\"notRequiredBody\")", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -79,8 +79,7 @@ public async Task TestNullableBodyWithoutAllowNullableBodyParameters()
var code = generator.GenerateFile();

// Assert
Assert.Contains("throw new System.ArgumentNullException(\"requiredBody\")", code);
Assert.Contains("throw new System.ArgumentNullException(\"notRequiredBody\")", code);
await VerifyHelper.Verify(code);
}

private static async Task<CSharpClientGenerator> GenerateCode(bool allowNullableBodyParameters)
Expand Down
7 changes: 0 additions & 7 deletions src/NSwag.CodeGeneration.CSharp.Tests/ArrayParameterTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NSwag.CodeGeneration.Tests;
using Xunit;

namespace NSwag.CodeGeneration.CSharp.Tests
{
Expand Down Expand Up @@ -67,7 +66,6 @@ public async Task When_parameter_is_array_then_CSharp_is_correct()

// Assert
await VerifyHelper.Verify(code);

CodeCompiler.AssertCompile(code);
}

Expand Down Expand Up @@ -144,7 +142,6 @@ public async Task When_explode_is_false_then_do_not_explode(string explode)

// Assert
await VerifyHelper.Verify(code).UseParameters(explode);

CodeCompiler.AssertCompile(code);
}

Expand Down Expand Up @@ -223,7 +220,6 @@ public async Task When_explode_is_explicitly_or_implicitly_true_then_explode(str

// Assert
await VerifyHelper.Verify(code).UseParameters(explode);

CodeCompiler.AssertCompile(code);
}

Expand Down Expand Up @@ -290,7 +286,6 @@ public async Task When_CollectionFormat_is_csv_then_do_not_explode()

// Assert
await VerifyHelper.Verify(code);

CodeCompiler.AssertCompile(code);
}

Expand Down Expand Up @@ -357,7 +352,6 @@ public async Task When_CollectionFormat_is_multi_then_explode()

// Assert
await VerifyHelper.Verify(code);

CodeCompiler.AssertCompile(code);
}

Expand Down Expand Up @@ -433,7 +427,6 @@ public async Task when_content_is_formdata_with_property_array_then_content_shou

// Assert
await VerifyHelper.Verify(code);

CodeCompiler.AssertCompile(code);
}
}
Expand Down
51 changes: 22 additions & 29 deletions src/NSwag.CodeGeneration.CSharp.Tests/BinaryTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using NSwag.CodeGeneration.CSharp.Models;
using Xunit;
using NSwag.CodeGeneration.Tests;

namespace NSwag.CodeGeneration.CSharp.Tests
{
public class BinaryTests
{

[Fact]
public async Task When_body_is_binary_then_stream_is_used_as_parameter_in_CSharp()
{
Expand Down Expand Up @@ -51,9 +50,9 @@ public async Task When_body_is_binary_then_stream_is_used_as_parameter_in_CSharp
var codeGenerator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings());
var code = codeGenerator.GenerateFile();

//// Assert
Assert.Contains("public virtual async System.Threading.Tasks.Task<FileToken> AddFileAsync(System.IO.Stream body, System.Threading.CancellationToken cancellationToken)", code);
Assert.Contains("var content_ = new System.Net.Http.StreamContent(body);", code);
// Assert
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand Down Expand Up @@ -105,8 +104,8 @@ public async Task When_body_is_binary_then_IFormFile_is_used_as_parameter_in_CSh
var code = codeGenerator.GenerateFile();

// Assert
Assert.Contains("Microsoft.AspNetCore.Http.IFormFile body", code);
Assert.DoesNotContain("FromBody]", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand Down Expand Up @@ -160,8 +159,8 @@ public async Task When_body_is_binary_array_then_IFormFile_Collection_is_used_as
var code = codeGenerator.GenerateFile();

// Assert
Assert.Contains("System.Collections.Generic.ICollection<Microsoft.AspNetCore.Http.IFormFile> body", code);
Assert.DoesNotContain("FromBody]", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand Down Expand Up @@ -225,10 +224,8 @@ public async Task WhenSpecContainsFormDataInSingleMultipartFile_ThenFormDataIsUs
var code = codeGenerator.GenerateFile();

// Assert
Assert.Contains("var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);", code);
Assert.Contains("var content_file_ = new System.Net.Http.StreamContent(file.Data);", code);
Assert.Contains("class FileParameter", code);
Assert.Contains("content_.Add(content_file_, \"file\", file.FileName ?? \"file\");", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand Down Expand Up @@ -295,10 +292,8 @@ public async Task WhenSpecContainsFormDataInMultipartFileArray_ThenFormDataIsUse
var code = codeGenerator.GenerateFile();

// Assert
Assert.Contains("var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);", code);
Assert.Contains("var content_files_ = new System.Net.Http.StreamContent(item_.Data);", code);
Assert.Contains("class FileParameter", code);
Assert.Contains("content_.Add(content_files_, \"files\", item_.FileName ?? \"files\");", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand Down Expand Up @@ -376,11 +371,10 @@ public async Task WhenSpecContainsFormDataInNestedMultipartForm_ThenFormDataIsUs
var code = codeGenerator.GenerateFile();

// Assert
Assert.Contains("var content_ = new System.Net.Http.MultipartFormDataContent(boundary_);", code);
Assert.Contains("var content_contents_ = new System.Net.Http.StreamContent(contents.Data);", code);
Assert.Contains("class FileParameter", code);
Assert.Contains("content_.Add(content_contents_, \"Contents\", contents.FileName ?? \"Contents\");", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
public async Task When_multipart_with_ref_should_read_schema()
{
Expand Down Expand Up @@ -440,11 +434,11 @@ public async Task When_multipart_with_ref_should_read_schema()
var codeGenerator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings());
var code = codeGenerator.GenerateFile();

//// Assert
Assert.Contains("public virtual async System.Threading.Tasks.Task<CreateAddFileResponse> AddFileAsync(FileParameter file, Model? model, System.Threading.CancellationToken cancellationToken)", code);
Assert.Contains("var content_file_ = new System.Net.Http.StreamContent(file.Data);", code);
Assert.Contains("public partial class FileParameter", code);
// Assert
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
public async Task When_multipart_inline_schema()
{
Expand Down Expand Up @@ -502,10 +496,9 @@ public async Task When_multipart_inline_schema()
var codeGenerator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings());
var code = codeGenerator.GenerateFile();

//// Assert
Assert.Contains("public virtual async System.Threading.Tasks.Task<CreateAddFileResponse> AddFileAsync(FileParameter file, Model? model, System.Threading.CancellationToken cancellationToken)", code);
Assert.Contains("var content_file_ = new System.Net.Http.StreamContent(file.Data);", code);
Assert.Contains("public partial class FileParameter", code);
// Assert
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}
}
}
35 changes: 19 additions & 16 deletions src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.NewtonsoftJson.Generation;
using NSwag.CodeGeneration.Tests;
using NSwag.Generation.WebApi;
using Xunit;

namespace NSwag.CodeGeneration.CSharp.Tests
{
Expand Down Expand Up @@ -36,7 +36,7 @@ public async Task When_ConfigurationClass_is_set_then_correct_ctor_is_generated(
var code = generator.GenerateFile();

// Assert
Assert.Contains("public FooClient(MyConfig configuration) : base(configuration)", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public async Task When_UseHttpRequestMessageCreationMethod_is_set_then_CreateReq
var code = generator.GenerateFile();

// Assert
Assert.Contains("var request_ = await CreateHttpRequestMessageAsync(cancellationToken).ConfigureAwait(false)", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -83,8 +83,8 @@ public async Task WhenUsingBaseUrl_ButNoProperty_ThenPropertyIsNotUsedAndFieldIs
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("BaseUrl", code);
Assert.Contains("string _baseUrl", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -103,7 +103,8 @@ public async Task When_parameter_name_is_reserved_keyword_then_it_is_appended_wi
var code = generator.GenerateFile();

// Assert
Assert.Contains("Task<object> GetPersonAsync(bool? @override, ", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -125,7 +126,8 @@ public async Task When_code_is_generated_then_by_default_the_system_httpclient_i
var code = generator.GenerateFile();

// Assert
Assert.Contains("var client_ = new System.Net.Http.HttpClient();", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -148,7 +150,7 @@ public async Task When_custom_http_client_type_is_specified_then_an_instance_of_
var code = generator.GenerateFile();

// Assert
Assert.Contains("var client_ = new CustomNamespace.CustomHttpClient();", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -170,7 +172,8 @@ public async Task When_client_base_interface_is_not_specified_then_client_interf
var code = generator.GenerateFile();

// Assert
Assert.Contains("public partial interface IFooClient\n", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -193,7 +196,8 @@ public async Task When_client_base_interface_is_not_specified_then_client_interf
var code = generator.GenerateFile();

// Assert
Assert.Contains("internal partial interface IFooClient\n", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -216,7 +220,7 @@ public async Task When_client_base_interface_is_specified_then_client_interface_
var code = generator.GenerateFile();

// Assert
Assert.Contains("public partial interface IFooClient : IClientBase", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -240,7 +244,7 @@ public async Task When_client_base_interface_is_specified_with_access_modifier_t
var code = generator.GenerateFile();

// Assert
Assert.Contains("internal partial interface IFooClient : IClientBase", code);
await VerifyHelper.Verify(code);
}

[Fact]
Expand All @@ -265,8 +269,8 @@ public async Task When_client_class_generation_is_enabled_and_suppressed_then_cl
var code = generator.GenerateFile();

// Assert
Assert.Contains("public partial interface IFooClient", code);
Assert.DoesNotContain("public partial class FooClient : IFooClient", code);
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

[Fact]
Expand All @@ -291,8 +295,7 @@ public async Task When_client_interface_generation_is_enabled_and_suppressed_the
var code = generator.GenerateFile();

// Assert
Assert.DoesNotContain("public partial interface IFooClient", code);
Assert.Contains("public partial class FooClient : IFooClient", code);
await VerifyHelper.Verify(code);
}
}
}
1 change: 1 addition & 0 deletions src/NSwag.CodeGeneration.CSharp.Tests/CodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static CodeCompiler()
.Select(x => MetadataReference.CreateFromFile(x.Location))
.Append(MetadataReference.CreateFromFile(typeof(HttpClient).Assembly.Location))
.Append(MetadataReference.CreateFromFile(typeof(Microsoft.AspNetCore.Mvc.FileResult).Assembly.Location))
.Append(MetadataReference.CreateFromFile(typeof(Microsoft.AspNetCore.Http.IFormFile).Assembly.Location))
.Append(MetadataReference.CreateFromFile(typeof(Newtonsoft.Json.JsonSerializer).Assembly.Location))
.Append(MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.RangeAttribute).Assembly.Location))
.Append(MetadataReference.CreateFromFile(typeof(System.Collections.ObjectModel.ObservableCollection<>).Assembly.Location))
Expand Down
Loading
Loading