Skip to content

Instantly share code, notes, and snippets.

@yevhen
Created July 27, 2011 15:26

Revisions

  1. yevhen revised this gist Jul 27, 2011. 1 changed file with 40 additions and 40 deletions.
    80 changes: 40 additions & 40 deletions SpecificationFixture.cs
    Original file line number Diff line number Diff line change
    @@ -1,61 +1,61 @@
    [TestFixture]
    public class SpecificationFixture
    {
    [Test, TestCaseSource("GetSpecificationTestCases")]
    public void Verify(SpecificationToRun spec)
    {
    var runner = new SpecificationRunner();
    RunResult result = runner.RunSpecifciation(spec);
    if (result.Passed)
    return;

    Assert.Fail(Format(result));
    }

    public TestCaseData[] GetSpecificationTestCases()
    {
    IEnumerable<SpecificationToRun> specs = TypeReader.GetSpecificationsIn(GetType());
    return specs.Select(AsTestCaseData).ToArray();
    }

    static TestCaseData AsTestCaseData(SpecificationToRun spec)
    {
    var data = new TestCaseData(spec);
    data.SetName(spec.Specification.GetName());
    return data;
    }

    private static string Format(RunResult result)
    [Test, TestCaseSource("GetSpecificationTestCases")]
    public void Verify(SpecificationToRun spec)
    {
    var runner = new SpecificationRunner();
    RunResult result = runner.RunSpecifciation(spec);
    if (result.Passed)
    return;

    Assert.Fail(Format(result));
    }

    public TestCaseData[] GetSpecificationTestCases()
    {
    IEnumerable<SpecificationToRun> specs = TypeReader.GetSpecificationsIn(GetType());
    return specs.Select(AsTestCaseData).ToArray();
    }

    static TestCaseData AsTestCaseData(SpecificationToRun spec)
    {
    var data = new TestCaseData(spec);
    data.SetName(spec.Specification.GetName());
    return data;
    }

    private static string Format(RunResult result)
    {
    var ret = "\n\nSPECIFICATION: " + (result.SpecificationName ?? result.FoundOnMemberInfo.Name) + " ";

    ret += (result.Passed ? "PASSED" : "FAILED") + " " + result.Message + "\n\n";
    ret += (result.Passed ? "PASSED" : "FAILED") + " " + result.Message + "\n\n";
    if (result.Thrown != null)
    ret += result.Thrown + "\n\n";

    ret += "\tEXPECTATIONS:\n\t-------------\n";
    ret += "\tEXPECTATIONS:\n\t-------------\n";

    foreach (var exp in result.Expectations)
    {
    if (!exp.Passed)
    ret += "\n\t<<<----------\n";
    if (!exp.Passed)
    ret += "\n\t<<<----------\n";

    ret += "\t" + exp.Text + " " + (exp.Passed ? "PASSED" : "FAILED") + "\n";

    if (!exp.Passed)
    {
    ret += PadMultiLineText(exp.Exception.Message) + "\n\n";
    ret += "\t>>>----------\n\n";
    }
    if (!exp.Passed)
    {
    ret += PadMultiLineText(exp.Exception.Message) + "\n\n";
    ret += "\t>>>----------\n\n";
    }
    }

    return ret;
    }

    static string PadMultiLineText(string txt)
    {
    string[] lines = txt.Split(new[] {"\n"}, StringSplitOptions.None);
    return lines.Aggregate("", (current, line) => current + ("\t" + line + "\n"));
    }
    static string PadMultiLineText(string txt)
    {
    string[] lines = txt.Split(new[] {"\n"}, StringSplitOptions.None);
    return lines.Aggregate("", (current, line) => current + ("\t" + line + "\n"));
    }
    }
  2. yevhen created this gist Jul 27, 2011.
    61 changes: 61 additions & 0 deletions SpecificationFixture.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    [TestFixture]
    public class SpecificationFixture
    {
    [Test, TestCaseSource("GetSpecificationTestCases")]
    public void Verify(SpecificationToRun spec)
    {
    var runner = new SpecificationRunner();

    RunResult result = runner.RunSpecifciation(spec);
    if (result.Passed)
    return;

    Assert.Fail(Format(result));
    }

    public TestCaseData[] GetSpecificationTestCases()
    {
    IEnumerable<SpecificationToRun> specs = TypeReader.GetSpecificationsIn(GetType());
    return specs.Select(AsTestCaseData).ToArray();
    }

    static TestCaseData AsTestCaseData(SpecificationToRun spec)
    {
    var data = new TestCaseData(spec);
    data.SetName(spec.Specification.GetName());
    return data;
    }

    private static string Format(RunResult result)
    {
    var ret = "\n\nSPECIFICATION: " + (result.SpecificationName ?? result.FoundOnMemberInfo.Name) + " ";

    ret += (result.Passed ? "PASSED" : "FAILED") + " " + result.Message + "\n\n";
    if (result.Thrown != null)
    ret += result.Thrown + "\n\n";

    ret += "\tEXPECTATIONS:\n\t-------------\n";

    foreach (var exp in result.Expectations)
    {
    if (!exp.Passed)
    ret += "\n\t<<<----------\n";

    ret += "\t" + exp.Text + " " + (exp.Passed ? "PASSED" : "FAILED") + "\n";

    if (!exp.Passed)
    {
    ret += PadMultiLineText(exp.Exception.Message) + "\n\n";
    ret += "\t>>>----------\n\n";
    }
    }

    return ret;
    }

    static string PadMultiLineText(string txt)
    {
    string[] lines = txt.Split(new[] {"\n"}, StringSplitOptions.None);
    return lines.Aggregate("", (current, line) => current + ("\t" + line + "\n"));
    }
    }