|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using NUnit.Framework; |
| 4 | +using Tomlyn.Serialization; |
| 5 | + |
| 6 | +namespace Tomlyn.Tests; |
| 7 | + |
| 8 | +public class NewApiAttributeContractTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public void ExportedTomlAttributes_HaveUniqueTypeNames() |
| 12 | + { |
| 13 | + var duplicateNames = typeof(TomlSerializer).Assembly |
| 14 | + .GetExportedTypes() |
| 15 | + .Where(static type => type.Name.StartsWith("Toml", StringComparison.Ordinal) && |
| 16 | + type.Name.EndsWith("Attribute", StringComparison.Ordinal)) |
| 17 | + .GroupBy(static type => type.Name, StringComparer.Ordinal) |
| 18 | + .Where(static group => group.Count() > 1) |
| 19 | + .Select(static group => group.Key) |
| 20 | + .ToArray(); |
| 21 | + |
| 22 | + Assert.That(duplicateNames, Is.Empty); |
| 23 | + } |
| 24 | + |
| 25 | + [Test] |
| 26 | + public void ExportedTomlAttributes_InheritTomlAttribute() |
| 27 | + { |
| 28 | + var nonDerivedAttributes = typeof(TomlSerializer).Assembly |
| 29 | + .GetExportedTypes() |
| 30 | + .Where(static type => type != typeof(TomlAttribute) && |
| 31 | + type.IsSubclassOf(typeof(Attribute)) && |
| 32 | + type.Name.StartsWith("Toml", StringComparison.Ordinal) && |
| 33 | + type.Name.EndsWith("Attribute", StringComparison.Ordinal)) |
| 34 | + .Where(static type => !typeof(TomlAttribute).IsAssignableFrom(type)) |
| 35 | + .Select(static type => type.FullName) |
| 36 | + .ToArray(); |
| 37 | + |
| 38 | + Assert.That(nonDerivedAttributes, Is.Empty); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void TomlPropertyNameAttribute_IsOnlyAvailableFromSerializationNamespace() |
| 43 | + { |
| 44 | + var exportedTypes = typeof(TomlSerializer).Assembly |
| 45 | + .GetExportedTypes() |
| 46 | + .Where(static type => type.Name == nameof(TomlPropertyNameAttribute)) |
| 47 | + .Select(static type => type.FullName) |
| 48 | + .OrderBy(static fullName => fullName, StringComparer.Ordinal) |
| 49 | + .ToArray(); |
| 50 | + |
| 51 | + Assert.That(exportedTypes, Is.EqualTo(new[] { "Tomlyn.Serialization.TomlPropertyNameAttribute" })); |
| 52 | + } |
| 53 | +} |
0 commit comments