Skip to content

Commit 583b389

Browse files
committed
Fix: ToTrace extension method can handle null values.
1 parent 0f206ad commit 583b389

2 files changed

Lines changed: 83 additions & 7 deletions

File tree

src/SenseNet.Tools.Tests/SnTraceTests.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using SenseNet.Diagnostics;
34
using System.Linq;
45
using System.Reflection;
@@ -438,5 +439,58 @@ public void SnTrace_DynamicCategoryOnOff()
438439
Assert.AreEqual("0124579BCDE", actual);
439440
}
440441

442+
[TestMethod]
443+
public void SnTrace_ToTrace_String()
444+
{
445+
string s1 = null;
446+
var s2 = string.Empty;
447+
var s3 = "short";
448+
var s4 = "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong";
449+
450+
Assert.AreEqual(string.Empty, s1.ToTrace());
451+
Assert.AreEqual(string.Empty, s2.ToTrace());
452+
Assert.AreEqual("short", s3.ToTrace());
453+
Assert.AreEqual("looooooooooooooooooo", s4.ToTrace(20));
454+
}
455+
456+
[TestMethod]
457+
public void SnTrace_ToTrace_Array_Int()
458+
{
459+
int[] a1 = null;
460+
var a2 = Array.Empty<int>();
461+
var a3 = new[] { 1, 2 };
462+
463+
Assert.AreEqual(string.Empty, a1.ToTrace());
464+
Assert.AreEqual("[]", a2.ToTrace());
465+
Assert.AreEqual("[1, 2]", a3.ToTrace());
466+
Assert.AreEqual("[1, ...]", a3.ToTrace(1));
467+
}
468+
[TestMethod]
469+
public void SnTrace_ToTrace_Array_String()
470+
{
471+
string[] a1 = null;
472+
var a2 = Array.Empty<string>();
473+
var a3 = new[] { "s1", "s2", null };
474+
475+
Assert.AreEqual(string.Empty, a1.ToTrace());
476+
Assert.AreEqual("[]", a2.ToTrace());
477+
Assert.AreEqual("[s1, s2, ]", a3.ToTrace());
478+
Assert.AreEqual("[s1, ...]", a3.ToTrace(1));
479+
}
480+
481+
[TestMethod]
482+
public void SnTrace_ToTrace_Dictionary_StringString()
483+
{
484+
Dictionary<string, string> d1 = null;
485+
var d2 = new Dictionary<string, string>()
486+
{
487+
{ "key1", null },
488+
{ "key2", "value2" },
489+
{ "key3", "loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong" },
490+
};
491+
492+
Assert.AreEqual(string.Empty, d1.ToTrace());
493+
Assert.AreEqual("key1: {null}, key2: value2 (6), key3: looooooooooooooooooo... (77)", d2.ToTrace());
494+
}
441495
}
442496
}

src/SenseNet.Tools/Diagnostics/SnTraceExtensions.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,36 @@ namespace SenseNet.Diagnostics
66
{
77
public static class SnTraceExtensions
88
{
9-
public static string ToTrace(this string text, int maxLength = 100) =>
10-
text.Length < maxLength ? text : text.Substring(0, maxLength);
9+
public static string ToTrace(this string text, int maxLength = 100)
10+
{
11+
if (string.IsNullOrEmpty(text))
12+
return string.Empty;
1113

12-
public static string ToTrace(this IEnumerable<int> items, int maxCount = 32) =>
13-
Format(items.Take(maxCount + 1).Select(x => x.ToString()).ToArray(), maxCount);
14+
return text.Length < maxLength ? text : text.Substring(0, maxLength);
15+
}
1416

15-
public static string ToTrace(this IEnumerable<string> items, int maxCount = 10) =>
16-
Format(items.Take(maxCount + 1).ToArray(), maxCount);
17+
public static string ToTrace(this IEnumerable<int> items, int maxCount = 32) => items == null
18+
? string.Empty
19+
: Format(items.Take(maxCount + 1).Select(x => x.ToString()).ToArray(), maxCount);
20+
21+
public static string ToTrace(this IEnumerable<string> items, int maxCount = 10) => items == null
22+
? string.Empty
23+
: Format(items.Take(maxCount + 1).ToArray(), maxCount);
24+
25+
public static string ToTrace(this IDictionary<string, string> data)
26+
{
27+
if (data == null)
28+
return string.Empty;
29+
30+
return string.Join(", ", data.Select(x =>
31+
{
32+
if (x.Value == null)
33+
return $"{x.Key}: {{null}}";
34+
return $"{x.Key}: " +
35+
$"{(x.Value.Length > 20 ? x.Value.Substring(0, 20) + "..." : x.Value)} " +
36+
$"({x.Value.Length})";
37+
}));
38+
}
1739

1840
private static string Format(string[] set, int maxCount) =>
1941
$"[{string.Join(", ", set.Take(maxCount))}{(set.Length > maxCount ? ", ...]" : "]")}";

0 commit comments

Comments
 (0)