Skip to content

Commit

Permalink
Playing with CultureInfo and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
pjmagee committed Jun 13, 2024
1 parent a3f2f74 commit ef729b7
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions tests/Kiota.Builder.Tests/Writers/Python/CodeUsingWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Writers;
using Kiota.Builder.Writers.Python;
Expand Down Expand Up @@ -92,8 +94,45 @@ public void DoesntAliasRegularSymbols()
Assert.Contains("from .bar import Bar", result);
}

[Fact]
public void WritesFutureImportsFirst()
[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("es-ES")]
[InlineData("it-IT")]
[InlineData("ja-JP")]
[InlineData("ko-KR")]
[InlineData("pt-BR")]
[InlineData("ru-RU")]
[InlineData("zh-CN")]
[InlineData("zh-TW")]
public void FutureShouldBeSortedBeforeDatetimeInDifferentCultures(string cultureName)
{
var cultureInfo = new CultureInfo(cultureName);
var compareInfo = cultureInfo.CompareInfo;

var sorted = new string[] { null, "__future__" }.OrderBy(x => x, StringComparer.Create(cultureInfo, CompareOptions.IgnoreCase)).ToArray();

// Half pass, half fail
// I know in the CodeUsingWriter - We are grouping on x.Declaration?.Name - Which can be null?
Assert.True(sorted[0] == "__future__");
}

[Theory]
[InlineData("en-US")]
[InlineData("en-GB")]
[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("es-ES")]
[InlineData("it-IT")]
[InlineData("ja-JP")]
[InlineData("ko-KR")]
[InlineData("pt-BR")]
[InlineData("ru-RU")]
[InlineData("zh-CN")]
[InlineData("zh-TW")]
public void WritesFutureImportsFirst(string cultureName)
{
// Generated with Kiota mcr.microsoft.com/openapi/kiota:1.15.0

Expand All @@ -103,6 +142,13 @@ public void WritesFutureImportsFirst()
// from kiota_abstractions.serialization import AdditionalDataHolder, Parsable, ParseNode, SerializationWriter
// from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Union

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);

var writer2 = LanguageWriter.GetLanguageWriter(GenerationLanguage.Python, DefaultPath, DefaultName);
var tw2 = new StringWriter();
writer2.SetTextWriter(tw2);
var root2 = CodeNamespace.InitRootNamespace();

var usingWriter = new CodeUsingWriter("foo");

var codeClass = new ClassDeclaration
Expand All @@ -126,9 +172,9 @@ public void WritesFutureImportsFirst()
codeClass.AddUsings(new CodeUsing { Name = "Union", Declaration = new CodeType { Name = "typing", IsExternal = true } });
codeClass.AddUsings(new CodeUsing { Name = "datetime", Declaration = new CodeType { Name = "-", IsExternal = true } });

usingWriter.WriteExternalImports(codeClass, writer);
usingWriter.WriteExternalImports(codeClass, writer2);

string[] usings = tw.ToString().Split(tw.NewLine, StringSplitOptions.RemoveEmptyEntries);
string[] usings = tw2.ToString().Split(tw2.NewLine, StringSplitOptions.RemoveEmptyEntries);

Assert.Equal("from __future__ import annotations", usings.First());
Assert.Equal("import datetime", usings[1]);
Expand Down

0 comments on commit ef729b7

Please sign in to comment.