Skip to content

Commit

Permalink
Add custom assertions and remove unused usings
Browse files Browse the repository at this point in the history
  • Loading branch information
weelink committed Jul 17, 2019
1 parent b4395e4 commit 5431a3e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
@@ -1,4 +1,6 @@
using FluentAssertions;
using System.Linq;

using FluentAssertions;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

Expand All @@ -23,10 +25,21 @@ public AndConstraint<AstNodeAssertions> BeEmpty(string because = "", params obje
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => Subject.ChildNodes)
.ForCondition(astNodes => astNodes.Count == 0)
.FailWith("Expected {context:astNode} to be empty, but found {0}", astNodes => astNodes);
.ForCondition(x => x.Count == 0)
.FailWith("Expected {context:astNode} to be empty, but found {0}", x => x);

return new AndConstraint<AstNodeAssertions>(this);
}

public AndWhichConstraint<AstNodeAssertions, T> Contain<T>(string because = "", params object[] becauseArgs) where T : AstNode
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => Subject.ChildNodes.OfType<T>())
.ForCondition(x => x.Any())
.FailWith("Expected {context:astNode} to contain elements of type {0}", x => typeof(T));

return new AndWhichConstraint<AstNodeAssertions, T>(this, Subject.ChildNodes.OfType<T>());
}
}
}
@@ -0,0 +1,32 @@
using FluentAssertions;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

using FluentSQL.Compilation.Parser;

namespace FluentSQL.Tests.Compilation.Parser.Assertions
{
public class FromNodeAssertions : ReferenceTypeAssertions<FromNode, FromNodeAssertions>
{
public FromNodeAssertions(FromNode instance)
{
Subject = instance;
}

protected override string Identifier
{
get { return "FromNode"; }
}

public AndConstraint<FromNodeAssertions> HaveAlias(string alias, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => Subject.Alias)
.ForCondition(x => x == alias)
.FailWith("Expected {context:FromNode} to have alias {0}, but it had {1}", x => alias, x => x);

return new AndConstraint<FromNodeAssertions>(this);
}
}
}
@@ -0,0 +1,13 @@
using FluentSQL.Compilation.Parser;
using FluentSQL.Tests.Compilation.Parser.Assertions;

namespace FluentSQL.Tests.Compilation.Parser.Extensions
{
public static class FromNodeExtensions
{
public static FromNodeAssertions Should(this FromNode instance)
{
return new FromNodeAssertions(instance);
}
}
}
8 changes: 4 additions & 4 deletions src/FluentSQL.Tests/Compilation/Parser/QueryParserTests.cs
Expand Up @@ -153,8 +153,8 @@ public void It_should_return_an_union_node()
[Fact]
public void It_should_contain_both_queries()
{
RootNode.As<UnionNode>().First.ChildNodes.OfType<FromNode>().Single().Alias.Should().Be(FirstAlias);
RootNode.As<UnionNode>().Second.ChildNodes.OfType<FromNode>().Single().Alias.Should().Be(SecondAlias);
RootNode.As<UnionNode>().First.Should().Contain<FromNode>().Which.Should().HaveAlias(FirstAlias);
RootNode.As<UnionNode>().Second.Should().Contain<FromNode>().Which.Should().HaveAlias(SecondAlias);
}

private AstNode RootNode { get; set; }
Expand Down Expand Up @@ -201,8 +201,8 @@ public void It_should_return_an_union_node()
[Fact]
public void It_should_contain_both_queries()
{
RootNode.As<UnionAllNode>().First.ChildNodes.OfType<FromNode>().Single().Alias.Should().Be(FirstAlias);
RootNode.As<UnionAllNode>().Second.ChildNodes.OfType<FromNode>().Single().Alias.Should().Be(SecondAlias);
RootNode.As<UnionAllNode>().First.Should().Contain<FromNode>().Which.Should().HaveAlias(FirstAlias);
RootNode.As<UnionAllNode>().Second.Should().Contain<FromNode>().Which.Should().HaveAlias(SecondAlias);
}

private AstNode RootNode { get; set; }
Expand Down
@@ -1,8 +1,6 @@
using System;
using System.Linq.Expressions;

using FluentSQL.Modelling;

namespace FluentSQL.Querying.Statements.Extensions
{
/// <summary>
Expand Down

0 comments on commit 5431a3e

Please sign in to comment.