|
| 1 | +// This file is part of YamlDotNet - A .NET library for YAML. |
| 2 | +// Copyright (c) Antoine Aubry and contributors |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +// this software and associated documentation files (the "Software"), to deal in |
| 6 | +// the Software without restriction, including without limitation the rights to |
| 7 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 | +// of the Software, and to permit persons to whom the Software is furnished to do |
| 9 | +// so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | +using FluentAssertions; |
| 24 | +using Xunit; |
| 25 | +using YamlDotNet.Core; |
| 26 | +using YamlDotNet.Serialization; |
| 27 | +using YamlDotNet.Serialization.NamingConventions; |
| 28 | + |
| 29 | +namespace YamlDotNet.Test.Serialization |
| 30 | +{ |
| 31 | + public class HiddenPropertyTests |
| 32 | + { |
| 33 | + public class HiddenPropertyBase |
| 34 | + { |
| 35 | + protected object value; |
| 36 | + |
| 37 | + public object Value => this.value; |
| 38 | + |
| 39 | + [YamlIgnore] |
| 40 | + public object SetOnly { set => this.value = value; } |
| 41 | + |
| 42 | + [YamlIgnore] |
| 43 | + public object GetAndSet { get; set; } |
| 44 | + } |
| 45 | + |
| 46 | + public class HiddenPropertyDerived<T> : HiddenPropertyBase |
| 47 | + { |
| 48 | + public new T Value { get => (T)this.value; } |
| 49 | + public new T SetOnly { set => this.value = value; } |
| 50 | + public new T GetAndSet { get; set; } |
| 51 | + } |
| 52 | + |
| 53 | + public class DuplicatePropertyBase |
| 54 | + { |
| 55 | + protected object value; |
| 56 | + |
| 57 | + public object Value => this.value; |
| 58 | + public object SetOnly { set => this.value = value; } |
| 59 | + public object GetAndSet { get; set; } |
| 60 | + } |
| 61 | + |
| 62 | + public class DuplicatePropertyDerived<T> : DuplicatePropertyBase |
| 63 | + { |
| 64 | + public new T Value { get => (T)this.value; } |
| 65 | + public new T SetOnly { set => this.value = value; } |
| 66 | + public new T GetAndSet { get; set; } |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void TestHidden() |
| 71 | + { |
| 72 | + var yaml = @" |
| 73 | +setOnly: set |
| 74 | +getAndSet: getAndSet |
| 75 | +"; |
| 76 | + var d = new DeserializerBuilder() |
| 77 | + .WithNamingConvention(CamelCaseNamingConvention.Instance) |
| 78 | + .Build(); |
| 79 | + |
| 80 | + var o = d.Deserialize<HiddenPropertyDerived<string>>(yaml); |
| 81 | + Assert.Equal("set", o.Value); |
| 82 | + Assert.Equal("getAndSet", o.GetAndSet); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void TestDuplicate() |
| 87 | + { |
| 88 | + var yaml = @" |
| 89 | +setOnly: set |
| 90 | +getAndSet: getAndSet |
| 91 | +"; |
| 92 | + var d = new DeserializerBuilder() |
| 93 | + .WithNamingConvention(CamelCaseNamingConvention.Instance) |
| 94 | + .Build(); |
| 95 | + |
| 96 | + Action action = () => { d.Deserialize<DuplicatePropertyDerived<string>>(yaml); }; |
| 97 | + action.ShouldThrow<YamlException>(); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments