using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SEP.Extensions.Tests
{
[TestClass]
public class InspectExtensionTests
{
[TestMethod]
public void ShouldInspectNull()
{
object o = null;
Assert.AreEqual("null", o.Inspect());
}
[TestMethod]
public void ShouldInspectBytes()
{
byte b = 0x80;
Assert.AreEqual("128", b.Inspect());
}
[TestMethod]
public void ShouldInspectChars()
{
Assert.AreEqual("'a'", 'a'.Inspect());
}
[TestMethod]
public void ShouldInspectIntegers()
{
Assert.AreEqual("99", 99.Inspect());
}
[TestMethod]
public void ShouldInspectLongs()
{
Assert.AreEqual("9999", 9999L.Inspect());
}
[TestMethod]
public void ShouldInspectStrings()
{
Assert.AreEqual("\"hello!\"", "hello!".Inspect());
}
[TestMethod]
public void ShouldInspectFloats()
{
Assert.AreEqual("1.5", 1.5F.Inspect());
}
[TestMethod]
public void ShouldInspectDoubles()
{
Assert.AreEqual("1.5", 1.5.Inspect());
}
[TestMethod]
public void ShouldInspectDecimals()
{
Assert.AreEqual("1.5", new Decimal(1.5).Inspect());
}
[TestMethod]
public void ShouldInspectDateTime()
{
Assert.AreEqual("1/1/2009 12:00:00 AM", new DateTime(2009, 1, 1).Inspect());
}
[TestMethod]
public void ShouldInspectStringPretendingToBeAnObject()
{
object o = "hello!";
Assert.AreEqual("\"hello!\"", o.Inspect());
}
[TestMethod]
public void ShouldInspectArray()
{
Assert.AreEqual("[1, 2, 3]", new object[] {1, 2, 3}.Inspect());
}
[TestMethod]
public void ShouldInspectList()
{
Assert.AreEqual("[1, 2, 3]", new List<object>() {1, 2, 3}.Inspect());
}
[TestMethod]
public void ShouldInspectDictionary()
{
var items = new Dictionary<string, int>() {{"key", 1}};
Assert.AreEqual("{\"key\" => 1}", items.Inspect());
}
[TestMethod]
public void ShouldReportProperties()
{
Assert.AreEqual("#<anon Name=\"Matt\", Level=4>", new {Name = "Matt", Level = 4}.Inspect());
}
[TestMethod]
public void ShouldNotRecurseIntoItemsInList()
{
var list = new List<object>();
list.Add(new List<string>());
Assert.AreEqual("[[...]]", list.Inspect());
}
[TestMethod]
public void ShouldNotRecurseIntoItemsInDictionary()
{
var dictionary = new Dictionary<string, object>();
dictionary["test"] = new Dictionary<string, int>();
AssertMatch("{\"test\" => {...}}",
dictionary.Inspect());
}
public class TestClass
{
public object Thing { get; set; }
internal object InternalThing { get; set; }
protected object ProtectedThing { get; set; }
private object PrivateThing { get; set; }
}
[TestMethod]
public void ShouldNotRecurseIntoItemsInObject()
{
var x = new TestClass();
x.Thing = new TestClass();
Assert.AreEqual(
"#<SEP.Extensions.Tests.InspectExtensionTests+TestClass Thing=#<SEP.Extensions.Tests.InspectExtensionTests+TestClass:0x11a2ccb>>",
x.Inspect());
}
public class TestGenericClass<T>
{
}
[TestMethod]
public void ShouldReportGenericTypes()
{
Assert.AreEqual("#<SEP.Extensions.Tests.InspectExtensionTests+TestGenericClass<System.Int32>>",
new TestGenericClass<int>().Inspect());
}
public class TestInspectableClass
{
public string Inspect()
{
return "inspection!";
}
}
[TestMethod]
public void ShouldInspectInspectable()
{
Assert.AreEqual("inspection!", new TestInspectableClass().Inspect());
}
[TestMethod]
public void ShouldInspectInspectableInList()
{
var list = new List<object>();
list.Add(new TestInspectableClass());
Assert.AreEqual("[inspection!]", list.Inspect());
}
[Flags]
public enum TestEnum
{
A = 0x01,
B = 0x02,
C = 0x04
}
[TestMethod]
public void ShouldInspectEnum()
{
Assert.AreEqual("A|B", (TestEnum.A | TestEnum.B).Inspect());
}
private void AssertMatch(string expectedPattern, string actual)
{
AssertMatch(new Regex(expectedPattern), actual);
}
private void AssertMatch(Regex expectedRegex, string actual)
{
Assert.IsTrue(expectedRegex.IsMatch(actual), "Expected to match: <" + expectedRegex + "> but was <" + actual + ">");
}
}
}