winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

TypeHelpersTests.cs (7197B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="TypeHelpersTests.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.UnitTests.Tests
      8 {
      9     using System.Collections;
     10     using System.Collections.Generic;
     11     using Microsoft.Management.Configuration.Processor.Helpers;
     12     using Microsoft.Management.Configuration.UnitTests.Fixtures;
     13     using Microsoft.Management.Configuration.UnitTests.Helpers;
     14     using Windows.Foundation.Collections;
     15     using Xunit;
     16     using Xunit.Abstractions;
     17 
     18     /// <summary>
     19     /// TypeHelpers tests.
     20     /// </summary>
     21     [Collection("UnitTestCollection")]
     22     [InProc]
     23     public class TypeHelpersTests
     24     {
     25         private readonly UnitTestFixture fixture;
     26         private readonly ITestOutputHelper log;
     27 
     28         /// <summary>
     29         /// Initializes a new instance of the <see cref="TypeHelpersTests"/> class.
     30         /// </summary>
     31         /// <param name="fixture">Unit test fixture.</param>
     32         /// <param name="log">Log helper.</param>
     33         public TypeHelpersTests(UnitTestFixture fixture, ITestOutputHelper log)
     34         {
     35             this.fixture = fixture;
     36             this.log = log;
     37         }
     38 
     39         private enum TestEnum
     40         {
     41             Value,
     42         }
     43 
     44         /// <summary>
     45         /// Tests PropertyExists.
     46         /// </summary>
     47         [Fact]
     48         public void PropertyExistsTests()
     49         {
     50             dynamic obj = new
     51             {
     52                 Property1 = "value",
     53             };
     54 
     55             Assert.True(TypeHelpers.PropertyExists(obj, "Property1"));
     56             Assert.False(TypeHelpers.PropertyExists(obj, "Property2"));
     57         }
     58 
     59         /// <summary>
     60         /// Tests PropertyWithTypeExists.
     61         /// </summary>
     62         [Fact]
     63         public void PropertyWithTypeExistsTests()
     64         {
     65             dynamic obj = new
     66             {
     67                 Property1 = "value",
     68                 Property2 = 42,
     69             };
     70 
     71             Assert.True(TypeHelpers.PropertyWithTypeExists<string>(obj, "Property1"));
     72             Assert.True(TypeHelpers.PropertyWithTypeExists<int>(obj, "Property2"));
     73 
     74             Assert.False(TypeHelpers.PropertyWithTypeExists<int>(obj, "Property1"));
     75             Assert.False(TypeHelpers.PropertyWithTypeExists<string>(obj, "Property2"));
     76         }
     77 
     78         /// <summary>
     79         /// Test PropertyExistsAndIsEnum.
     80         /// </summary>
     81         [Fact]
     82         public void PropertyExistsAndIsEnumTests()
     83         {
     84             dynamic obj = new
     85             {
     86                 Property1 = TestEnum.Value,
     87                 Property2 = "string",
     88             };
     89 
     90             Assert.True(TypeHelpers.PropertyExistsAndIsEnum(obj, "Property1"));
     91             Assert.False(TypeHelpers.PropertyExistsAndIsEnum(obj, "Property2"));
     92         }
     93 
     94         /// <summary>
     95         /// Tests PropertyExistsAndIsList.
     96         /// </summary>
     97         [Fact]
     98         public void PropertyExistsAndIsListTests()
     99         {
    100             dynamic obj = new
    101             {
    102                 Property1 = new List<string>() { "value" },
    103                 Property2 = "string",
    104             };
    105 
    106             Assert.True(TypeHelpers.PropertyExistsAndIsList(obj, "Property1"));
    107             Assert.False(TypeHelpers.PropertyExistsAndIsList(obj, "Property2"));
    108         }
    109 
    110         /// <summary>
    111         /// Tests GetAllPropertiesValues.
    112         /// </summary>
    113         [Fact]
    114         public void GetAllPropertiesValuesTests()
    115         {
    116             string s = "value";
    117             int i = 42;
    118             TestEnum e = TestEnum.Value;
    119             dynamic obj = new
    120             {
    121                 Property1 = s,
    122                 Property2 = i,
    123                 Property3 = e,
    124             };
    125 
    126             ValueSet set = TypeHelpers.GetAllPropertiesValues(obj);
    127             Assert.Equal(3, set.Count);
    128 
    129             Assert.True(set.ContainsKey("Property1"));
    130             Assert.True(set.TryGetValue("Property1", out object v1));
    131             Assert.Equal(s, v1 as string);
    132 
    133             Assert.True(set.ContainsKey("Property2"));
    134             Assert.True(set.TryGetValue("Property2", out object v2));
    135             Assert.Equal(i, (int)v2);
    136 
    137             Assert.True(set.ContainsKey("Property3"));
    138             Assert.True(set.TryGetValue("Property3", out object v3));
    139             Assert.Equal(e.ToString(), v3);
    140         }
    141 
    142         /// <summary>
    143         /// Verifies when a property is a Hashtable. It must be converted to a ValueSet.
    144         /// </summary>
    145         [Fact]
    146         public void GetAllPropertiesValuesTest_Hashtable()
    147         {
    148             string k1 = "key1";
    149             string k2 = "key2";
    150             int v1 = 7;
    151             string v2 = "value2";
    152             dynamic obj = new
    153             {
    154                 Property1 = new Hashtable
    155                 {
    156                     { k1, v1 },
    157                     { k2, v2 },
    158                 },
    159             };
    160 
    161             ValueSet set = TypeHelpers.GetAllPropertiesValues(obj);
    162             Assert.Single(set);
    163 
    164             Assert.True(set.ContainsKey("Property1"));
    165             Assert.True(set.TryGetValue("Property1", out object valueSetResultObj));
    166 
    167             ValueSet? valueSetResult = valueSetResultObj as ValueSet;
    168             Assert.NotNull(valueSetResult);
    169             Assert.Equal(2, valueSetResult.Count);
    170             Assert.True(valueSetResult.ContainsKey(k1));
    171             Assert.Equal(v1, (int)valueSetResult[k1]);
    172             Assert.True(valueSetResult.ContainsKey(k2));
    173             Assert.Equal(v2, (string)valueSetResult[k2]);
    174         }
    175 
    176         /// <summary>
    177         /// Verifies when a property is an array. It must generate a ValueSet
    178         /// where the keys are the index and a key treatAsArray means the value
    179         /// must be treated an array.
    180         /// </summary>
    181         [Fact]
    182         public void GetAllPropertiesValuesTest_Array()
    183         {
    184             dynamic obj = new
    185             {
    186                 Property1 = new int[]
    187                 {
    188                     1,
    189                     2,
    190                     3,
    191                     4,
    192                 },
    193             };
    194 
    195             ValueSet set = TypeHelpers.GetAllPropertiesValues(obj);
    196             Assert.Single(set);
    197 
    198             Assert.True(set.ContainsKey("Property1"));
    199             Assert.True(set.TryGetValue("Property1", out object valueSetResultObj));
    200 
    201             ValueSet? valueSetResult = valueSetResultObj as ValueSet;
    202             Assert.NotNull(valueSetResult);
    203             Assert.Equal(5, valueSetResult.Count);
    204 
    205             Assert.True(valueSetResult.ContainsKey("treatAsArray"));
    206             Assert.True(valueSetResult.ContainsKey("0"));
    207             Assert.True(valueSetResult.ContainsKey("1"));
    208             Assert.True(valueSetResult.ContainsKey("2"));
    209             Assert.True(valueSetResult.ContainsKey("3"));
    210         }
    211     }
    212 }