Skip to content

Commit f7fad45

Browse files
committed
add PluginGraphAttributesBuilder.WithTitle() to be able to overwrite graph_title
1 parent 96dd822 commit f7fad45

File tree

2 files changed

+85
-33
lines changed

2 files changed

+85
-33
lines changed

src/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/PluginGraphAttributesBuilder.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// SPDX-License-Identifier: MIT
33
using System;
44
using System.Collections.Generic;
5+
#if SYSTEM_DIAGNOSTICS_CODEANALYSIS_MEMBERNOTNULLATTRIBUTE
6+
using System.Diagnostics.CodeAnalysis;
7+
#endif
58
using System.Text.RegularExpressions;
69

710
namespace Smdn.Net.MuninPlugin;
@@ -42,8 +45,7 @@ private static void ThrowIfNotMatch(Regex regex, string input, string paramName,
4245
}
4346
}
4447

45-
private readonly string title;
46-
48+
private string title;
4749
private bool? showGraph;
4850
private string? category;
4951
private int? height;
@@ -106,14 +108,14 @@ public PluginGraphAttributesBuilder(string title, PluginGraphAttributesBuilder b
106108
/// <paramref name="title"/> contains invalid characters.
107109
/// </exception>
108110
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#graph-title">Plugin reference - Global attributes - graph_title</seealso>
111+
#if !SYSTEM_DIAGNOSTICS_CODEANALYSIS_MEMBERNOTNULLATTRIBUTE
112+
#pragma warning disable CS8618
113+
#endif
109114
public PluginGraphAttributesBuilder(string title)
110115
{
111-
ArgumentExceptionShim.ThrowIfNullOrWhiteSpace(title, nameof(title));
112-
113-
ThrowIfNotMatch(RegexTitle, title, nameof(title), "graph_title");
114-
115-
this.title = title;
116+
WithTitle(title);
116117
}
118+
#pragma warning restore CS8618
117119

118120
/// <summary>Sets a value for the <c>graph</c> to <c>yes</c>.</summary>
119121
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#graph">Plugin reference - Global attributes - graph</seealso>
@@ -231,6 +233,27 @@ public PluginGraphAttributesBuilder DisableUnitScaling()
231233
return this;
232234
}
233235

236+
/// <summary>Overwrites a value for the <c>graph_title</c>.</summary>
237+
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#graph-title">Plugin reference - Global attributes - graph_title</seealso>
238+
/// <exception cref="ArgumentNullException"><paramref name="title"/> is <see langword="null"/>.</exception>
239+
/// <exception cref="ArgumentException">
240+
/// <paramref name="title"/> is empty, or
241+
/// <paramref name="title"/> contains invalid characters.
242+
/// </exception>
243+
#if SYSTEM_DIAGNOSTICS_CODEANALYSIS_MEMBERNOTNULLATTRIBUTE
244+
[MemberNotNull(nameof(title))]
245+
#endif
246+
public PluginGraphAttributesBuilder WithTitle(string title)
247+
{
248+
ArgumentExceptionShim.ThrowIfNullOrWhiteSpace(title, nameof(title));
249+
250+
ThrowIfNotMatch(RegexTitle, title, nameof(title), "graph_title");
251+
252+
this.title = title;
253+
254+
return this;
255+
}
256+
234257
/// <summary>Sets a value for the <c>graph_total</c>.</summary>
235258
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#graph-total">Plugin reference - Global attributes - graph_total</seealso>
236259
/// <exception cref="ArgumentNullException"><paramref name="labelForTotal"/> is <see langword="null"/>.</exception>

tests/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/PluginGraphAttributesBuilder.cs

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,41 @@ namespace Smdn.Net.MuninPlugin;
1010

1111
[TestFixture]
1212
public partial class PluginGraphAttributesBuilderTests {
13-
[TestCase(null, typeof(ArgumentNullException))]
14-
[TestCase("", typeof(ArgumentException))]
15-
[TestCase(" ", typeof(ArgumentException))]
16-
[TestCase("\0", typeof(ArgumentException))]
17-
[TestCase("\0title", typeof(ArgumentException))]
18-
[TestCase("\ntitle", typeof(ArgumentException))]
19-
[TestCase("\rtitle", typeof(ArgumentException))]
20-
[TestCase("\ttitle", typeof(ArgumentException))]
21-
[TestCase("title\0", typeof(ArgumentException))]
22-
[TestCase("title\n", typeof(ArgumentException))]
23-
[TestCase("title\r", typeof(ArgumentException))]
24-
[TestCase("title\t", typeof(ArgumentException))]
13+
private static System.Collections.IEnumerable YieldTestCases_WithTitle_ArgumentException()
14+
{
15+
yield return new object?[] { null, typeof(ArgumentNullException) };
16+
yield return new object?[] { "", typeof(ArgumentException) };
17+
yield return new object?[] { " ", typeof(ArgumentException) };
18+
yield return new object?[] { "\0", typeof(ArgumentException) };
19+
yield return new object?[] { "\0title", typeof(ArgumentException) };
20+
yield return new object?[] { "\ntitle", typeof(ArgumentException) };
21+
yield return new object?[] { "\rtitle", typeof(ArgumentException) };
22+
yield return new object?[] { "\ttitle", typeof(ArgumentException) };
23+
yield return new object?[] { "title\0", typeof(ArgumentException) };
24+
yield return new object?[] { "title\n", typeof(ArgumentException) };
25+
yield return new object?[] { "title\r", typeof(ArgumentException) };
26+
yield return new object?[] { "title\t", typeof(ArgumentException) };
27+
}
28+
29+
private static System.Collections.IEnumerable YieldTestCases_WithTitle()
30+
{
31+
yield return new object[] { "t" };
32+
yield return new object[] { "title" };
33+
yield return new object[] { "Title" };
34+
yield return new object[] { "TITLE" };
35+
yield return new object[] { "title0" };
36+
yield return new object[] { "title9" };
37+
yield return new object[] { "0title" };
38+
yield return new object[] { "9title" };
39+
yield return new object[] { "title-x" };
40+
yield return new object[] { ".title." };
41+
yield return new object[] { "'title'" };
42+
yield return new object[] { "title - title" };
43+
yield return new object[] { "<title>" };
44+
yield return new object[] { "タイトル" };
45+
}
46+
47+
[TestCaseSource(nameof(YieldTestCases_WithTitle_ArgumentException))]
2548
public void Ctor_ArgumentException(string? title, Type expectedTypeOfException)
2649
=> Assert.That(
2750
() => new PluginGraphAttributesBuilder(
@@ -43,20 +66,7 @@ IEnumerable<string> expectedAttributeList
4366
Is.EquivalentTo(expectedAttributeList).Using((IEqualityComparer<string>)StringComparer.Ordinal)
4467
);
4568

46-
[TestCase("t")]
47-
[TestCase("title")]
48-
[TestCase("Title")]
49-
[TestCase("TITLE")]
50-
[TestCase("title0")]
51-
[TestCase("title9")]
52-
[TestCase("0title")]
53-
[TestCase("9title")]
54-
[TestCase("title-x")]
55-
[TestCase(".title.")]
56-
[TestCase("'title'")]
57-
[TestCase("title - title")]
58-
[TestCase("<title>")]
59-
[TestCase("タイトル")]
69+
[TestCaseSource(nameof(YieldTestCases_WithTitle))]
6070
public void Ctor(string title)
6171
=> AssertBuiltGraphAttributes(
6272
new PluginGraphAttributesBuilder(title),
@@ -281,6 +291,25 @@ public void DisableUnitScaling()
281291
["graph_title title", "graph_scale no"]
282292
);
283293

294+
[TestCaseSource(nameof(YieldTestCases_WithTitle_ArgumentException))]
295+
public void WithTitle_ArgumentException(string? title, Type expectedTypeOfException)
296+
=> Assert.That(
297+
() => new PluginGraphAttributesBuilder("title")
298+
.WithTitle(title: title!),
299+
Throws
300+
.TypeOf(expectedTypeOfException)
301+
.With
302+
.Property(nameof(ArgumentException.ParamName))
303+
.EqualTo("title")
304+
);
305+
306+
[TestCaseSource(nameof(YieldTestCases_WithTitle))]
307+
public void WithTitle(string title)
308+
=> AssertBuiltGraphAttributes(
309+
new PluginGraphAttributesBuilder("title").WithTitle(title),
310+
[$"graph_title {title}"]
311+
);
312+
284313
[TestCase(null, typeof(ArgumentNullException))]
285314
[TestCase("", typeof(ArgumentException))]
286315
[TestCase(" ", typeof(ArgumentException))]

0 commit comments

Comments
 (0)