Пример #1
0
        public void WriteStringListAsYamlShouldMatchExpected(string[] stringValues, string expectedYaml)
        {
            // Arrange
            var outputString = new StringWriter(CultureInfo.InvariantCulture);
            var writer       = new OpenApiYamlWriter(outputString);

            // Act
            writer.WriteStartArray();
            foreach (var stringValue in stringValues)
            {
                writer.WriteValue(stringValue);
            }

            writer.WriteEndArray();
            writer.Flush();

            var actualYaml = outputString.GetStringBuilder()
                             .ToString()
                             .MakeLineBreaksEnvironmentNeutral();

            expectedYaml = expectedYaml.MakeLineBreaksEnvironmentNeutral();

            // Assert
            actualYaml.Should().Be(expectedYaml);
        }
Пример #2
0
        private void WriteValueRecursive(OpenApiYamlWriter writer, object value)
        {
            if (value == null ||
                value.GetType().IsPrimitive ||
                value is decimal ||
                value is string ||
                value is DateTimeOffset ||
                value is DateTime)
            {
                writer.WriteValue(value);
            }
            else if (value.GetType().IsGenericType&&
                     (typeof(IDictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition()) ||
                      typeof(Dictionary <,>).IsAssignableFrom(value.GetType().GetGenericTypeDefinition())))
            {
                writer.WriteStartObject();
                foreach (var elementValue in (dynamic)(value))
                {
                    writer.WritePropertyName(elementValue.Key);
                    WriteValueRecursive(writer, elementValue.Value);
                }

                writer.WriteEndObject();
            }
            else if (typeof(IEnumerable).IsAssignableFrom(value.GetType()))
            {
                writer.WriteStartArray();
                foreach (var elementValue in (IEnumerable)value)
                {
                    WriteValueRecursive(writer, elementValue);
                }

                writer.WriteEndArray();
            }
        }
Пример #3
0
        public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string expected)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiYamlWriter(outputStringWriter);

            // Act
            writer.WriteValue(input);
            var actual = outputStringWriter.GetStringBuilder().ToString();

            // Assert
            actual.Should().Be(expected);
        }
Пример #4
0
        public void WriteDateTimeAsJsonShouldMatchExpected(DateTimeOffset dateTimeOffset)
        {
            // Arrange
            var outputString = new StringWriter(CultureInfo.InvariantCulture);
            var writer       = new OpenApiYamlWriter(outputString);

            // Act
            writer.WriteValue(dateTimeOffset);

            var writtenString  = outputString.GetStringBuilder().ToString();
            var expectedString = " '" + dateTimeOffset.ToString("o") + "'";

            // Assert
            writtenString.Should().Be(expectedString);
        }
Пример #5
0
        public void WriteStringWithNewlineCharactersInArrayAsYamlWorks(string input, string expected)
        {
            // Arrange
            var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
            var writer             = new OpenApiYamlWriter(outputStringWriter)
            {
                UseLiteralStyle = true,
            };

            // Act
            writer.WriteStartArray();
            writer.WriteValue(input);
            var actual = outputStringWriter.GetStringBuilder().ToString()
                         // Normalize newline for cross platform
                         .Replace("\r", "");

            // Assert
            actual.Should().Be(expected);
        }