Skip to content

Commit

Permalink
Visitor Refinements (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Jul 5, 2019
1 parent 6e328ee commit 192307d
Show file tree
Hide file tree
Showing 24 changed files with 26,483 additions and 1,301 deletions.
160 changes: 159 additions & 1 deletion src/Core/Language.Tests/Visitors/SyntaxNodeVisitorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks.Dataflow;
using System.Collections.Generic;
using Xunit;
using ChilliCream.Testing;
using Snapshooter.Xunit;

namespace HotChocolate.Language
{
Expand All @@ -14,9 +15,141 @@ public void AutoSkip()
new StringValueNode("bar")));

obj.Accept(new Foo());
}

[Fact]
public void Visit_Kitchen_Sink_Query()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();

// act
document.Accept(new Bar(), visitationMap);

// assert
visitationMap.VisitedNodes.MatchSnapshot();
}

[Fact]
public void Visit_Kitchen_Sink_Schema()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("schema-kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();

// act
document.Accept(new Bar(), visitationMap);

// assert
visitationMap.VisitedNodes.MatchSnapshot();
}

[Fact]
public void Visit_Kitchen_Sink_Schema_Names_With_Delegate()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("schema-kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();
var enterNames = new List<string>();
var leaveNames = new List<string>();

// act
document.Accept<NameNode>(
(node, parent, path, ancestors) =>
{
enterNames.Add(node.Value);
return VisitorAction.Continue;
},
(node, parent, path, ancestors) =>
{
leaveNames.Add(node.Value);
return VisitorAction.Continue;
},
node => VisitorAction.Continue);

// assert
Assert.Equal(enterNames, leaveNames);
new { enterNames, leaveNames }.MatchSnapshot();
}

[Fact]
public void Visit_Kitchen_Sink_Query_Names_With_Delegate()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();
var enterNames = new List<string>();
var leaveNames = new List<string>();

// act
document.Accept<NameNode>(
(node, parent, path, ancestors) =>
{
enterNames.Add(node.Value);
return VisitorAction.Continue;
},
(node, parent, path, ancestors) =>
{
leaveNames.Add(node.Value);
return VisitorAction.Continue;
},
node => VisitorAction.Continue);

// assert
Assert.Equal(enterNames, leaveNames);
new { enterNames, leaveNames }.MatchSnapshot();
}

[Fact]
public void Visit_Kitchen_Sink_Query_Names_With_Delegate_OnlyEnter()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();
var visitedNames = new List<string>();

// act
document.Accept<NameNode>(
(node, parent, path, ancestors) =>
{
visitedNames.Add(node.Value);
return VisitorAction.Continue;
},
null,
node => VisitorAction.Continue);

// assert
visitedNames.MatchSnapshot();
}

[Fact]
public void Visit_Kitchen_Sink_Query_Names_With_Delegate_OnlyLeave()
{
// arrange
DocumentNode document = Utf8GraphQLParser.Parse(
FileResource.Open("kitchen-sink.graphql"));
var visitationMap = new BarVisitationMap();
var visitedNames = new List<string>();

// act
document.Accept<NameNode>(
null,
(node, parent, path, ancestors) =>
{
visitedNames.Add(node.Value);
return VisitorAction.Continue;
},
node => VisitorAction.Continue);

// assert
visitedNames.MatchSnapshot();
}

private class Foo
: SyntaxNodeVisitor
Expand Down Expand Up @@ -48,5 +181,30 @@ public override VisitorAction Leave(
return VisitorAction.Skip;
}
}

private class Bar
: SyntaxNodeVisitor
{
public Bar()
: base(VisitorAction.Continue)
{

}
}

private class BarVisitationMap
: VisitationMap
{
public List<ISyntaxNode> VisitedNodes { get; } =
new List<ISyntaxNode>();

public override void ResolveChildren(
ISyntaxNode node,
IList<SyntaxNodeInfo> children)
{
VisitedNodes.Add(node);
base.ResolveChildren(node, children);
}
}
}
}
Loading

0 comments on commit 192307d

Please sign in to comment.