Skip to content

Commit

Permalink
Added configurable assert message creation
Browse files Browse the repository at this point in the history
Also moved Configuration setters related to testing to a separate test instance

closes #22
  • Loading branch information
kbilsted committed Apr 2, 2015
1 parent 151d364 commit c99de76
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 107 deletions.
3 changes: 2 additions & 1 deletion StatePrinter.Tests/Configurations/ConfigurationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void SettingNullValues()
Assert.Throws<ArgumentNullException>(() => sut.SetCulture(null));
Assert.Throws<ArgumentNullException>(() => sut.SetNewlineDefinition(null));
Assert.Throws<ArgumentNullException>(() => sut.SetAreEqualsMethod(null));
Assert.Throws<ArgumentNullException>(() => sut.SetAutomaticTestRewrite(null));
Assert.Throws<ArgumentNullException>(() => sut.Test.SetAreEqualsMethod(null));
Assert.Throws<ArgumentNullException>(() => sut.Test.SetAutomaticTestRewrite(null));
}
}
}
24 changes: 15 additions & 9 deletions StatePrinter.Tests/FieldHarvesters/ProjectionHarvesterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,26 @@ class C
public DateTime X;
}

[SetCulture("da-DK")]
[Test]
public void TestFluintInterface_exclude()
public void Obsolete_TestFluintInterface_exclude()
{
var cfg = TestHelper.CreateTestConfiguration();
cfg.Projectionharvester().Exclude<A>(x => x.X, x => x.Y);
var printer = new Stateprinter(cfg);

var state = printer.PrintObject(new A { X = DateTime.Now, Name = "Charly" });
Assert.AreEqual(@"new A() { Name = ""Charly"" }", state);
}

[Test]
public void TestFluintInterface_exclude()
{
var cfg = TestHelper.CreateTestConfiguration();
cfg.Project.Exclude<A>(x => x.X, x => x.Y);
var printer = new Stateprinter(cfg);

var state = printer.PrintObject(new A { X = DateTime.Now, Name = "Charly" });
Assert.AreEqual(@"new A() { Name = ""Charly"" }", state);

state =
printer.PrintObject(new B { X = DateTime.Now, Name = "Charly", Age = 43 });
Expand All @@ -73,12 +83,11 @@ public void TestFluintInterface_exclude()
Assert.AreEqual(@"new C() { X = 08-09-2010 00:00:00 }", state);
}

[SetCulture("da-DK")]
[Test]
public void TestFluintInterface_Include()
{
var cfg = TestHelper.CreateTestConfiguration();
cfg.Projectionharvester().Include<A>(x => x.Name);
cfg.Project.Include<A>(x => x.Name);
var printer = new Stateprinter(cfg);

var state = printer.PrintObject(new A { X = DateTime.Now, Name = "Charly" });
Expand All @@ -92,14 +101,11 @@ public void TestFluintInterface_Include()
Assert.AreEqual(@"new C() { X = 08-09-2010 00:00:00 }", state);
}

[SetCulture("da-DK")]
[Test]
public void UserStory()
{
var cfg = TestHelper.CreateTestConfiguration();
cfg.Projectionharvester()
.AddFilter<A>(
x => x.Where(y => y.SanitizedName != "X" && y.SanitizedName != "Y"));
cfg.Project.AddFilter<A>(x => x.Where(y => y.SanitizedName != "X" && y.SanitizedName != "Y"));

var printer = new Stateprinter(cfg);

Expand Down Expand Up @@ -306,7 +312,7 @@ public void IncludeSubsetOfFields()
new ProjectionHarvester().Include<B>(
x => x.Name,
x => x.Age)));
stateprinter.Configuration.SetAreEqualsMethod(Assert.AreEqual);
stateprinter.Configuration.Test.SetAreEqualsMethod(Assert.AreEqual);

var expected = @"new B()
{
Expand Down
2 changes: 1 addition & 1 deletion StatePrinter.Tests/IntegrationTests/InheritanceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class InheritanceTest
public void Setup()
{
var cfg = ConfigurationHelper.GetStandardConfiguration()
.SetAreEqualsMethod(Assert.AreEqual);
.Test.SetAreEqualsMethod(Assert.AreEqual);
cfg.OutputFormatter = new CurlyBraceStyle(cfg);
printer = new Stateprinter(cfg);
}
Expand Down
8 changes: 4 additions & 4 deletions StatePrinter.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static Asserter Assert()
public static Stateprinter CreateTestPrinter()
{
var cfg = ConfigurationHelper.GetStandardConfiguration()
.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
.SetCulture(CultureInfo.CreateSpecificCulture("da-DK"))
.SetAutomaticTestRewrite((x) => false);
.Test.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
.Test.SetAutomaticTestRewrite(x => false);

return new Stateprinter(cfg);
}
Expand All @@ -47,8 +47,8 @@ public static Configuration CreateTestConfiguration()
.GetStandardConfiguration("")
.SetNewlineDefinition(" ")
.SetCulture(CultureInfo.CreateSpecificCulture("da-DK"))
.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
.SetAutomaticTestRewrite((x) => false);
.Test.SetAreEqualsMethod(NUnit.Framework.Assert.AreEqual)
.Test.SetAutomaticTestRewrite(x => false);

return res;
}
Expand Down
8 changes: 4 additions & 4 deletions StatePrinter.Tests/TestingAssistance/ReWriterMockedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public void Rewriter_calls_to_testframework_autorewriting()
var fakeReadContent = new System.Text.UTF8Encoding(true).GetBytes(TestFileContent);
var mock = new FileRepositoryMock(fakeReadContent);
printer.Configuration.FactoryFileRepository = () => mock;
printer.Configuration.SetAutomaticTestRewrite((x) => true);
printer.Configuration.Test.SetAutomaticTestRewrite(x => true);

var assertMock = new AreEqualsMethodMock();
printer.Configuration.SetAreEqualsMethod(assertMock.AreEqualsMock);
printer.Configuration.Test.SetAreEqualsMethod(assertMock.AreEqualsMock);

string expected = "boo";
printer.Assert.IsSame(expected, "actul");
Expand Down Expand Up @@ -113,10 +113,10 @@ public void Rewriter_calls_to_testframework_fileTooShort()

var fakeReadContent = new System.Text.UTF8Encoding(true).GetBytes(TestFileContent);
printer.Configuration.FactoryFileRepository = () => new FileRepositoryMock(fakeReadContent);
printer.Configuration.SetAutomaticTestRewrite((x) => true);
printer.Configuration.Test.SetAutomaticTestRewrite((x) => true);

var assertMock = new AreEqualsMethodMock();
printer.Configuration.SetAreEqualsMethod(assertMock.AreEqualsMock);
printer.Configuration.Test.SetAreEqualsMethod(assertMock.AreEqualsMock);

string expected = @"expect";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestingAssistanceRewriteTest
public void Autocorrection_works_var()
{
var printer = TestHelper.CreateTestPrinter();
printer.Configuration.SetAutomaticTestRewrite((x) => true);
printer.Configuration.Test.SetAutomaticTestRewrite((x) => true);

var expected = @"""test auto""";
printer.Assert.PrintIsSame(expected, "test auto");
Expand All @@ -47,7 +47,7 @@ public void Autocorrection_works_var()
public void Autocorrection_works_string()
{
var printer = TestHelper.CreateTestPrinter();
printer.Configuration.SetAutomaticTestRewrite((x) => true);
printer.Configuration.Test.SetAutomaticTestRewrite((x) => true);

string expected = @"""test auto""";
printer.Assert.PrintIsSame(expected, "test auto");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void AreEquals_WhenConfigured()
{
var assertMock = new AreEqualsMethodMock();
Asserter assert = TestHelper.Assert();
assert.Configuration.SetAreEqualsMethod(assertMock.AreEqualsMock);
assert.Configuration.Test.SetAreEqualsMethod(assertMock.AreEqualsMock);

// without "
assert.AreEqual("a", "b");
Expand Down
76 changes: 23 additions & 53 deletions StatePrinter/Configurations/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Configuration
public const string DefaultIndention = " ";

public LegacyBehaviour LegacyBehaviour { get; set; }
public TestingBehaviour Test { get; set; }

/// <summary>
/// Specifies the indentation size.
Expand Down Expand Up @@ -77,7 +78,6 @@ public Configuration SetNewlineDefinition(string newlineCharacters)
return this;
}


/// <summary>
/// Instantiate using the <see cref="DefaultIndention"/> and the <see cref="CurlyBraceStyle"/>
/// </summary>
Expand All @@ -87,17 +87,27 @@ public Configuration SetNewlineDefinition(string newlineCharacters)
{
IndentIncrement = indentIncrement;
OutputFormatter = new CurlyBraceStyle(this);
AreEqualsMethod = areEqualsMethod;
NewLineDefinition = Environment.NewLine;
LegacyBehaviour = new LegacyBehaviour();
AutomaticTestRewrite = (x) => false;

Test = new TestingBehaviour(this);
Test.SetAutomaticTestRewrite(x => false);
Test.AssertMessageCreator = DefaultAssertMessage.Create;

if(areEqualsMethod != null)
Test.SetAreEqualsMethod(areEqualsMethod);
}

/// <summary>
/// Defines how the output is formatted.
/// </summary>
public IOutputFormatter OutputFormatter;

public Configuration SetOutputFormatter(IOutputFormatter formatter)
{
OutputFormatter = formatter;
return this;
}
readonly List<IValueConverter> valueConverters = new List<IValueConverter>();

/// <summary>
Expand Down Expand Up @@ -162,74 +172,34 @@ public bool TryGetFieldHarvester(Type source, out IFieldHarvester result)
return result != null;
}

#region unit testing support
ProjectionHarvester projection;

/// <summary>
/// Adds to the configuration a <see cref="ProjectionHarvester"/> and returns it.
/// </summary>
public ProjectionHarvester Projectionharvester()
public ProjectionHarvester Project
{
return projection ?? (projection = new ProjectionHarvester(this));
get { return projection ?? (projection = new ProjectionHarvester(this)); }
}

/// <summary>
/// Configure how to call AreEquals in the unit testing framework of your choice.
/// Only set this field if you are using the <see cref="Stateprinter.Assert"/> functionality.
/// </summary>
public TestFrameworkAreEqualsMethod AreEqualsMethod { get; private set; }

/// <summary>
/// Configure how to call AreEquals in the unit testing framework of your choice.
/// Only set this field if you are using the <see cref="Stateprinter.Assert"/> functionality.
/// Instead use <see cref="Project"/>
/// </summary>
public Configuration SetAreEqualsMethod(TestFrameworkAreEqualsMethod areEqualsMethod)
[Obsolete("Use the Project property instead")]
public ProjectionHarvester Projectionharvester()
{
if (areEqualsMethod == null)
throw new ArgumentNullException("areEqualsMethod");
AreEqualsMethod = areEqualsMethod;

return this;
return Project;
}

/// <summary>
/// The signature for finding out if a test's expected value may be automatically re-written.
/// Instead use <see cref="TestingBehaviour.SetAreEqualsMethod"/>
/// </summary>
/// <param name="pathToUnitTest">Path to the failing test.</param>
/// <returns>True if the test may be rewritten with the new expected value to make the test pass again.</returns>
public delegate bool TestRewriteIndicator(string pathToUnitTest);

/// <summary>
/// Evaluate the function for each failing test. <para></para>
/// Your function can rely on anything such as an environment variable or a file on the file system. <para></para>
/// If you only want to do this evaluation once pr. test suite execution you should wrap your function in a <see cref="Lazy"/>
/// </summary>
public Configuration SetAutomaticTestRewrite(TestRewriteIndicator indicator)
[Obsolete("Use the Configuration.Test.SetAreEqualsMethod instead")]
public Configuration SetAreEqualsMethod(TestFrameworkAreEqualsMethod areEqualsMethod)
{
if (indicator == null)
throw new ArgumentNullException("indicator");
AutomaticTestRewrite = indicator;

return this;
return Test.SetAreEqualsMethod(areEqualsMethod);
}

/// <summary>
/// Evaluate the function for each failing test. <para></para>
/// Your function can rely on anything such as an environment variable or a file on the file system. <para></para>
/// If you only want to do this evaluation once pr. test suite execution you should wrap your function in a <see cref="Lazy"/>
/// </summary>
public TestRewriteIndicator AutomaticTestRewrite { get; private set; }

#endregion

public Func<FileRepository> FactoryFileRepository = () => new FileRepository();
}

public class LegacyBehaviour
{
/// <summary>
/// To mimic the behaviour of v1.0.6 and below, set this to false.
/// </summary>
public bool TrimTrailingNewlines = true;
}
}
29 changes: 29 additions & 0 deletions StatePrinter/Configurations/LegacyBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2014-2015 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.

namespace StatePrinter.Configurations
{
public class LegacyBehaviour
{
/// <summary>
/// To mimic the behaviour of v1.0.6 and below, set this to false.
/// </summary>
public bool TrimTrailingNewlines = true;
}
}

0 comments on commit c99de76

Please sign in to comment.