Skip to content

Commit

Permalink
Code samples generator: print doubles with '.' (not ',') regardless o…
Browse files Browse the repository at this point in the history
…f current culture.
  • Loading branch information
Andrei Timofeev committed Oct 27, 2024
1 parent 877d6df commit 197d57f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
16 changes: 11 additions & 5 deletions generator/ServiceClientGeneratorLib/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Operation Operation
/// The example id taken from the model.
/// </summary>
/// <remarks>
/// This unique id is used for the region in the emitted code sample
/// This unique id is used for the region in the emitted code sample
/// that will be parsed to include the code in the documentation.
/// </remarks>
public string Id
Expand Down Expand Up @@ -201,7 +201,7 @@ public IList<string> GetResponseAssignments()

return result;
}


/// <summary>
/// Given a member and sample data, build a literal/instantation for the
Expand All @@ -227,9 +227,15 @@ public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb)
if (shape.IsString && data.IsString)
cb.AppendQuote(data.ToString());
else if (shape.IsBoolean)
cb.Append(data.ToString().ToLower());
cb.Append(data.ToString().ToLowerInvariant());

else if (shape.IsFloat || shape.IsInt || shape.IsDouble || shape.IsLong)
cb.Append(data.ToString());
{
if (data.IsDouble)
cb.Append(((double)data).ToString(CultureInfo.InvariantCulture));
else
cb.Append(data.ToString());
}

else if (shape.IsList && data.IsArray)
{
Expand Down Expand Up @@ -285,7 +291,7 @@ public void GetSampleLiteral(Shape shape, JsonData data, CodeBuilder cb)
foreach (var field in data.PropertyNames)
{
var property = shape.Members.GetMemberByName(field);

if (null == property)
continue;

Expand Down
37 changes: 37 additions & 0 deletions generator/ServiceClientGeneratorTests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Reflection;
using Xunit;
using System.Linq;
using System.Text;
using System.Globalization;
using System.IO;

namespace ServiceClientGeneratorTests
{
Expand Down Expand Up @@ -66,5 +69,39 @@ public void TestEndpointNameConstruction(string regionCode, string expectedName)
Assert.Equal(GeneratorDriver.ConstructEndpointName(regionCode), expectedName);
}

[Theory]
[InlineData("string", "str", "\"str\"")]
[InlineData("integer", 5, "5")]
[InlineData("float", 0.4, "0.4")]
[InlineData("double", 0.5, "0.5")]
public void TestExampleShapes(string shapeType, object value, string expectedText)
{
TextReader tr = new StringReader("{}");
ServiceModel testModel = new(tr, null);

JsonData shapeJsonData = new()
{
["type"] = shapeType
};

var sb = new StringBuilder();
var cb = new CodeBuilder(sb, 0);

string operationName = "fake";
Example example = new(testModel, operationName, null);

Shape shape = Shape.CreateShape(testModel, operationName, shapeJsonData);
JsonData sampleData = new(value);

CultureInfo originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR"); // In French, the comma is used as a decimal point

example.GetSampleLiteral(shape, sampleData, cb);

CultureInfo.CurrentCulture = originalCulture;

var printedText = sb.ToString();
Assert.Equal(expectedText, printedText);
}
}
}

0 comments on commit 197d57f

Please sign in to comment.