HelperTest.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  
 7  using Microsoft.PowerToys.Settings.UI.Library.Utilities;
 8  using Microsoft.VisualStudio.TestTools.UnitTesting;
 9  
10  namespace CommonLibTest
11  {
12      [TestClass]
13      public class HelperTest
14      {
15          public static void TestStringIsSmaller(string v1, string v2)
16          {
17              var res = Helper.CompareVersions(v1, v2);
18              Assert.IsTrue(res < 0);
19          }
20  
21          public static void TestStringsAreEqual(string v1, string v2)
22          {
23              var res = Helper.CompareVersions(v1, v2);
24              Assert.IsTrue(res == 0);
25          }
26  
27          [TestMethod]
28          public void HelperCompareVersionsShouldBeEqualWhenSuccessful()
29          {
30              TestStringsAreEqual("v0.0.0", "v0.0.0");
31              TestStringsAreEqual("v0.1.1", "v0.1.1");
32              TestStringsAreEqual("v1.1.1", "v1.1.1");
33              TestStringsAreEqual("v1.999.99", "v1.999.99");
34          }
35  
36          [TestMethod]
37          public void HelperCompareVersionsShouldBeSmallerWhenSuccessful()
38          {
39              TestStringIsSmaller("v0.0.0", "v0.0.1");
40              TestStringIsSmaller("v0.0.0", "v0.1.0");
41              TestStringIsSmaller("v0.0.0", "v1.0.0");
42              TestStringIsSmaller("v1.0.1", "v1.0.2");
43              TestStringIsSmaller("v1.1.1", "v1.1.2");
44              TestStringIsSmaller("v1.1.1", "v1.2.0");
45              TestStringIsSmaller("v1.999.99", "v2.0.0");
46              TestStringIsSmaller("v1.0.99", "v1.2.0");
47          }
48  
49          [TestMethod]
50          [ExpectedException(typeof(FormatException))]
51          public void HelperCompareVersionsShouldThrowBadFormatWhenNoVersionString()
52          {
53              Helper.CompareVersions("v0.0.1", string.Empty);
54          }
55  
56          [TestMethod]
57          [ExpectedException(typeof(FormatException))]
58          public void HelperCompareVersionsShouldThrowBadFormatWhenShortVersionString()
59          {
60              Helper.CompareVersions("v0.0.1", "v0.1");
61          }
62  
63          [TestMethod]
64          [ExpectedException(typeof(FormatException))]
65          public void HelperCompareVersionsShouldThrowBadFormatWhenLongVersionString()
66          {
67              Helper.CompareVersions("v0.0.1", "v0.0.0.1");
68          }
69  
70          [TestMethod]
71          [ExpectedException(typeof(FormatException))]
72          public void HelperCompareVersionsShouldThrowBadFormatWhenItIsNotAVersionString()
73          {
74              Helper.CompareVersions("v0.0.1", "HelloWorld");
75          }
76      }
77  }