|
| 1 | +// ------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | +// ------------------------------------------------------------ |
| 5 | + |
| 6 | +using FluentAssertions; |
| 7 | +using Microsoft.OpenApi.Any; |
| 8 | +using Microsoft.OpenApi.Readers.ParseNodes; |
| 9 | +using Microsoft.OpenApi.Readers.V3; |
| 10 | +using SharpYaml.Serialization; |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | +using System.IO; |
| 14 | +using System.Linq; |
| 15 | +using System.Text; |
| 16 | +using System.Threading.Tasks; |
| 17 | +using Xunit; |
| 18 | + |
| 19 | +namespace Microsoft.OpenApi.Readers.Tests.V3Tests |
| 20 | +{ |
| 21 | + [Collection("DefaultSettings")] |
| 22 | + public class OpenApiAnyTests |
| 23 | + { |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void ParseMapAsAnyShouldSucceed() |
| 27 | + { |
| 28 | + |
| 29 | + var input = @" |
| 30 | +aString: fooBar |
| 31 | +aInteger: 10 |
| 32 | +aDouble: 2.34 |
| 33 | +aDateTime: 2017-01-01 |
| 34 | + "; |
| 35 | + var yamlStream = new YamlStream(); |
| 36 | + yamlStream.Load(new StringReader(input)); |
| 37 | + var yamlNode = yamlStream.Documents.First().RootNode; |
| 38 | + |
| 39 | + var context = new ParsingContext(); |
| 40 | + var diagnostic = new OpenApiDiagnostic(); |
| 41 | + |
| 42 | + var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode); |
| 43 | + |
| 44 | + var anyMap = node.CreateAny(); |
| 45 | + |
| 46 | + diagnostic.Errors.Should().BeEmpty(); |
| 47 | + |
| 48 | + anyMap.ShouldBeEquivalentTo( |
| 49 | + new OpenApiObject |
| 50 | + { |
| 51 | + ["aString"] = new OpenApiString("fooBar"), |
| 52 | + ["aInteger"] = new OpenApiInteger(10), |
| 53 | + ["aDouble"] = new OpenApiDouble(2.34), |
| 54 | + ["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01")) |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void ParseListAsAnyShouldSucceed() |
| 61 | + { |
| 62 | + |
| 63 | + var input = @" |
| 64 | +- fooBar |
| 65 | +- 10 |
| 66 | +- 2.34 |
| 67 | +- 2017-01-01 |
| 68 | + "; |
| 69 | + var yamlStream = new YamlStream(); |
| 70 | + yamlStream.Load(new StringReader(input)); |
| 71 | + var yamlNode = yamlStream.Documents.First().RootNode; |
| 72 | + |
| 73 | + var context = new ParsingContext(); |
| 74 | + var diagnostic = new OpenApiDiagnostic(); |
| 75 | + |
| 76 | + var node = new ListNode(context, diagnostic, (YamlSequenceNode)yamlNode); |
| 77 | + |
| 78 | + var any = node.CreateAny(); |
| 79 | + |
| 80 | + diagnostic.Errors.Should().BeEmpty(); |
| 81 | + |
| 82 | + any.ShouldBeEquivalentTo( |
| 83 | + new OpenApiArray |
| 84 | + { |
| 85 | + new OpenApiString("fooBar"), |
| 86 | + new OpenApiInteger(10), |
| 87 | + new OpenApiDouble(2.34), |
| 88 | + new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01")) |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void ParseScalarIntegertAsAnyShouldSucceed() |
| 94 | + { |
| 95 | + |
| 96 | + var input = @" |
| 97 | +10 |
| 98 | + "; |
| 99 | + var yamlStream = new YamlStream(); |
| 100 | + yamlStream.Load(new StringReader(input)); |
| 101 | + var yamlNode = yamlStream.Documents.First().RootNode; |
| 102 | + |
| 103 | + var context = new ParsingContext(); |
| 104 | + var diagnostic = new OpenApiDiagnostic(); |
| 105 | + |
| 106 | + var node = new ValueNode(context, diagnostic, (YamlScalarNode)yamlNode); |
| 107 | + |
| 108 | + var any = node.CreateAny(); |
| 109 | + |
| 110 | + diagnostic.Errors.Should().BeEmpty(); |
| 111 | + |
| 112 | + any.ShouldBeEquivalentTo( |
| 113 | + new OpenApiInteger(10) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments