Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Ordering #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion StatePrinter.Tests/Configurations/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Internal;
using StatePrinting.Orderers;
using StatePrinting.Configurations;
using StatePrinting.FieldHarvesters;
using StatePrinting.TestAssistance;
Expand All @@ -32,7 +33,7 @@ namespace StatePrinting.Tests.Configurations
class ConfigurationTest
{
[Test]
public void TryFind()
public void TryGetValueConverter()
{
var config = new Configuration();
config.Add(new StandardTypesConverter(null));
Expand All @@ -42,6 +43,27 @@ public void TryFind()
Assert.IsTrue(h is StandardTypesConverter);
}

[Test]
public void TryGetFieldHarvester()
{
var config = new Configuration();
config.AddHandler(type => type == typeof(decimal), type => new List<SanitizedFieldInfo>());

Assert.IsTrue(config.TryGetFieldHarvester(typeof(decimal), out var h));
Assert.IsTrue(h is AnonymousHarvester);
}

[Test]
public void TryGetEnumerableOrderer()
{
var config = new Configuration();
var orderer = new ComparableOrderer();
config.Add(orderer);

Assert.IsTrue(config.TryGetEnumerableOrderer(typeof(IEnumerable<decimal>), out var h));
Assert.AreEqual(h, orderer);
}

[Test]
public void SettingNullValues()
{
Expand All @@ -53,11 +75,16 @@ public void SettingNullValues()

Assert.Throws<ArgumentNullException>(() => sut.Add((IFieldHarvester)null));
Assert.Throws<ArgumentNullException>(() => sut.Add((IValueConverter)null));
Assert.Throws<ArgumentNullException>(() => sut.Add((IEnumerableOrderer)null));

Assert.Throws<ArgumentNullException>(() => sut.AddHandler(null, t => new List<SanitizedFieldInfo>()));
Assert.Throws<ArgumentNullException>(() => sut.AddHandler(t => true, null));
Assert.Throws<ArgumentNullException>(() => sut.AddHandler(null, null));

Assert.Throws<ArgumentNullException>(() => sut.AddOrderer(null, x => x));
Assert.Throws<ArgumentNullException>(() => sut.AddOrderer(x => true, null));
Assert.Throws<ArgumentNullException>(() => sut.AddOrderer(null, null));

Assert.Throws<ArgumentNullException>(() => sut.Test.SetAreEqualsMethod((TestFrameworkAreEqualsMethod) null));
Assert.Throws<ArgumentNullException>(() => sut.Test.SetAutomaticTestRewrite(null));
}
Expand Down
67 changes: 67 additions & 0 deletions StatePrinter.Tests/Orderers/AnonymousOrdererTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//// Copyright 2014 Kasper B. Graversen
////
//// Licensed to the Apache Software Foundation (ASF) under one
//// or more contributor license agreements. See the NOTICE file
//// distributed with this work for additional information
//// regarding copyright ownership. The ASF licenses this file
//// to you under the Apache License, Version 2.0 (the
//// "License"); you may not use this file except in compliance
//// with the License. You may obtain a copy of the License at
////
//// http://www.apache.org/licenses/LICENSE-2.0
////
//// Unless required by applicable law or agreed to in writing,
//// software distributed under the License is distributed on an
//// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//// KIND, either express or implied. See the License for the
//// specific language governing permissions and limitations
//// under the License.

using NUnit.Framework;
using StatePrinting.Configurations;
using System.Collections.Generic;
using System.Linq;

namespace StatePrinting.Tests.Orderers
{
[TestFixture]
class AnonymousOrdererTest
{
[Test]
public void AnonymousOrdererUsesOrderFuncForHandledType()
{
var cfg = ConfigurationHelper.GetStandardConfiguration(" ");
cfg.AddOrderer(type => type == typeof(List<int>), enumerable => enumerable.Cast<int>().Reverse());
var statePrinter = new Stateprinter(cfg);
var list = new List<int>() { 0, 1, 2 };

var actual = statePrinter.PrintObject(list);

var expected = @"new List<Int32>()
{
[0] = 2
[1] = 1
[2] = 0
}";
Assert.AreEqual(expected, actual);
}

[Test]
public void AnonymousOrdererDoesNotUseOrderFuncForUnhandledType()
{
var cfg = ConfigurationHelper.GetStandardConfiguration(" ");
cfg.AddOrderer(type => false, enumerable => null);
var statePrinter = new Stateprinter(cfg);
var list = new List<int> { 1, 0 };

var actual = statePrinter.PrintObject(list);

var expected = @"new List<Int32>()
{
[0] = 1
[1] = 0
}";
Assert.AreEqual(expected, actual);
}
}
}
145 changes: 145 additions & 0 deletions StatePrinter.Tests/Orderers/ComparableOrdererTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright 2014 Kasper B. Graversen
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

using NUnit.Framework;
using StatePrinting.Configurations;
using StatePrinting.Orderers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace StatePrinting.Tests.Orderers
{
[TestFixture]
class ComparableOrdererTest
{
[TestFixture]
class UnitTests
{
[TestCase(typeof(IEnumerable<IComparable>), ExpectedResult = true)]
[TestCase(typeof(int), ExpectedResult = false)]
[TestCase(typeof(List<int>), ExpectedResult = true)]
[TestCase(typeof(IEnumerable<int>), ExpectedResult = true)]
[TestCase(typeof(Dictionary<int, int>), ExpectedResult = false)]
[TestCase(typeof(Dictionary<int, ComparableOrdererTest>), ExpectedResult = false)]
[TestCase(typeof(Dictionary<ComparableOrdererTest, ComparableOrdererTest>), ExpectedResult = false)]
[TestCase(typeof(Dictionary<int, int>.KeyCollection), ExpectedResult = true)]
[TestCase(typeof(NonGenericTypeImplementingIEnumerable), ExpectedResult = true)]
public bool CanHandleTypesImplementingIEnumerableOfIComparables(Type type)
{
var comparableOrderer = new ComparableOrderer();

return comparableOrderer.CanHandleType(type);
}

[Test]
public void CanOrderArrayOfValueTypes()
{
var comparableOrderer = new ComparableOrderer();
var ints = new object[] { 5, 1, 3, 4, 2 };

var ordered = comparableOrderer.Order(ints).Cast<int>().ToArray();

var expected = new[] { 1, 2, 3, 4, 5 };
CollectionAssert.AreEqual(expected, ordered);
}

[Test]
public void CanOrderArrayOfReferenceTypes()
{
var comparableOrderer = new ComparableOrderer();
var strings = new[] { "c", "a", "b" };

var ordered = comparableOrderer.Order(strings).Cast<string>().ToArray();

var expected = new[] { "a", "b", "c" };
CollectionAssert.AreEqual(expected, ordered);
}

[Test]
public void CannotOrderArrayOfDifferentTypes()
{
var comparableOrderer = new ComparableOrderer();
var array = new object[] { 1, "a" };

Assert.Throws<ArgumentException>(() => comparableOrderer.Order(array).Cast<object>().ToArray());
}

class NonGenericTypeImplementingIEnumerable : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator() => throw new NotImplementedException();

IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
}
}

[TestFixture]
public class IntegrationTests
{
[Test]
public void IsUsedForEnumerables()
{
Stateprinter statePrinter = GetStatePrinterWithComparableOrderer();
var list = new List<int>() { 3, 1, 2, 0 };

var actual = statePrinter.PrintObject(list);

var expected = @"new List<Int32>()
{
[0] = 0
[1] = 1
[2] = 2
[3] = 3
}";
Assert.AreEqual(expected, actual);
}

[Test]
public void IsUsedForDictionaries()
{
var statePrinter = GetStatePrinterWithComparableOrderer();
var dictionary = new Dictionary<int, int>()
{
{ 3, 1 },
{ 1, 2 },
{ 2, 3 },
};

var actual = statePrinter.PrintObject(dictionary);

var expected = @"new Dictionary<Int32, Int32>()
{
[1] = 2
[2] = 3
[3] = 1
}";
Assert.AreEqual(expected, actual);
}

private static Stateprinter GetStatePrinterWithComparableOrderer()
{
var cfg = ConfigurationHelper.GetStandardConfiguration(" ");
cfg.Add(new ComparableOrderer());
var statePrinter = new Stateprinter(cfg);
return statePrinter;
}
}
}
}
2 changes: 2 additions & 0 deletions StatePrinter.Tests/StatePrinter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
<Compile Include="FieldHarvesters\ToStringAwareHarvesterTest.cs" />
<Compile Include="Introspection\ReferenceTest.cs" />
<Compile Include="Mocks\Mocks.cs" />
<Compile Include="Orderers\AnonymousOrdererTest.cs" />
<Compile Include="Orderers\ComparableOrdererTest.cs" />
<Compile Include="OutputFormatters\RollingGuidValueConverterTest.cs" />
<Compile Include="PerformanceTests\ManySmallCollections.cs" />
<Compile Include="PerformanceTests\PerformanceTestsBase.cs" />
Expand Down
Loading