Skip to content

Commit

Permalink
unit tests for CodeEnum writer
Browse files Browse the repository at this point in the history
  • Loading branch information
koros committed Nov 21, 2024
1 parent 41345d5 commit cfabf37
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 124 deletions.
125 changes: 1 addition & 124 deletions src/Kiota.Builder/Refiners/HttpRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
using Kiota.Builder.Extensions;

namespace Kiota.Builder.Refiners;
public class HttpRefiner : CommonLanguageRefiner
public class HttpRefiner(GenerationConfiguration configuration) : CommonLanguageRefiner(configuration)
{
public HttpRefiner(GenerationConfiguration configuration) : base(configuration) { }
public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken cancellationToken)
{
return Task.Run(() =>
Expand Down Expand Up @@ -38,17 +37,8 @@ public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken
SetBaseUrlForRequestBuilderMethods(generatedCode, GetBaseUrl(generatedCode));
// Remove unused code from the DOM e.g Models, BarrelInitializers, e.t.c
RemoveUnusedCodeElements(generatedCode);
CorrectCoreType(
generatedCode,
CorrectMethodType,
CorrectPropertyType,
CorrectImplements);
}, cancellationToken);
}
private static void CorrectImplements(ProprietableBlockDeclaration block)
{
block.ReplaceImplementByName(KiotaBuilder.AdditionalHolderInterface, "AdditionalDataHolder");
}

private string? GetBaseUrl(CodeElement element)
{
Expand All @@ -61,119 +51,6 @@ private static void CorrectImplements(ProprietableBlockDeclaration block)
.BaseUrl;
}

private static void CorrectMethodType(CodeMethod currentMethod)
{
var parentClass = currentMethod.Parent as CodeClass;
if (currentMethod.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator))
{
if (currentMethod.IsOfKind(CodeMethodKind.RequestExecutor))
currentMethod.Parameters.Where(x => x.Type.Name.Equals("IResponseHandler", StringComparison.Ordinal)).ToList().ForEach(x =>
{
x.Type.Name = "ResponseHandler";
x.Type.IsNullable = false; //no pointers
});
else if (currentMethod.IsOfKind(CodeMethodKind.RequestGenerator))
currentMethod.ReturnType.IsNullable = true;
}
else if (currentMethod.IsOfKind(CodeMethodKind.Serializer))
currentMethod.Parameters.Where(x => x.Type.Name.Equals("ISerializationWriter", StringComparison.Ordinal)).ToList().ForEach(x => x.Type.Name = "SerializationWriter");
else if (currentMethod.IsOfKind(CodeMethodKind.Deserializer))
{
currentMethod.ReturnType.Name = "[String:FieldDeserializer<T>][String:FieldDeserializer<T>]";
currentMethod.Name = "getFieldDeserializers";
}
else if (currentMethod.IsOfKind(CodeMethodKind.ClientConstructor, CodeMethodKind.Constructor, CodeMethodKind.RawUrlConstructor))
{
var rawUrlParam = currentMethod.Parameters.OfKind(CodeParameterKind.RawUrl);
if (rawUrlParam != null)
rawUrlParam.Type.IsNullable = false;
currentMethod.Parameters.Where(x => x.IsOfKind(CodeParameterKind.RequestAdapter))
.Where(x => x.Type.Name.StartsWith('I'))
.ToList()
.ForEach(x => x.Type.Name = x.Type.Name[1..]); // removing the "I"
}
else if (currentMethod.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility, CodeMethodKind.RequestBuilderWithParameters, CodeMethodKind.RequestBuilderBackwardCompatibility, CodeMethodKind.Factory))
{
currentMethod.ReturnType.IsNullable = true;
if (currentMethod.Parameters.OfKind(CodeParameterKind.ParseNode) is CodeParameter parseNodeParam)
{
parseNodeParam.Type.Name = parseNodeParam.Type.Name[1..];
parseNodeParam.Type.IsNullable = false;
}
if (currentMethod.IsOfKind(CodeMethodKind.Factory))
currentMethod.ReturnType = new CodeType { Name = "Parsable", IsNullable = false, IsExternal = true };
}
CorrectCoreTypes(parentClass, DateTypesReplacements, currentMethod.Parameters
.Select(x => x.Type)
.Union(new[] { currentMethod.ReturnType })
.ToArray());
}
private static readonly Dictionary<string, (string, CodeUsing?)> DateTypesReplacements = new(StringComparer.OrdinalIgnoreCase) {
{"DateTimeOffset", ("Date", new CodeUsing {
Name = "Date",
Declaration = new CodeType {
Name = "Foundation",
IsExternal = true,
},
})},
{"TimeSpan", ("Date", new CodeUsing {
Name = "Date",
Declaration = new CodeType {
Name = "Foundation",
IsExternal = true,
},
})},
{"DateOnly", ("Date", new CodeUsing {
Name = "Date",
Declaration = new CodeType {
Name = "Foundation",
IsExternal = true,
},
})},
{"TimeOnly", ("Date", new CodeUsing {
Name = "Date",
Declaration = new CodeType {
Name = "Foundation",
IsExternal = true,
},
})},
};
private static void CorrectPropertyType(CodeProperty currentProperty)
{
if (currentProperty.Type != null)
{
if (currentProperty.IsOfKind(CodePropertyKind.RequestAdapter))
{
currentProperty.Type.IsNullable = true;
currentProperty.Type.Name = "RequestAdapter";
}
else if (currentProperty.IsOfKind(CodePropertyKind.BackingStore))
currentProperty.Type.Name = currentProperty.Type.Name[1..]; // removing the "I"
else if (currentProperty.IsOfKind(CodePropertyKind.AdditionalData))
{
currentProperty.Type.IsNullable = false;
currentProperty.Type.Name = "[String:Any]";
currentProperty.DefaultValue = $"{currentProperty.Type.Name}()";
}
else if (currentProperty.IsOfKind(CodePropertyKind.PathParameters))
{
currentProperty.Type.IsNullable = true;
currentProperty.Type.Name = "[String:String]";
if (!string.IsNullOrEmpty(currentProperty.DefaultValue))
currentProperty.DefaultValue = $"{currentProperty.Type.Name}()";
}
else if (currentProperty.IsOfKind(CodePropertyKind.Options))
{
currentProperty.Type.IsNullable = false;
currentProperty.Type.Name = "RequestOption";
currentProperty.Type.CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Array;
}
else if (currentProperty.IsOfKind(CodePropertyKind.QueryParameter) && currentProperty.Parent is CodeClass parentClass)
currentProperty.Type.Name = $"{parentClass.Name}{currentProperty.Type.Name}";
CorrectCoreTypes(currentProperty.Parent as CodeClass, DateTypesReplacements, currentProperty.Type);
}
}

private static void CapitalizeNamespacesFirstLetters(CodeElement current)
{
if (current is CodeNamespace currentNamespace)
Expand Down
63 changes: 63 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/HTTP/CodeEnumWriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Linq;

using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using Kiota.Builder.Writers;
using Kiota.Builder.Writers.http;
using Xunit;

namespace Kiota.Builder.Tests.Writers.Http;
public sealed class CodeEnumWriterTests : IDisposable
{
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
private readonly LanguageWriter writer;
private readonly CodeEnum currentEnum;
private const string EnumName = "someEnum";
private readonly CodeEnumWriter codeEnumWriter;
public CodeEnumWriterTests()
{
writer = LanguageWriter.GetLanguageWriter(GenerationLanguage.HTTP, DefaultPath, DefaultName);
codeEnumWriter = new CodeEnumWriter(new HttpConventionService("foo"));
tw = new StringWriter();
writer.SetTextWriter(tw);
var root = CodeNamespace.InitRootNamespace();
currentEnum = root.AddEnum(new CodeEnum
{
Name = EnumName,
}).First();
if (CodeConstant.FromCodeEnum(currentEnum) is CodeConstant constant)
{
currentEnum.CodeEnumObject = constant;
root.AddConstant(constant);
}
}
public void Dispose()
{
tw?.Dispose();
GC.SuppressFinalize(this);
}
[Fact]
public void WriteCodeElement_ThrowsException_WhenCodeElementIsNull()
{
Assert.Throws<ArgumentNullException>(() => codeEnumWriter.WriteCodeElement(null, writer));
}
[Fact]
public void WriteCodeElement_ThrowsException_WhenWriterIsNull()
{
var codeElement = new CodeEnum();
Assert.Throws<ArgumentNullException>(() => codeEnumWriter.WriteCodeElement(codeElement, null));
}
[Fact]
public void SkipsEnum()
{
const string optionName = "option1";
currentEnum.AddOption(new CodeEnumOption { Name = optionName });
codeEnumWriter.WriteCodeElement(currentEnum, writer);
var result = tw.ToString();
Assert.True(string.IsNullOrEmpty(result));
}
}

0 comments on commit cfabf37

Please sign in to comment.