HashtableExtensionsTests.cs (4324B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="HashtableExtensionsTests.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 Microsoft.Management.Configuration.Processor.Exceptions; 11 using Microsoft.Management.Configuration.Processor.Extensions; 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 /// Hashtable extension tests. 20 /// </summary> 21 [Collection("UnitTestCollection")] 22 [InProc] 23 public class HashtableExtensionsTests 24 { 25 private readonly UnitTestFixture fixture; 26 private readonly ITestOutputHelper log; 27 28 /// <summary> 29 /// Initializes a new instance of the <see cref="HashtableExtensionsTests"/> class. 30 /// </summary> 31 /// <param name="fixture">Unit test fixture.</param> 32 /// <param name="log">Log helper.</param> 33 public HashtableExtensionsTests(UnitTestFixture fixture, ITestOutputHelper log) 34 { 35 this.fixture = fixture; 36 this.log = log; 37 } 38 39 /// <summary> 40 /// Tests ToValueSet with simple types. 41 /// </summary> 42 [Fact] 43 public void ToValueSet_Test() 44 { 45 var ht = new Hashtable() 46 { 47 { "key1", "value1" }, 48 { "key2", 2 }, 49 { "key3", true }, 50 }; 51 52 var valueSet = ht.ToValueSet(); 53 54 Assert.True(valueSet.ContainsKey("key1")); 55 Assert.Equal("value1", (string)valueSet["key1"]); 56 57 Assert.True(valueSet.ContainsKey("key2")); 58 Assert.Equal(2, (int)valueSet["key2"]); 59 60 Assert.True(valueSet.ContainsKey("key3")); 61 Assert.True((bool)valueSet["key3"]); 62 } 63 64 /// <summary> 65 /// Test for inner hashtables. 66 /// </summary> 67 [Fact] 68 public void ToValueSet_InnerHashtable() 69 { 70 var ht = new Hashtable() 71 { 72 { 73 "hashtableKey", new Hashtable() 74 { 75 { "key1", "value1" }, 76 { "key2", 2 }, 77 { "key3", true }, 78 } 79 }, 80 }; 81 82 var valueSet = ht.ToValueSet(); 83 84 Assert.True(valueSet.ContainsKey("hashtableKey")); 85 var resultValueSet = (ValueSet)valueSet["hashtableKey"]; 86 87 Assert.True(resultValueSet.ContainsKey("key1")); 88 Assert.Equal("value1", (string)resultValueSet["key1"]); 89 90 Assert.True(resultValueSet.ContainsKey("key2")); 91 Assert.Equal(2, (int)resultValueSet["key2"]); 92 93 Assert.True(resultValueSet.ContainsKey("key3")); 94 Assert.True((bool)resultValueSet["key3"]); 95 } 96 97 /// <summary> 98 /// Test for inner arrays. 99 /// </summary> 100 [Fact] 101 public void ToValueSet_InnerArray() 102 { 103 var ht = new Hashtable() 104 { 105 { 106 "arrayKey", new string[] 107 { 108 "s1", 109 "s2", 110 "s3", 111 } 112 }, 113 }; 114 115 var valueSet = ht.ToValueSet(); 116 117 Assert.True(valueSet.ContainsKey("arrayKey")); 118 var resultValueSet = (ValueSet)valueSet["arrayKey"]; 119 Assert.True(resultValueSet.ContainsKey("treatAsArray")); 120 Assert.Equal(4, resultValueSet.Count); 121 } 122 123 /// <summary> 124 /// Test when a key is not a string. 125 /// </summary> 126 [Fact] 127 public void ToValueSet_KeyNotString() 128 { 129 var ht = new Hashtable() 130 { 131 { 1, "value" }, 132 }; 133 134 Assert.Throws<UnitPropertyUnsupportedException>(() => ht.ToValueSet()); 135 } 136 } 137 }