Skip to content

Commit

Permalink
Merge pull request #11 from Janus-vistaprint/BooleanFormatter
Browse files Browse the repository at this point in the history
Added boolean formatter
  • Loading branch information
cubsk authored Apr 18, 2018
2 parents cc4b9ec + dd51c9e commit d5cd41f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/OKLogger.Tests/FormattingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ObjectFormatter_SimpleList()
var objectFormatter = new ObjectFormatter(formatters, ",", 5);
var result = objectFormatter.Format(objectToFormat, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.Equal(result["Alpha"], "1,2,3,4,5");


Expand Down Expand Up @@ -124,7 +124,7 @@ public void ArrayFormatter_Int()
var arrayFormatter = new ArrayFormatter(",", Scrubber);
var result = arrayFormatter.Format(testArray, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "1,2,3,4");

Expand All @@ -140,7 +140,7 @@ public void ArrayFormatter_Double()
var arrayFormatter = new ArrayFormatter(",", Scrubber);
var result = arrayFormatter.Format(testArray, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "1.1,2.2,3.3,4.4");

Expand All @@ -154,7 +154,7 @@ public void ArrayFormatter_String()
var arrayFormatter = new ArrayFormatter(",", Scrubber);
var result = arrayFormatter.Format(testArray, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "alpha,beta,gamma");
}
Expand All @@ -167,7 +167,7 @@ public void GuidFormatter()
var formatter = new GuidFormatter();
var result = formatter.Format(testGuid, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "df0187e1-e1eb-4a9f-a528-4afebfecf4a5");
}
Expand All @@ -180,7 +180,7 @@ public void EnumFormatter()
var formatter = new EnumFormatter();
var result = formatter.Format(testval, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "SecondValue");
}
Expand All @@ -194,7 +194,7 @@ public void EnumFormatter_Nullable_WithValue()
var formatter = new EnumFormatter();
var result = formatter.Format(testval, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "SecondValue");
}
Expand All @@ -208,7 +208,7 @@ public void EnumFormatter_Nullable_NoValue()
var formatter = new EnumFormatter();
var result = formatter.Format(testval, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], string.Empty);
}
Expand All @@ -221,7 +221,7 @@ public void Nullable_Integer_With_Value()
var formatter = new NumericFormatter();
var result = formatter.Format(testVal, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], "3");
}
Expand All @@ -234,7 +234,7 @@ public void Nullable_Integer_No_Value()
var formatter = new NumericFormatter();
var result = formatter.Format(testVal, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal(result[string.Empty], string.Empty);
}
Expand Down Expand Up @@ -281,12 +281,34 @@ public void DateTimeFormatter()

var result = formatter.Format(testVal, 0);

Assert.Equal(1, result.Count);
Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal("1979-03-01 05:04:03Z", result[string.Empty]);
}

[Fact]
public void BooleanFormatter()
{
var formatter = new BooleanFormatter();
var result = formatter.Format(false, 0);

Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal("false", result[string.Empty]);

result = formatter.Format(true, 0);

Assert.Single(result);
Assert.True(result.ContainsKey(string.Empty), "Should have a single, empty key");
Assert.Equal("true", result[string.Empty]);

Assert.True(formatter.Handles(typeof(bool)), "Should handle boolean");
Assert.True(formatter.Handles(typeof(Boolean)), "Should handle boolean");

Assert.False(formatter.Handles(typeof(string)), "Should not handle non-boolean types");
Assert.False(formatter.Handles(typeof(object)), "Should not handle non-boolean types");

}


public enum SampleEnum
Expand Down
41 changes: 41 additions & 0 deletions src/OKLogger/Parsing/BooleanFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace OKLogger.Parsing
{
public class BooleanFormatter : IEntityFormatter
{
private HashSet<Type> HandledTypes = new HashSet<Type> {
typeof(bool), typeof(Boolean)
};

public bool Handles(Type t)
{
return HandledTypes.Contains(t);
}

public Dictionary<string, string> Format(object item, int depth)
{
if (item == null) return new Dictionary<string, string>() { { string.Empty, string.Empty } };

bool itemAsBoolean = (bool)item;
if(itemAsBoolean)
{
return new Dictionary<string, string>()
{
{ string.Empty, "true" }
};
}
else
{
return new Dictionary<string, string>()
{
{ string.Empty, "false" }
};
}

}
}
}

0 comments on commit d5cd41f

Please sign in to comment.