Skip to content

Commit

Permalink
remove reserved names provider; add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Nov 27, 2024
1 parent 0343d2a commit 0fa1c0d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/Kiota.Builder/Refiners/HttpReservedNamesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
namespace Kiota.Builder.Refiners;
public class HttpReservedNamesProvider : IReservedNamesProvider
{
private readonly Lazy<HashSet<string>> _reservedNames = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"GET",
"POST",
"PUT",
"DELETE",
"PATCH",
"OPTIONS",
"HEAD",
"CONNECT",
"TRACE"
private readonly Lazy<HashSet<string>> _reservedNames = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
});
public HashSet<string> ReservedNames => _reservedNames.Value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Configuration;
using Kiota.Builder.Extensions;
using Kiota.Builder.Refiners;
using Kiota.Builder.Tests.OpenApiSampleFiles;
using Kiota.Builder.Writers;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;
using static Kiota.Builder.Refiners.HttpRefiner;

namespace Kiota.Builder.Tests.Writers.Http;
public sealed class CodeClassDeclarationWriterTests : IDisposable
{
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeNamespace root;

public CodeClassDeclarationWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.HTTP, DefaultPath, DefaultName);
tw = new StringWriter();
writer.SetTextWriter(tw);
root = CodeNamespace.InitRootNamespace();
}
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}

[Fact]
public async Task TestWriteTypeDeclaration()
{
var codeClass = new CodeClass
{
Name = "TestClass",
Kind = CodeClassKind.RequestBuilder
};
var urlTemplateProperty = new CodeProperty
{
Name = "urlTemplate",
Kind = CodePropertyKind.UrlTemplate,
DefaultValue = "\"https://example.com/{id}\"",
Type = new CodeType
{
Name = "string",
IsExternal = true
},
};
codeClass.AddProperty(urlTemplateProperty);

// Add a new property named BaseUrl and set its value to the baseUrl string
var baseUrlProperty = new CodeProperty
{
Name = "BaseUrl",
Kind = CodePropertyKind.Custom,
Access = AccessModifier.Private,
DefaultValue = "https://example.com",
Type = new CodeType { Name = "string", IsExternal = true }
};
codeClass.AddProperty(baseUrlProperty);

var method = new CodeMethod
{
Name = "get",
Kind = CodeMethodKind.RequestExecutor,
Documentation = new CodeDocumentation { DescriptionTemplate = "GET method" },
ReturnType = new CodeType { Name = "void" }
};

codeClass.AddMethod(method);

root.AddClass(codeClass);

await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.HTTP }, root);

writer.Write(codeClass.StartBlock);
var result = tw.ToString();

Assert.Contains("# Base url for the server/host", result);
Assert.Contains("@url = https://example.com", result);
}
}

0 comments on commit 0fa1c0d

Please sign in to comment.