|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.IO.Abstractions.TestingHelpers; |
| 6 | +using Elastic.Documentation.Diagnostics; |
| 7 | +using Elastic.Markdown.Myst.Directives; |
| 8 | +using Elastic.Markdown.Myst.Directives.Image; |
| 9 | +using FluentAssertions; |
| 10 | + |
| 11 | +namespace Elastic.Markdown.Tests.Directives; |
| 12 | + |
| 13 | +public class ImageCarouselBlockTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 14 | +""" |
| 15 | +:::{carousel} |
| 16 | +:id: my-carousel |
| 17 | +:fixed-height: medium |
| 18 | +
|
| 19 | +```{image} img/image1.png |
| 20 | +:alt: First image |
| 21 | +``` |
| 22 | +
|
| 23 | +```{image} img/image2.png |
| 24 | +:alt: Second image |
| 25 | +``` |
| 26 | +::: |
| 27 | +""" |
| 28 | +) |
| 29 | +{ |
| 30 | + protected override void AddToFileSystem(MockFileSystem fileSystem) |
| 31 | + { |
| 32 | + fileSystem.AddFile(@"docs/img/image1.png", ""); |
| 33 | + fileSystem.AddFile(@"docs/img/image2.png", ""); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void ParsesBlock() => Block.Should().NotBeNull(); |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void ParsesCarouselProperties() |
| 41 | + { |
| 42 | + Block!.Id.Should().Be("my-carousel"); |
| 43 | + Block!.FixedHeight.Should().Be("medium"); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void ProcessesNestedImages() |
| 48 | + { |
| 49 | + Block!.Images.Should().HaveCount(2); |
| 50 | + Block!.Images[0].Alt.Should().Be("First image"); |
| 51 | + Block!.Images[0].ImageUrl.Should().Be("/img/image1.png"); |
| 52 | + Block!.Images[1].Alt.Should().Be("Second image"); |
| 53 | + Block!.Images[1].ImageUrl.Should().Be("/img/image2.png"); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void AllImagesFoundSoNoErrorIsEmitted() |
| 58 | + { |
| 59 | + Block!.Images.Should().AllSatisfy(img => img.Found.Should().BeTrue()); |
| 60 | + Collector.Diagnostics.Count.Should().Be(0); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +public class ImageCarouselWithSmallHeightTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 65 | +""" |
| 66 | +:::{carousel} |
| 67 | +:fixed-height: small |
| 68 | +
|
| 69 | +```{image} img/small.png |
| 70 | +:alt: Small image |
| 71 | +``` |
| 72 | +::: |
| 73 | +""" |
| 74 | +) |
| 75 | +{ |
| 76 | + protected override void AddToFileSystem(MockFileSystem fileSystem) => |
| 77 | + fileSystem.AddFile(@"docs/img/small.png", ""); |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public void ParsesSmallFixedHeight() |
| 81 | + { |
| 82 | + Block!.FixedHeight.Should().Be("small"); |
| 83 | + Collector.Diagnostics.Count.Should().Be(0); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +public class ImageCarouselWithAutoHeightTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 88 | +""" |
| 89 | +:::{carousel} |
| 90 | +:fixed-height: auto |
| 91 | +
|
| 92 | +```{image} img/auto.png |
| 93 | +:alt: Auto height image |
| 94 | +``` |
| 95 | +::: |
| 96 | +""" |
| 97 | +) |
| 98 | +{ |
| 99 | + protected override void AddToFileSystem(MockFileSystem fileSystem) => |
| 100 | + fileSystem.AddFile(@"docs/img/auto.png", ""); |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void ParsesAutoFixedHeight() |
| 104 | + { |
| 105 | + Block!.FixedHeight.Should().Be("auto"); |
| 106 | + Collector.Diagnostics.Count.Should().Be(0); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +public class ImageCarouselWithInvalidHeightTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 111 | +""" |
| 112 | +:::{carousel} |
| 113 | +:fixed-height: large |
| 114 | +
|
| 115 | +```{image} img/invalid.png |
| 116 | +:alt: Invalid height image |
| 117 | +``` |
| 118 | +::: |
| 119 | +""" |
| 120 | +) |
| 121 | +{ |
| 122 | + protected override void AddToFileSystem(MockFileSystem fileSystem) => |
| 123 | + fileSystem.AddFile(@"docs/img/invalid.png", ""); |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void WarnsOnInvalidFixedHeight() |
| 127 | + { |
| 128 | + Block!.FixedHeight.Should().Be("large"); |
| 129 | + |
| 130 | + Collector.Diagnostics.Should().HaveCount(1) |
| 131 | + .And.OnlyContain(d => d.Severity == Severity.Warning); |
| 132 | + |
| 133 | + var warning = Collector.Diagnostics.First(); |
| 134 | + warning.Message.Should().Contain("Invalid fixed-height value 'large'"); |
| 135 | + warning.Message.Should().Contain("Valid options are: auto, small, medium"); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +public class ImageCarouselWithoutImagesTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 140 | +""" |
| 141 | +:::{carousel} |
| 142 | +:id: empty-carousel |
| 143 | +::: |
| 144 | +""" |
| 145 | +) |
| 146 | +{ |
| 147 | + [Fact] |
| 148 | + public void EmitsErrorForEmptyCarousel() |
| 149 | + { |
| 150 | + Block!.Images.Should().BeEmpty(); |
| 151 | + |
| 152 | + Collector.Diagnostics.Should().HaveCount(1) |
| 153 | + .And.OnlyContain(d => d.Severity == Severity.Error); |
| 154 | + |
| 155 | + var error = Collector.Diagnostics.First(); |
| 156 | + error.Message.Should().Be("carousel directive requires nested image directives"); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +public class ImageCarouselMinimalTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 161 | +""" |
| 162 | +:::{carousel} |
| 163 | +
|
| 164 | +```{image} img/minimal.png |
| 165 | +:alt: Minimal carousel |
| 166 | +``` |
| 167 | +::: |
| 168 | +""" |
| 169 | +) |
| 170 | +{ |
| 171 | + protected override void AddToFileSystem(MockFileSystem fileSystem) => |
| 172 | + fileSystem.AddFile(@"docs/img/minimal.png", ""); |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void ParsesMinimalCarousel() |
| 176 | + { |
| 177 | + Block!.Id.Should().BeNull(); |
| 178 | + Block!.FixedHeight.Should().BeNull(); |
| 179 | + Block!.Images.Should().HaveCount(1); |
| 180 | + Block!.Images[0].Alt.Should().Be("Minimal carousel"); |
| 181 | + Collector.Diagnostics.Count.Should().Be(0); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +public class ImageCarouselWithMissingImageTests(ITestOutputHelper output) : DirectiveTest<ImageCarouselBlock>(output, |
| 186 | +""" |
| 187 | +:::{carousel} |
| 188 | +
|
| 189 | +```{image} img/missing.png |
| 190 | +:alt: Missing image |
| 191 | +``` |
| 192 | +
|
| 193 | +```{image} img/exists.png |
| 194 | +:alt: Existing image |
| 195 | +``` |
| 196 | +::: |
| 197 | +""" |
| 198 | +) |
| 199 | +{ |
| 200 | + protected override void AddToFileSystem(MockFileSystem fileSystem) => |
| 201 | + fileSystem.AddFile(@"docs/img/exists.png", ""); |
| 202 | + |
| 203 | + [Fact] |
| 204 | + public void HandlesPartiallyMissingImages() |
| 205 | + { |
| 206 | + Block!.Images.Should().HaveCount(2); |
| 207 | + Block!.Images[0].Found.Should().BeFalse(); // missing.png |
| 208 | + Block!.Images[1].Found.Should().BeTrue(); // exists.png |
| 209 | + |
| 210 | + // Should have diagnostics for the missing image |
| 211 | + Collector.Diagnostics.Should().HaveCount(1) |
| 212 | + .And.OnlyContain(d => d.Severity == Severity.Error); |
| 213 | + } |
| 214 | +} |
0 commit comments