Skip to content

Commit

Permalink
Tests for double and decimal CellValue ctors (#906)
Browse files Browse the repository at this point in the history
* change cellvalue ctors to use CultureInfo.InvariantCulture
* add culture test for double and decimal CellValue ctor
  • Loading branch information
tomjebo committed May 13, 2021
1 parent c2d4e99 commit 6de7de8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Common/CultureHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DocumentFormat.OpenXml.Tests.Common
{
internal readonly struct CultureInfoTester : IDisposable
{
private readonly CultureInfo _old;

public CultureInfoTester(string desired)
{
_old = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo(desired);
}

public void Dispose()
{
Thread.CurrentThread.CurrentCulture = _old;
}
}
}
51 changes: 51 additions & 0 deletions test/DocumentFormat.OpenXml.Tests/Spreadsheet/CellValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Tests.Common;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Xunit;

namespace DocumentFormat.OpenXml.Tests
Expand Down Expand Up @@ -74,6 +77,30 @@ public void CellDoubleTest(double num)
Assert.Equal(num, result);
}

[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("zh-CH")]
[InlineData("ru-RU")]
[InlineData("en-US")]
[InlineData("tr-TR")]
[InlineData("ar-EG")]
[InlineData("fa-IR")]
[Theory]
public void CellDoubleCultureTest(string culture)
{
// Change current culture
using (new CultureInfoTester(culture))
{
// Set to a double value
double num = 103.2;
var value = new CellValue(num);

// Ensure that thread culture is not used.
Assert.Equal("103.2", value.Text);
Assert.Equal("103.2", value.InnerText);
}
}

[InlineData("987.6E+30", 9.876E+32)]
[InlineData("-12.34E-20", -1.234E-19)]
[Theory]
Expand Down Expand Up @@ -180,6 +207,30 @@ public void CellDecimalTestNegative(string input)
Assert.False(value.TryGetDecimal(out _));
}

[InlineData("fr-FR")]
[InlineData("de-DE")]
[InlineData("zh-CH")]
[InlineData("ru-RU")]
[InlineData("en-US")]
[InlineData("tr-TR")]
[InlineData("ar-EG")]
[InlineData("fa-IR")]
[Theory]
public void CellDecimalCultureTest(string culture)
{
// Change current culture
using (new CultureInfoTester(culture))
{
// Set to a decimal value
decimal num = 6049.9M;
var value = new CellValue(num);

// Ensure that thread culture is not used.
Assert.Equal("6049.9", value.Text);
Assert.Equal("6049.9", value.InnerText);
}
}

[InlineData("0", false)]
[InlineData("false", false)]
[InlineData("1", true)]
Expand Down

0 comments on commit 6de7de8

Please sign in to comment.