Skip to content

Commit

Permalink
Refactor and unit test for ToSnakeCase extension method (#42)
Browse files Browse the repository at this point in the history
* Refactor and unit test for ToSnakeCase extension method

* Code cleanup

* Lock contention assertion should be GreaterThanOrEqualTo instead of EqualTo
  • Loading branch information
NazmiAltun authored Oct 11, 2020
1 parent 15fb9b4 commit 3507cb8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task Will_measure_contention_on_a_contested_lock()

// Why -1? The first thread will not contend the lock
const int numLocksContended = numThreads - 1;
Assert.That(() => StatsCollector.ContentionTotal.Value, Is.EqualTo(numLocksContended).After(200, 10));
Assert.That(() => StatsCollector.ContentionTotal.Value, Is.GreaterThanOrEqualTo(numLocksContended).After(200, 10));

// Pattern of expected contention times is: 50ms, 100ms, 150ms, etc.
var expectedDelay = TimeSpan.FromMilliseconds(Enumerable.Range(1, numLocksContended).Aggregate(sleepForMs, (acc, next) => acc + (sleepForMs * next)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using NUnit.Framework;
using Prometheus.DotNetRuntime.StatsCollectors;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.IntegrationTests
{
Expand All @@ -23,9 +20,9 @@ public void Will_measure_when_occurring_an_exception()

try
{
var result = 1 / divider;
_ = 1 / divider;
}
catch (System.DivideByZeroException divZeroEx)
catch (DivideByZeroException)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NUnit.Framework;
using Prometheus.DotNetRuntime.StatsCollectors.Util;

namespace Prometheus.DotNetRuntime.Tests.StatsCollectors.Util
{
public class StringExtensionsTests
{
[TestCase("", "")]
[TestCase("myGreatVariableName", "my_great_variable_name")]
[TestCase("my_great_variable_name", "my_great_variable_name")]
[TestCase("MyGreatVariableName", "my_great_variable_name")]
public void ToSnakeCase_Should_Convert_To_Snake_Case(string given, string expected)
{
Assert.AreEqual(given.ToSnakeCase(), expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ private void CleanupExpiredValues()

foreach (var key in _cache.Keys.ToArray())
{
CacheValue<TValue> value;
if (!_cache.TryGetValue(key, out value))
if (!_cache.TryGetValue(key, out var value))
continue;

if (value.TimeStamp < earliestAddedTime)
_cache.TryRemove(key, out var _);
_cache.TryRemove(key, out _);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;

namespace Prometheus.DotNetRuntime.StatsCollectors.Util
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prometheus.DotNetRuntime.StatsCollectors.Util
{
Expand All @@ -14,14 +13,8 @@ public class LabelGenerator
public static Dictionary<TEnum, string> MapEnumToLabelValues<TEnum>()
where TEnum : Enum
{
var dict = new Dictionary<TEnum, string>();

foreach (var v in Enum.GetValues(typeof(TEnum)).Cast<TEnum>())
{
dict.Add(v, Enum.GetName(typeof(TEnum), v).ToSnakeCase());
}

return dict;
return Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
.ToDictionary(k => k, v => Enum.GetName(typeof(TEnum), v).ToSnakeCase());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Threading;

namespace Prometheus.DotNetRuntime.StatsCollectors.Util
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,25 @@ public static class StringExtensions
public static string ToSnakeCase(this string str)
{
var sb = new StringBuilder();
bool lastCharWasUpper = false;
bool isFirst = true;
var lastCharWasUpper = false;

foreach (var c in str)
for(var i = 0 ; i < str.Length ; i++)
{
if (char.IsUpper(c))
if (char.IsUpper(str[i]))
{
if (!lastCharWasUpper && !isFirst)
if (!lastCharWasUpper && i != 0)
{
sb.Append("_");
}

sb.Append(char.ToLower(c));
sb.Append(char.ToLower(str[i]));
lastCharWasUpper = true;
}
else
{
sb.Append(c);
sb.Append(str[i]);
lastCharWasUpper = false;
}

isFirst = false;
}

return sb.ToString();
Expand Down

0 comments on commit 3507cb8

Please sign in to comment.