Skip to content

Commit 6de7de8

Browse files
authored
Tests for double and decimal CellValue ctors (#906)
* change cellvalue ctors to use CultureInfo.InvariantCulture * add culture test for double and decimal CellValue ctor
1 parent c2d4e99 commit 6de7de8

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
namespace DocumentFormat.OpenXml.Tests.Common
13+
{
14+
internal readonly struct CultureInfoTester : IDisposable
15+
{
16+
private readonly CultureInfo _old;
17+
18+
public CultureInfoTester(string desired)
19+
{
20+
_old = Thread.CurrentThread.CurrentCulture;
21+
Thread.CurrentThread.CurrentCulture = new CultureInfo(desired);
22+
}
23+
24+
public void Dispose()
25+
{
26+
Thread.CurrentThread.CurrentCulture = _old;
27+
}
28+
}
29+
}

test/DocumentFormat.OpenXml.Tests/Spreadsheet/CellValueTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using DocumentFormat.OpenXml.Spreadsheet;
5+
using DocumentFormat.OpenXml.Tests.Common;
56
using System;
67
using System.Collections.Generic;
8+
using System.Globalization;
9+
using System.Threading;
710
using Xunit;
811

912
namespace DocumentFormat.OpenXml.Tests
@@ -74,6 +77,30 @@ public void CellDoubleTest(double num)
7477
Assert.Equal(num, result);
7578
}
7679

80+
[InlineData("fr-FR")]
81+
[InlineData("de-DE")]
82+
[InlineData("zh-CH")]
83+
[InlineData("ru-RU")]
84+
[InlineData("en-US")]
85+
[InlineData("tr-TR")]
86+
[InlineData("ar-EG")]
87+
[InlineData("fa-IR")]
88+
[Theory]
89+
public void CellDoubleCultureTest(string culture)
90+
{
91+
// Change current culture
92+
using (new CultureInfoTester(culture))
93+
{
94+
// Set to a double value
95+
double num = 103.2;
96+
var value = new CellValue(num);
97+
98+
// Ensure that thread culture is not used.
99+
Assert.Equal("103.2", value.Text);
100+
Assert.Equal("103.2", value.InnerText);
101+
}
102+
}
103+
77104
[InlineData("987.6E+30", 9.876E+32)]
78105
[InlineData("-12.34E-20", -1.234E-19)]
79106
[Theory]
@@ -180,6 +207,30 @@ public void CellDecimalTestNegative(string input)
180207
Assert.False(value.TryGetDecimal(out _));
181208
}
182209

210+
[InlineData("fr-FR")]
211+
[InlineData("de-DE")]
212+
[InlineData("zh-CH")]
213+
[InlineData("ru-RU")]
214+
[InlineData("en-US")]
215+
[InlineData("tr-TR")]
216+
[InlineData("ar-EG")]
217+
[InlineData("fa-IR")]
218+
[Theory]
219+
public void CellDecimalCultureTest(string culture)
220+
{
221+
// Change current culture
222+
using (new CultureInfoTester(culture))
223+
{
224+
// Set to a decimal value
225+
decimal num = 6049.9M;
226+
var value = new CellValue(num);
227+
228+
// Ensure that thread culture is not used.
229+
Assert.Equal("6049.9", value.Text);
230+
Assert.Equal("6049.9", value.InnerText);
231+
}
232+
}
233+
183234
[InlineData("0", false)]
184235
[InlineData("false", false)]
185236
[InlineData("1", true)]

0 commit comments

Comments
 (0)