|
| 1 | +using Microsoft.Extensions.Logging; |
| 2 | +using Microsoft.Extensions.Logging.Abstractions; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace Netdocs.Core.Tests; |
| 6 | + |
| 7 | +public class TemplateBlockValidatorTests |
| 8 | +{ |
| 9 | + private sealed class TestLogger : ILogger |
| 10 | + { |
| 11 | + public List<string> Warnings { get; } = new(); |
| 12 | + |
| 13 | + public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; |
| 14 | + public bool IsEnabled(LogLevel logLevel) => true; |
| 15 | + public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) |
| 16 | + { |
| 17 | + if (logLevel == LogLevel.Warning) |
| 18 | + Warnings.Add(formatter(state, exception)); |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void DetectsNoDuplicates_EmitsNoWarning() |
| 24 | + { |
| 25 | + var logger = new TestLogger(); |
| 26 | + var validator = new Netdocs.Core.Templating.TemplateBlockValidator(logger); |
| 27 | + var content = "{% block header %}...{% endblock %}\n{% block content %}...{% endblock %}"; |
| 28 | + |
| 29 | + validator.Validate("test.html", content); |
| 30 | + |
| 31 | + Assert.Empty(logger.Warnings); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void DetectsDuplicateBlocks_EmitsWarning() |
| 36 | + { |
| 37 | + var logger = new TestLogger(); |
| 38 | + var validator = new Netdocs.Core.Templating.TemplateBlockValidator(logger); |
| 39 | + var content = "{% block header %}first{% endblock %}\n{% block header %}second{% endblock %}"; |
| 40 | + |
| 41 | + validator.Validate("test.html", content); |
| 42 | + |
| 43 | + Assert.Single(logger.Warnings); |
| 44 | + Assert.Contains("header", logger.Warnings[0]); |
| 45 | + Assert.Contains("2 definitions", logger.Warnings[0]); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void DetectsMultipleDuplicates_EmitsWarningPerBlock() |
| 50 | + { |
| 51 | + var logger = new TestLogger(); |
| 52 | + var validator = new Netdocs.Core.Templating.TemplateBlockValidator(logger); |
| 53 | + var content = @" |
| 54 | +{% block header %}a{% endblock %} |
| 55 | +{% block header %}b{% endblock %} |
| 56 | +{% block footer %}x{% endblock %} |
| 57 | +{% block footer %}y{% endblock %} |
| 58 | +{% block footer %}z{% endblock %} |
| 59 | +"; |
| 60 | + |
| 61 | + validator.Validate("test.html", content); |
| 62 | + |
| 63 | + Assert.Equal(2, logger.Warnings.Count); |
| 64 | + Assert.Contains("header", logger.Warnings[0]); |
| 65 | + Assert.Contains("footer", logger.Warnings[1]); |
| 66 | + } |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void CaseInsensitive_TreatsHeaderAndHEADERAsIdentical() |
| 70 | + { |
| 71 | + var logger = new TestLogger(); |
| 72 | + var validator = new Netdocs.Core.Templating.TemplateBlockValidator(logger); |
| 73 | + var content = "{% block header %}{% endblock %}\n{% block HEADER %}{% endblock %}"; |
| 74 | + |
| 75 | + validator.Validate("test.html", content); |
| 76 | + |
| 77 | + Assert.Single(logger.Warnings); |
| 78 | + } |
| 79 | +} |
0 commit comments