Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Nest/QueryDsl/Geo/WKT/GeoWKTWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

Expand Down Expand Up @@ -121,13 +122,13 @@ private static void WriteEnvelope(IEnvelopeGeoShape envelope, StringBuilder buil

// WKT specification expects the following order: minLon, maxLon, maxLat, minLat.
// envelope is top_left (minLon, maxLat), bottom_right (maxLon, minLat)
builder.Append(topLeft.Longitude)
builder.Append(topLeft.Longitude.ToString(CultureInfo.InvariantCulture))
.Append(", ")
.Append(bottomRight.Longitude)
.Append(bottomRight.Longitude.ToString(CultureInfo.InvariantCulture))
.Append(", ")
.Append(topLeft.Latitude)
.Append(topLeft.Latitude.ToString(CultureInfo.InvariantCulture))
.Append(", ")
.Append(bottomRight.Latitude)
.Append(bottomRight.Latitude.ToString(CultureInfo.InvariantCulture))
.Append(")");
}

Expand Down Expand Up @@ -162,12 +163,12 @@ private static void WriteCoordinates(IEnumerable<GeoCoordinate> coordinates, Str

private static void WriteCoordinate(GeoCoordinate coordinate, StringBuilder builder)
{
builder.Append(coordinate.Longitude)
builder.Append(coordinate.Longitude.ToString(CultureInfo.InvariantCulture))
.Append(" ")
.Append(coordinate.Latitude);
.Append(coordinate.Latitude.ToString(CultureInfo.InvariantCulture));

if (coordinate.Z.HasValue)
builder.Append(" ").Append(coordinate.Z.Value);
builder.Append(" ").Append(coordinate.Z.Value.ToString(CultureInfo.InvariantCulture));
}
}
}
20 changes: 20 additions & 0 deletions src/Tests/Tests.Reproduce/GitHubIssue3819.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Elastic.Xunit.XunitPlumbing;
using FluentAssertions;
using Nest;

namespace Tests.Reproduce
{
public class GitHubIssue3819
{
[U]
[UseCulture("fr-FR")]
public void WriteWKTCoordinatesWithInvariantCulture()
{
var wkt = "POINT (45.1 42.25)";

var shape = GeoWKTReader.Read(wkt);
var actual = GeoWKTWriter.Write(shape);
actual.Should().Be(wkt);
}
}
}
110 changes: 110 additions & 0 deletions src/Tests/Tests.Reproduce/UseCultureAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#region License
// Copyright (c) .NET Foundation and Contributors
// All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// https://github.com/xunit/samples.xunit/blob/master/UseCulture/UseCultureAttribute.cs
#endregion

using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using Xunit.Sdk;

namespace Tests.Reproduce
{
/// <summary>
/// Apply this attribute to your test method to replace the
/// <see cref="Thread.CurrentThread" /> <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> with another culture.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class UseCultureAttribute : BeforeAfterTestAttribute
{
private readonly Lazy<CultureInfo> _culture;
private readonly Lazy<CultureInfo> _uiCulture;

private CultureInfo _originalCulture;
private CultureInfo _originalUiCulture;

/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" />
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <remarks>
/// <para>
/// This constructor overload uses <paramref name="culture" /> for both
/// <see cref="Culture" /> and <see cref="UICulture" />.
/// </para>
/// </remarks>
public UseCultureAttribute(string culture)
: this(culture, culture) { }

/// <summary>
/// Replaces the culture and UI culture of the current thread with
/// <paramref name="culture" /> and <paramref name="uiCulture" />
/// </summary>
/// <param name="culture">The name of the culture.</param>
/// <param name="uiCulture">The name of the UI culture.</param>
public UseCultureAttribute(string culture, string uiCulture)
{
_culture = new Lazy<CultureInfo>(() => new CultureInfo(culture, false));
_uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture, false));
}

/// <summary>
/// Gets the culture.
/// </summary>
public CultureInfo Culture => _culture.Value;

/// <summary>
/// Gets the UI culture.
/// </summary>
public CultureInfo UICulture => _uiCulture.Value;

/// <summary>
/// Stores the current <see cref="Thread.CurrentPrincipal" />
/// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
/// and replaces them with the new cultures defined in the constructor.
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = Thread.CurrentThread.CurrentCulture;
_originalUiCulture = Thread.CurrentThread.CurrentUICulture;

Thread.CurrentThread.CurrentCulture = Culture;
Thread.CurrentThread.CurrentUICulture = UICulture;

CultureInfo.CurrentCulture.ClearCachedData();
CultureInfo.CurrentUICulture.ClearCachedData();
}

/// <summary>
/// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
/// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />
/// </summary>
/// <param name="methodUnderTest">The method under test</param>
public override void After(MethodInfo methodUnderTest)
{
Thread.CurrentThread.CurrentCulture = _originalCulture;
Thread.CurrentThread.CurrentUICulture = _originalUiCulture;

CultureInfo.CurrentCulture.ClearCachedData();
CultureInfo.CurrentUICulture.ClearCachedData();
}
}
}