|
| 1 | +namespace ByteBard.AsyncAPI.Tests.Validation; |
| 2 | + |
| 3 | +using System.Collections.Generic; |
| 4 | +using ByteBard.AsyncAPI.Readers; |
| 5 | +using FluentAssertions; |
| 6 | +using NUnit.Framework; |
| 7 | + |
| 8 | +public class KeyRegexTests |
| 9 | +{ |
| 10 | + [TestCase("chat.v1")] |
| 11 | + [TestCase("chat_01")] |
| 12 | + [TestCase("chat-1")] |
| 13 | + public void ChannelKey_RetainsExistingPattern(string key) |
| 14 | + { |
| 15 | + var input = $$""" |
| 16 | + asyncapi: 3.0.0 |
| 17 | + info: |
| 18 | + title: Key validation |
| 19 | + version: '1.0' |
| 20 | + channels: |
| 21 | + '{{key}}': {} |
| 22 | + """; |
| 23 | + |
| 24 | + var document = new AsyncApiStringReader().Read(input, out var diagnostic); |
| 25 | + |
| 26 | + diagnostic.Errors.Should().BeEmpty(); |
| 27 | + document.Channels.Should().ContainKey(key); |
| 28 | + } |
| 29 | + |
| 30 | + public static IEnumerable<TestCaseData> ServerKeys() |
| 31 | + { |
| 32 | + foreach (var version in new[] { "2.6.0", "3.0.0" }) |
| 33 | + { |
| 34 | + foreach (var key in new[] { "production", "Server_01", "server-1", "0" }) |
| 35 | + { |
| 36 | + yield return new TestCaseData(version, key, true); |
| 37 | + } |
| 38 | + |
| 39 | + foreach (var key in new[] { "server.one", "sérver", "server/name", "server name", "", "server\n", "server\r\n", "server\nother" }) |
| 40 | + { |
| 41 | + yield return new TestCaseData(version, key, false); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public static IEnumerable<TestCaseData> ComponentKeys() |
| 47 | + { |
| 48 | + foreach (var version in new[] { "2.6.0", "3.0.0" }) |
| 49 | + { |
| 50 | + foreach (var key in new[] { "Message", "message.v1", "message_01", "message-1", "0" }) |
| 51 | + { |
| 52 | + yield return new TestCaseData(version, key, true); |
| 53 | + } |
| 54 | + |
| 55 | + foreach (var key in new[] { "méssage", "消息", "message\u0301", "message/name", "message name", "" }) |
| 56 | + { |
| 57 | + yield return new TestCaseData(version, key, false); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + [TestCaseSource(nameof(ServerKeys))] |
| 63 | + public void ServerKey_UsesSpecificationPattern(string version, string key, bool valid) |
| 64 | + { |
| 65 | + var addressField = version == "2.6.0" ? "url" : "host"; |
| 66 | + var yamlKey = key.Replace("\r", "\\r").Replace("\n", "\\n"); |
| 67 | + var input = $$""" |
| 68 | + asyncapi: {{version}} |
| 69 | + info: |
| 70 | + title: Key validation |
| 71 | + version: '1.0' |
| 72 | + channels: |
| 73 | + chat: {} |
| 74 | + servers: |
| 75 | + "{{yamlKey}}": |
| 76 | + {{addressField}}: localhost |
| 77 | + protocol: kafka |
| 78 | + """; |
| 79 | + |
| 80 | + var document = new AsyncApiStringReader().Read(input, out var diagnostic); |
| 81 | + |
| 82 | + document.Servers.Should().ContainKey(key); |
| 83 | + if (valid) |
| 84 | + { |
| 85 | + diagnostic.Errors.Should().BeEmpty(); |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + diagnostic.Errors.Should().ContainSingle(e => e.Message.Contains("in 'servers' MUST match")); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [TestCaseSource(nameof(ComponentKeys))] |
| 94 | + public void ComponentKey_UsesAsciiSpecificationPattern(string version, string key, bool valid) |
| 95 | + { |
| 96 | + var input = $$""" |
| 97 | + asyncapi: {{version}} |
| 98 | + info: |
| 99 | + title: Key validation |
| 100 | + version: '1.0' |
| 101 | + channels: |
| 102 | + chat: {} |
| 103 | + components: |
| 104 | + messages: |
| 105 | + '{{key}}': |
| 106 | + name: example |
| 107 | + """; |
| 108 | + |
| 109 | + var document = new AsyncApiStringReader().Read(input, out var diagnostic); |
| 110 | + |
| 111 | + document.Components.Messages.Should().ContainKey(key); |
| 112 | + if (valid) |
| 113 | + { |
| 114 | + diagnostic.Errors.Should().BeEmpty(); |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + diagnostic.Errors.Should().ContainSingle(e => e.Message.Contains("in 'messages' MUST match")); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments