|
| 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 | + |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace System.Printing.IndexedProperties; |
| 7 | + |
| 8 | +public class PrintStringPropertyTests |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public void Constructor_Name() |
| 12 | + { |
| 13 | + using PrintStringProperty property = new("TestProperty"); |
| 14 | + property.Value.Should().BeNull(); |
| 15 | + property.Name.Should().Be("TestProperty"); |
| 16 | + bool disposed = property.TestAccessor().Dynamic.IsDisposed; |
| 17 | + disposed.Should().BeFalse(); |
| 18 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 19 | + initialized.Should().BeFalse(); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void Constructor_Name_Value() |
| 24 | + { |
| 25 | + using PrintStringProperty property = new("TestProperty", "TestValue"); |
| 26 | + property.Value.Should().Be("TestValue"); |
| 27 | + property.Name.Should().Be("TestProperty"); |
| 28 | + bool disposed = property.TestAccessor().Dynamic.IsDisposed; |
| 29 | + disposed.Should().BeFalse(); |
| 30 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 31 | + initialized.Should().BeFalse(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Constructor_Name_Value_Changed() |
| 36 | + { |
| 37 | + List<string?> changes = []; |
| 38 | + |
| 39 | + using PrintStringProperty property = new( |
| 40 | + "TestProperty", |
| 41 | + "TestValue", |
| 42 | + (PrintSystemDelegates.StringValueChanged)((string? value) => changes.Add(value))); |
| 43 | + |
| 44 | + property.Value.Should().Be("TestValue"); |
| 45 | + property.Name.Should().Be("TestProperty"); |
| 46 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 47 | + initialized.Should().BeFalse(); |
| 48 | + |
| 49 | + changes.Should().BeEquivalentTo(["TestValue"]); |
| 50 | + |
| 51 | + property.Value = new object(); |
| 52 | + changes.Should().BeEquivalentTo(["TestValue"]); |
| 53 | + |
| 54 | + property.Value = "SecondValue"; |
| 55 | + changes.Should().BeEquivalentTo(["TestValue", "SecondValue"]); |
| 56 | + |
| 57 | + property.Value = null; |
| 58 | + changes.Should().BeEquivalentTo(["TestValue", "SecondValue", null]); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Dispose() |
| 63 | + { |
| 64 | + PrintStringProperty property = new("TestProperty", "TestValue"); |
| 65 | + property.Dispose(); |
| 66 | + |
| 67 | + // Name and Value are set to null |
| 68 | + property.Value.Should().BeNull(); |
| 69 | + property.Name.Should().BeNull(); |
| 70 | + bool disposed = property.TestAccessor().Dynamic.IsDisposed; |
| 71 | + disposed.Should().BeTrue(); |
| 72 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 73 | + initialized.Should().BeFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Value_Set() |
| 78 | + { |
| 79 | + using PrintStringProperty property = new("TestProperty"); |
| 80 | + |
| 81 | + // Set to a string |
| 82 | + property.Value = "TestValue"; |
| 83 | + property.Value.Should().Be("TestValue"); |
| 84 | + property.Name.Should().Be("TestProperty"); |
| 85 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 86 | + initialized.Should().BeFalse(); |
| 87 | + |
| 88 | + // Set to non-string does nothing |
| 89 | + property.Value = new object(); |
| 90 | + property.Value.Should().Be("TestValue"); |
| 91 | + initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 92 | + initialized.Should().BeFalse(); |
| 93 | + |
| 94 | + // Set to null |
| 95 | + property.Value = null; |
| 96 | + property.Value.Should().BeNull(); |
| 97 | + initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 98 | + initialized.Should().BeFalse(); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public void Create_Name() |
| 103 | + { |
| 104 | + using var property = PrintStringProperty.Create("TestProperty"); |
| 105 | + |
| 106 | + property.Value.Should().BeNull(); |
| 107 | + property.Name.Should().Be("TestProperty"); |
| 108 | + bool disposed = property.TestAccessor().Dynamic.IsDisposed; |
| 109 | + disposed.Should().BeFalse(); |
| 110 | + bool initialized = property.TestAccessor().Dynamic.IsInitialized; |
| 111 | + initialized.Should().BeFalse(); |
| 112 | + } |
| 113 | +} |
0 commit comments