Skip to content

Commit 5462c5e

Browse files
Merge pull request #3854 from Sergio0694/tests/vector-parse-brackets
Added unit tests for Vector2/3/4/Quaternion parsing
2 parents dc5a00b + 66ba32e commit 5462c5e

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using System;
7+
using Microsoft.Toolkit.Uwp.UI;
8+
using System.Numerics;
9+
using System.Globalization;
10+
11+
namespace UnitTests.Extensions
12+
{
13+
[TestClass]
14+
public class Test_StringExtensions
15+
{
16+
[TestCategory("StringExtensions")]
17+
[TestMethod]
18+
[DataRow(0f)]
19+
[DataRow(3.14f)]
20+
public void Test_StringExtensions_ToVector2_XX(float x)
21+
{
22+
string text = x.ToString("R", CultureInfo.InvariantCulture);
23+
24+
Vector2
25+
expected = new(x),
26+
result = text.ToVector2();
27+
28+
Assert.AreEqual(expected, result);
29+
30+
result = $"<{text}>".ToVector2();
31+
32+
Assert.AreEqual(expected, result);
33+
}
34+
35+
[TestCategory("StringExtensions")]
36+
[TestMethod]
37+
[DataRow(0f, 0f)]
38+
[DataRow(0f, 22f)]
39+
[DataRow(3.14f, 3.24724928f)]
40+
[DataRow(float.MaxValue / 2, 0.3248f)]
41+
public void Test_StringExtensions_ToVector2_XYZW(float x, float y)
42+
{
43+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)}";
44+
45+
Vector2
46+
expected = new(x, y),
47+
result = text.ToVector2();
48+
49+
Assert.AreEqual(expected, result);
50+
51+
result = text.Replace(",", ", ").ToVector2();
52+
53+
Assert.AreEqual(expected, result);
54+
55+
result = $"<{text}>".ToVector2();
56+
57+
Assert.AreEqual(expected, result);
58+
}
59+
60+
[TestCategory("StringExtensions")]
61+
[TestMethod]
62+
[DataRow("")]
63+
[DataRow("Hello")]
64+
[DataRow("1, 2, 3")]
65+
[DataRow("<1, 2, 3")]
66+
[DataRow("<1, 2, 3>")]
67+
[DataRow("<1, 2, 3, 4>")]
68+
[ExpectedException(typeof(FormatException))]
69+
public void Test_StringExtensions_ToVector2_Fail(string text)
70+
{
71+
_ = text.ToVector2();
72+
}
73+
74+
[TestCategory("StringExtensions")]
75+
[TestMethod]
76+
[DataRow(0f)]
77+
[DataRow(3.14f)]
78+
public void Test_StringExtensions_ToVector3_XXX(float x)
79+
{
80+
string text = x.ToString("R", CultureInfo.InvariantCulture);
81+
82+
Vector3
83+
expected = new(x),
84+
result = text.ToVector3();
85+
86+
Assert.AreEqual(expected, result);
87+
88+
result = $"<{text}>".ToVector3();
89+
90+
Assert.AreEqual(expected, result);
91+
}
92+
93+
[TestCategory("StringExtensions")]
94+
[TestMethod]
95+
[DataRow(0f, 0f, 0f)]
96+
[DataRow(0f, 0f, 22f)]
97+
[DataRow(3.14f, 6.55f, 3.24724928f)]
98+
[DataRow(float.MaxValue / 2, 22f, 0.3248f)]
99+
public void Test_StringExtensions_ToVector3_XYZW(float x, float y, float z)
100+
{
101+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)}";
102+
103+
Vector3
104+
expected = new(x, y, z),
105+
result = text.ToVector3();
106+
107+
Assert.AreEqual(expected, result);
108+
109+
result = text.Replace(",", ", ").ToVector3();
110+
111+
Assert.AreEqual(expected, result);
112+
113+
result = $"<{text}>".ToVector3();
114+
115+
Assert.AreEqual(expected, result);
116+
}
117+
118+
[TestCategory("StringExtensions")]
119+
[TestMethod]
120+
[DataRow("")]
121+
[DataRow("Hello")]
122+
[DataRow("1, 2")]
123+
[DataRow("1, 2, 3, 99")]
124+
[DataRow("<1, 2>")]
125+
[DataRow("<1, 2, 3")]
126+
[DataRow("<1, 2, 3, 4>")]
127+
[ExpectedException(typeof(FormatException))]
128+
public void Test_StringExtensions_ToVector3_Fail(string text)
129+
{
130+
_ = text.ToVector3();
131+
}
132+
133+
[TestCategory("StringExtensions")]
134+
[TestMethod]
135+
[DataRow(0f)]
136+
[DataRow(3.14f)]
137+
public void Test_StringExtensions_ToVector4_XXXX(float x)
138+
{
139+
string text = x.ToString("R", CultureInfo.InvariantCulture);
140+
141+
Vector4
142+
expected = new(x),
143+
result = text.ToVector4();
144+
145+
Assert.AreEqual(expected, result);
146+
147+
result = $"<{text}>".ToVector4();
148+
149+
Assert.AreEqual(expected, result);
150+
}
151+
152+
[TestCategory("StringExtensions")]
153+
[TestMethod]
154+
[DataRow(0f, 0f, 0f, 0f)]
155+
[DataRow(0f, 0f, 22f, 6.89f)]
156+
[DataRow(3.14f, 6.55f, 3838f, 3.24724928f)]
157+
[DataRow(float.MaxValue / 2, float.Epsilon, 22f, 0.3248f)]
158+
public void Test_StringExtensions_ToVector4_XYZW(float x, float y, float z, float w)
159+
{
160+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)},{w.ToString("R", CultureInfo.InvariantCulture)}";
161+
162+
Vector4
163+
expected = new(x, y, z, w),
164+
result = text.ToVector4();
165+
166+
// Test the "1,2,3,4" format
167+
Assert.AreEqual(expected, result);
168+
169+
result = text.Replace(",", ", ").ToVector4();
170+
171+
// Test the "1, 2, 3, 4" format
172+
Assert.AreEqual(expected, result);
173+
174+
// Test the "<1, 2, 3, 4>" format
175+
result = $"<{text}>".ToVector4();
176+
177+
Assert.AreEqual(expected, result);
178+
}
179+
180+
[TestCategory("StringExtensions")]
181+
[TestMethod]
182+
[DataRow("")]
183+
[DataRow("Hello")]
184+
[DataRow("1, 2")]
185+
[DataRow("1, 2, 3")]
186+
[DataRow("1, 2, 3, 99, 100")]
187+
[DataRow("<1, 2, 3>")]
188+
[DataRow("<1, 2, 3, 4")]
189+
[DataRow("<1, 2, 3, 4, 5>")]
190+
[ExpectedException(typeof(FormatException))]
191+
public void Test_StringExtensions_ToVector4_Fail(string text)
192+
{
193+
_ = text.ToVector4();
194+
}
195+
196+
[TestCategory("StringExtensions")]
197+
[TestMethod]
198+
[DataRow(0f, 0f, 0f, 0f)]
199+
[DataRow(0f, 0f, 22f, 6.89f)]
200+
[DataRow(3.14f, 6.55f, 3838f, 3.24724928f)]
201+
[DataRow(float.MaxValue / 2, float.Epsilon, 22f, 0.3248f)]
202+
public void Test_StringExtensions_ToQuaternion_XYZW(float x, float y, float z, float w)
203+
{
204+
string text = $"{x.ToString("R", CultureInfo.InvariantCulture)},{y.ToString("R", CultureInfo.InvariantCulture)},{z.ToString("R", CultureInfo.InvariantCulture)},{w.ToString("R", CultureInfo.InvariantCulture)}";
205+
206+
Quaternion
207+
expected = new(x, y, z, w),
208+
result = text.ToQuaternion();
209+
210+
// Test the "1,2,3,4" format
211+
Assert.AreEqual(expected, result);
212+
213+
result = text.Replace(",", ", ").ToQuaternion();
214+
215+
// Test the "1, 2, 3, 4" format
216+
Assert.AreEqual(expected, result);
217+
218+
// Test the "<1, 2, 3, 4>" format
219+
result = $"<{text}>".ToQuaternion();
220+
221+
Assert.AreEqual(expected, result);
222+
}
223+
224+
[TestCategory("StringExtensions")]
225+
[TestMethod]
226+
[DataRow("")]
227+
[DataRow("Hello")]
228+
[DataRow("1, 2")]
229+
[DataRow("1, 2, 3")]
230+
[DataRow("1, 2, 3, 99, 100")]
231+
[DataRow("<1, 2, 3>")]
232+
[DataRow("<1, 2, 3, 4")]
233+
[DataRow("<1, 2, 3, 4, 5>")]
234+
[ExpectedException(typeof(FormatException))]
235+
public void Test_StringExtensions_ToQuaternion_Fail(string text)
236+
{
237+
_ = text.ToQuaternion();
238+
}
239+
}
240+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<Compile Include="Converters\Test_StringFormatConverter.cs" />
143143
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
144144
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
145+
<Compile Include="Extensions\Test_StringExtensions.cs" />
145146
<Compile Include="Extensions\Test_VisualTreeExtensions.cs" />
146147
<Compile Include="Extensions\Test_PointExtensions.cs" />
147148
<Compile Include="Extensions\Test_RectExtensions.cs" />

0 commit comments

Comments
 (0)