microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Common.Tests/UnionTests.cs
92lines · modecode
| 1 | using System.Text.Json; |
| 2 | |
| 3 | namespace Microsoft.Teams.Common.Tests; |
| 4 | |
| 5 | public class UnionTests |
| 6 | { |
| 7 | [Fact] |
| 8 | public void JsonSerialize() |
| 9 | { |
| 10 | var value = new Union<string, int, Dictionary<string, object>>("test"); |
| 11 | var json = JsonSerializer.Serialize(value); |
| 12 | Assert.Equal("\"test\"", json); |
| 13 | |
| 14 | value = 200; |
| 15 | json = JsonSerializer.Serialize(value); |
| 16 | Assert.Equal("200", json); |
| 17 | |
| 18 | value = new Dictionary<string, object>() |
| 19 | { |
| 20 | { "hello", "world" }, |
| 21 | { "test", 123 } |
| 22 | }; |
| 23 | |
| 24 | json = JsonSerializer.Serialize(value); |
| 25 | Assert.Equal("{\"hello\":\"world\",\"test\":123}", json); |
| 26 | } |
| 27 | |
| 28 | [Fact] |
| 29 | public void JsonDeserialize() |
| 30 | { |
| 31 | // Test deserializing string value |
| 32 | var stringJson = "\"test\""; |
| 33 | var stringResult = JsonSerializer.Deserialize<Union<string, int>>(stringJson); |
| 34 | Assert.Equal("test", stringResult.Value); |
| 35 | |
| 36 | // Test deserializing int value |
| 37 | var intJson = "200"; |
| 38 | var intResult = JsonSerializer.Deserialize<Union<string, int>>(intJson); |
| 39 | Assert.Equal(200, intResult.Value); |
| 40 | |
| 41 | // Test deserializing float value (relevant to Column.Width issue) |
| 42 | var floatJson = "1.5"; |
| 43 | var floatResult = JsonSerializer.Deserialize<Union<string, float>>(floatJson); |
| 44 | Assert.Equal(1.5f, floatResult.Value); |
| 45 | |
| 46 | // Test deserializing string value for Union<string, float> (the "auto" case) |
| 47 | var autoJson = "\"auto\""; |
| 48 | var autoResult = JsonSerializer.Deserialize<Union<string, float>>(autoJson); |
| 49 | Assert.Equal("auto", autoResult.Value); |
| 50 | |
| 51 | // Test deserializing complex object |
| 52 | var objectJson = "{\"hello\":\"world\",\"test\":123}"; |
| 53 | var objectResult = JsonSerializer.Deserialize<Union<string, Dictionary<string, object>>>(objectJson); |
| 54 | Assert.IsType<Dictionary<string, object>>(objectResult.Value); |
| 55 | var dict = (Dictionary<string, object>)objectResult.Value; |
| 56 | Assert.Equal("world", dict["hello"].ToString()); |
| 57 | Assert.Equal(123, ((System.Text.Json.JsonElement)dict["test"]).GetInt32()); |
| 58 | } |
| 59 | |
| 60 | [Fact] |
| 61 | public void IUnion_JsonSerialize() |
| 62 | { |
| 63 | // Test that IUnion interface properties serialize correctly (not as {"Value":x}) |
| 64 | IUnion<string, int> iUnionString = new Union<string, int>("test"); |
| 65 | var json = JsonSerializer.Serialize(iUnionString); |
| 66 | Assert.Equal("\"test\"", json); |
| 67 | |
| 68 | IUnion<string, int> iUnionInt = new Union<string, int>(200); |
| 69 | json = JsonSerializer.Serialize(iUnionInt); |
| 70 | Assert.Equal("200", json); |
| 71 | |
| 72 | // Test in object context (like TaskInfo.Width) |
| 73 | var obj = new { width = (IUnion<int, string>)new Union<int, string>(500) }; |
| 74 | json = JsonSerializer.Serialize(obj); |
| 75 | Assert.Equal("{\"width\":500}", json); |
| 76 | } |
| 77 | |
| 78 | [Fact] |
| 79 | public void IUnion_JsonDeserialize() |
| 80 | { |
| 81 | // Test deserializing to IUnion interface |
| 82 | var stringJson = "\"test\""; |
| 83 | var stringResult = JsonSerializer.Deserialize<IUnion<string, int>>(stringJson); |
| 84 | Assert.NotNull(stringResult); |
| 85 | Assert.Equal("test", stringResult.Value); |
| 86 | |
| 87 | var intJson = "200"; |
| 88 | var intResult = JsonSerializer.Deserialize<IUnion<string, int>>(intJson); |
| 89 | Assert.NotNull(intResult); |
| 90 | Assert.Equal(200, intResult.Value); |
| 91 | } |
| 92 | } |