forked from datalust/seq-syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemingTests.cs
More file actions
296 lines (251 loc) · 10.4 KB
/
Copy pathThemingTests.cs
File metadata and controls
296 lines (251 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System.Text.Json.Nodes;
using Seq.Syntax.Templates;
using Seq.Syntax.Templates.Encoding;
using Seq.Syntax.Templates.Themes;
using Seq.Syntax.Tests.Support;
using Xunit;
namespace Seq.Syntax.Tests.Templates;
public class ThemingTests
{
static string Render(string template, JsonObject evt, TemplateOutputEncoder? encoder = null)
{
var compiled = new ExpressionTemplate(template, encoder: encoder);
var output = new StringWriter();
compiled.Format(evt, output);
return output.ToString();
}
static string RenderMarked(string template, JsonObject evt)
{
return Render(template, evt, encoder: new TemplateOutputEncoder(MarkerTheme.Instance));
}
static JsonObject MessageEvent(string messageTemplate) =>
new() { ["@t"] = "2026-08-30T01:02:03.0000000Z", ["@mt"] = messageTemplate };
[Fact]
public void MessageHoleScalarKindsSelectStyles()
{
var evt = MessageEvent("x{S}{C}{N}{D}{B}{Z}{G}{T}{Missing}");
evt["S"] = "s";
evt["C"] = JsonValue.Create('c');
evt["N"] = 1;
evt["D"] = JsonValue.Create(2.5);
evt["B"] = true;
evt["Z"] = null;
evt["G"] = JsonValue.Create(Guid.Empty);
evt["T"] = JsonValue.Create(new DateTimeOffset(2026, 8, 30, 1, 2, 3, TimeSpan.Zero));
Assert.Equal(
"<Text>x</Text>" +
"<String>s</String>" +
"<String>c</String>" +
"<Number>1</Number>" +
"<Number>2.5</Number>" +
"<Boolean>True</Boolean>" +
"<Null>null</Null>" +
$"<Scalar>{Guid.Empty}</Scalar>" +
"<Scalar>2026-08-30T01:02:03.0000000Z</Scalar>" +
"<Invalid>{Missing}</Invalid>",
RenderMarked("{@Message}", evt));
}
[Fact]
public void LiteralTextIsTertiaryTextMarkup()
{
Assert.Equal("<TertiaryText>hi </TertiaryText><SecondaryText>s</SecondaryText>",
RenderMarked("hi {'s'}", MessageEvent("-")));
}
[Fact]
public void StructuredValuesStyleThroughTheJsonWriter()
{
var evt = MessageEvent("-");
evt["A"] = new JsonObject { ["n"] = new JsonArray(1, "s", true, null) };
Assert.Equal(
"<TertiaryText>{</TertiaryText>" +
"<Name>\"n\"</Name>" +
"<TertiaryText>:</TertiaryText>" +
"<TertiaryText>[</TertiaryText>" +
"<Number>1</Number>" +
"<TertiaryText>,</TertiaryText>" +
"<String>\"s\"</String>" +
"<TertiaryText>,</TertiaryText>" +
"<Boolean>true</Boolean>" +
"<TertiaryText>,</TertiaryText>" +
"<Null>null</Null>" +
"<TertiaryText>]</TertiaryText>" +
"<TertiaryText>}</TertiaryText>",
RenderMarked("{A}", evt));
}
[Fact]
public void ExceptionLinesStyleByStackFramePrefix()
{
var evt = MessageEvent("-");
evt["@x"] = "System.Exception: boom\n at Frame.One()";
Assert.Equal(
"<Text>System.Exception: boom\n</Text>" +
"<SecondaryText> at Frame.One()\n</SecondaryText>",
RenderMarked("{@Exception}", evt).ReplaceLineEndings("\n"));
}
// The rendered level keeps the document's spelling; only the style follows the
// canonicalized name.
[Theory]
[InlineData("TRACE", "LevelVerbose")]
[InlineData("verbose", "LevelVerbose")]
[InlineData("dbug", "LevelDebug")]
[InlineData("info", "LevelInformation")]
[InlineData("notice", "LevelInformation")]
[InlineData("WARN", "LevelWarning")]
[InlineData("eror", "LevelError")]
[InlineData("fatal", "LevelFatal")]
[InlineData("critical", "LevelFatal")]
[InlineData("emerg", "LevelFatal")]
[InlineData("alert", "LevelFatal")]
[InlineData("panic", "LevelFatal")]
[InlineData("OK", "LevelInformation")]
public void LevelSpellingsStyleByCanonicalName(string level, string expectedStyle)
{
var evt = MessageEvent("-");
evt["@l"] = level;
Assert.Equal($"<{expectedStyle}>{level}</{expectedStyle}>", RenderMarked("{@Level}", evt));
}
[Fact]
public void AbsentLevelStylesAsInformation()
{
Assert.Equal("<LevelInformation>Information</LevelInformation>", RenderMarked("{@Level}", MessageEvent("-")));
}
[Fact]
public void LevelAlignmentPadsToVisibleWidth()
{
// "<LevelInformation>" + "</LevelInformation>" are invisible; "INF" pads to visible width 8.
Assert.Equal("<LevelInformation>INF</LevelInformation> ", RenderMarked("{@Level,-8:u3}", MessageEvent("-")));
}
[Fact]
public void MessageHoleAlignmentPadsToVisibleWidth()
{
var evt = MessageEvent("|{S,-6}|");
evt["S"] = "ab";
Assert.Equal("<Text>|</Text><String>ab</String> <Text>|</Text>", RenderMarked("{@Message}", evt));
}
[Fact]
public void MessageTokenAlignmentPadsToVisibleWidth()
{
Assert.Equal("<Text>hi</Text> ", RenderMarked("{@Message,-12}", MessageEvent("hi")));
}
[Fact]
public void FormattedExpressionAlignmentPadsToVisibleWidth()
{
var evt = MessageEvent("-");
evt["S"] = "ab";
Assert.Equal("<SecondaryText>ab</SecondaryText> ", RenderMarked("{S,-6}", evt));
}
// Adapted from Serilog.Expressions' `EncodingAppliesToThemedOutput`: there, the whole themed
// hole — ANSI sequences included — passed through the encoder. Here the escaper applies to
// content runs only, and the theme's own sequences are never mangled.
[Fact]
public void EscaperAppliesToContentWithinThemedRuns()
{
var evt = Some.InformationEvent("Hello, {Name}!", "nblumhardt");
var encoder = new TemplateOutputEncoder(StringHashPrefixingTheme.Instance, BracketingEscaper.Instance);
Assert.Equal("-[Hello, ]#[nblumhardt]\x1b[0m[!]-", Render("-{@Message}-", evt, encoder: encoder));
}
[Fact]
public void UnsafeBypassesEscaperButKeepsTheme()
{
var evt = MessageEvent("-");
evt["A"] = "x";
var encoder = new TemplateOutputEncoder(MarkerTheme.Instance, BracketingEscaper.Instance);
Assert.Equal("<SecondaryText>x</SecondaryText>", Render("{unsafe(A)}", evt, encoder: encoder));
Assert.Equal("<SecondaryText>[x]</SecondaryText>", Render("{A}", evt, encoder: encoder));
}
[Fact]
public void UnsafeUnderThemeWithoutEscaperIsRejected()
{
Assert.Throws<ArgumentException>(() => RenderMarked("{unsafe(A)}", MessageEvent("-")));
}
[Fact]
public void TruncatedMessageRecoversNeutralStyling()
{
var evt = MessageEvent(string.Concat(Enumerable.Repeat("{A}", 200)));
evt["A"] = new string('x', 1000);
var actual = Render("{@Message}", evt, encoder: TemplateOutputEncoder.Ansi(TemplateTheme.Code));
Assert.Equal(16 * 1024 + "\x1b[0m".Length, actual.Length);
Assert.EndsWith("\x1b[0m", actual);
}
[Fact]
public void CustomAnsiThemesOverrideBaseThemeStyles()
{
var custom = new AnsiTheme((AnsiTheme)TemplateTheme.Sixteen, new Dictionary<TemplateThemeStyle, string>
{
[TemplateThemeStyle.String] = "\x1b[38;5;159m"
});
var evt = MessageEvent("{A}");
evt["A"] = "x";
Assert.Equal("\x1b[38;5;159mx\x1b[0m", Render("{@Message}", evt, encoder: TemplateOutputEncoder.Ansi(custom)));
}
const string Hostile = "\x1b[31m<script>";
static JsonObject HostileEvent(bool includePreRenderedMessage)
{
var evt = new JsonObject
{
["@t"] = "2026-08-30T01:02:03.0000000Z",
["@l"] = Hostile,
["@mt"] = $"text{Hostile} {{A}} {{B.C}} {{Missing}}",
["@x"] = $"boom{Hostile}\n at Frame{Hostile}",
["A"] = Hostile,
["B"] = new JsonObject
{
["C"] = 1.5,
[$"name{Hostile}"] = new JsonArray(Hostile, 5, true, null, new JsonObject { [Hostile] = Hostile })
}
};
if (includePreRenderedMessage)
evt["@m"] = $"rendered {Hostile}";
return evt;
}
// The audit backing the design's classification of every write site: an event with hostile
// text laced through every field must not be able to smuggle a single escaper-significant
// character into the output.
[Theory]
[InlineData(true)]
[InlineData(false)]
public void HostileEventContentCannotReachTerminalOutput(bool includePreRenderedMessage)
{
var evt = HostileEvent(includePreRenderedMessage);
var encoder = new TemplateOutputEncoder(escaper: TemplateOutputEscaper.Terminal);
var actual = Render("{@Level} {@Message} {@Exception} {A,12} {B} {rest()}", evt, encoder: encoder);
Assert.DoesNotContain('\x1b', actual);
Assert.Contains("<script>", actual);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void HostileEventContentCannotReachHtmlOutput(bool includePreRenderedMessage)
{
var evt = HostileEvent(includePreRenderedMessage);
var actual = Render("{@Level} {@Message} {@Exception} {A,12} {B} {rest()}", evt,
encoder: TemplateOutputEncoder.Html);
Assert.DoesNotContain('<', actual);
Assert.Contains("<script>", actual);
}
[Fact]
public void AnsiThemedOutputIsTerminalSafeByDefault()
{
var actual = Render("{@Message}", HostileEvent(includePreRenderedMessage: true), encoder: TemplateOutputEncoder.Ansi(TemplateTheme.Code));
// The theme's own sequences survive; event-derived ESC characters do not.
Assert.Equal("\x1b[38;5;0253mrendered [31m<script>\x1b[0m", actual);
}
[Theory]
[InlineData("plain text", "plain text")]
[InlineData("keep\ttabs\r\nand newlines", "keep\ttabs\r\nand newlines")]
[InlineData("\x1b[31mred\x1b[0m", "[31mred[0m")]
[InlineData("nul\0bel\abs\b", "nulbelbs")]
[InlineData("del\u007Fc1\u0080\u009F-", "delc1-")]
public void TerminalEscaperStripsControlCharacters(string content, string expected)
{
Assert.Equal(expected, TemplateOutputEscaper.Terminal.Escape(content));
}
[Theory]
[InlineData("plain text", "plain text")]
[InlineData("a & b", "a & b")]
[InlineData("<b attr=\"v\">'q'</b>", "<b attr="v">'q'</b>")]
public void HtmlEscaperEscapesMarkupSignificantCharacters(string content, string expected)
{
Assert.Equal(expected, TemplateOutputEscaper.Html.Escape(content));
}
}