-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #562 from Fargekritt/main
Added simple logic to make most identifier strings valid
- Loading branch information
Showing
2 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Refitter.Tests/Examples/OperationIdWithInvalidFirstCharTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using FluentAssertions; | ||
using Refitter.Core; | ||
using Refitter.Tests.Build; | ||
using Xunit; | ||
|
||
namespace Refitter.Tests.Examples; | ||
|
||
public class OperationIdWithInvalidFirstCharTests | ||
{ | ||
|
||
private const string OpenApiSpec = @" | ||
openapi: '3.0.0' | ||
paths: | ||
/jobs/{job-id}: | ||
get: | ||
tags: | ||
- 'Jobs' | ||
operationId: '2fa' | ||
description: '2 factr auth' | ||
parameters: | ||
- in: 'path' | ||
name: 'job-id' | ||
description: 'Job ID' | ||
required: true | ||
schema: | ||
type: 'string' | ||
responses: | ||
'200': | ||
description: 'successful operation' | ||
"; | ||
|
||
[Fact] | ||
public async Task Can_Generate_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
|
||
[Fact] | ||
public async Task Adds_Underscore_At_Beginning_With_Ivalid_Methode_Name() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
generateCode.Should().Contain("_2fa"); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_Build_Generated_Code() | ||
{ | ||
string generateCode = await GenerateCode(); | ||
BuildHelper | ||
.BuildCSharp(generateCode) | ||
.Should() | ||
.BeTrue(); | ||
} | ||
|
||
private static async Task<string> GenerateCode() | ||
{ | ||
var swaggerFile = await CreateSwaggerFile(OpenApiSpec); | ||
var settings = new RefitGeneratorSettings | ||
{ | ||
OpenApiPath = swaggerFile | ||
}; | ||
|
||
var sut = await RefitGenerator.CreateAsync(settings); | ||
var generateCode = sut.Generate(); | ||
return generateCode; | ||
} | ||
|
||
private static async Task<string> CreateSwaggerFile(string contents) | ||
{ | ||
var filename = $"{Guid.NewGuid()}.yml"; | ||
var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); | ||
Directory.CreateDirectory(folder); | ||
var swaggerFile = Path.Combine(folder, filename); | ||
await File.WriteAllTextAsync(swaggerFile, contents); | ||
return swaggerFile; | ||
} | ||
} |