Skip to content

Commit 6f1da00

Browse files
author
par.dahlman
committed
Identifying indeces of numerical arguments
That will be used to preformat those args.
1 parent a7f1f3e commit 6f1da00

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Common.Logging.Serilog.Tests/SerilogPreformatterTests.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void Should_Return_Same_Template_String_If_No_Args_Provided()
4949
public void Should_Format_Entire_String_If_Only_Numeric_Formatting_Is_Used()
5050
{
5151
/* Setup */
52-
const string originalTemplate = "Look at this,a {0} formatted string!";
52+
const string originalTemplate = "Look at this, a {0} formatted string!";
5353
var args = new object[] {"nicely "};
5454
var expected = string.Format(originalTemplate, args);
5555

@@ -60,5 +60,34 @@ public void Should_Format_Entire_String_If_Only_Numeric_Formatting_Is_Used()
6060
Assert.That(result, Is.True);
6161
Assert.That(_resultTemplate, Is.EqualTo(expected));
6262
}
63+
64+
public class When_Calling_GetIndecesOfNumericalFormatting : SerilogPreformatterTests
65+
{
66+
[Test]
67+
public void Should_Return_Empty_List_If_Template_String_Is_Not_Formatted()
68+
{
69+
/* Setup */
70+
const string originalTemplate = "This is a template without any args";
71+
72+
/* Test */
73+
var result = _preformatter.GetIndecesOfNumericalFormatting(originalTemplate);
74+
75+
/* Assert */
76+
Assert.That(result, Is.Empty);
77+
}
78+
79+
[TestCase("A pure numeric string with {0} and {1}.", new[] {0,1})]
80+
[TestCase("A pure {@Serilog} formatted string", new int[0])]
81+
[TestCase("A mixed string with both {@Seri} and numeric {1} formatting", new[] {1})]
82+
public void Should_Return_Expected_Indeces(string originalTemplate, int[] expectedResult)
83+
{
84+
/* Setup */
85+
/* Test */
86+
var result = _preformatter.GetIndecesOfNumericalFormatting(originalTemplate);
87+
88+
/* Assert */
89+
Assert.That(result, Is.EquivalentTo(expectedResult));
90+
}
91+
}
6392
}
6493
}

src/Properties/AssemblyInfo.cs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// to COM components. If you need to access a type in this assembly from
1919
// COM, set the ComVisible attribute to true on that type.
2020
[assembly: ComVisible(false)]
21+
[assembly: InternalsVisibleTo("Common.Logging.Serilog.Tests")]
2122

2223
// The following GUID is for the ID of the typelib if this project is exposed to COM
2324
[assembly: Guid("f3753a5f-71d4-4fd5-9208-f79beb1de08c")]

src/SerilogPreformatter.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text.RegularExpressions;
24

35
namespace Common.Logging.Serilog
46
{
57
public class SerilogPreformatter
68
{
9+
private readonly Regex _numericFormattedRegex = new Regex(@"{(\d)}", RegexOptions.Compiled);
10+
711
public bool TryPreformat(string templateString, object[] args, out string newTemplate, out object[] newArgs)
812
{
913
if (string.IsNullOrEmpty(templateString))
@@ -24,5 +28,21 @@ public bool TryPreformat(string templateString, object[] args, out string newTem
2428
newArgs = null;
2529
return true;
2630
}
31+
32+
internal IEnumerable<int> GetIndecesOfNumericalFormatting(string templateString)
33+
{
34+
var matches = _numericFormattedRegex.Matches(templateString);
35+
36+
for (var i = 0; i < matches.Count; i++)
37+
{
38+
var numericMatch = matches[i].Groups
39+
.OfType<Group>()
40+
.Skip(1)
41+
.First()
42+
.Value;
43+
44+
yield return int.Parse(numericMatch);
45+
}
46+
}
2747
}
2848
}

0 commit comments

Comments
 (0)