Skip to content

Commit 977356b

Browse files
committed
Move test to correct file
1 parent ace9a7e commit 977356b

File tree

2 files changed

+50
-55
lines changed

2 files changed

+50
-55
lines changed

test/Esprima.Tests/AstTests.cs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -212,59 +212,4 @@ public void ReusedNodeInstancesRewrittenOnlyOnce(string source, Func<IEnumerable
212212

213213
Assert.Single(reusedNodeSelector(nodes));
214214
}
215-
216-
[Fact]
217-
public void GenericAstVisitor()
218-
{
219-
var parser = new JavaScriptParser();
220-
var module = parser.ParseModule("10 + 5");
221-
222-
var visitor = new CalculatorVisitor();
223-
var result = visitor.Visit(module);
224-
225-
Assert.Equal(15, result);
226-
}
227-
228-
public class CalculatorVisitor : AstVisitor<double>
229-
{
230-
protected internal override double VisitLiteral(Literal literal)
231-
{
232-
return literal.Value switch
233-
{
234-
double d => d,
235-
int i => i,
236-
_ => throw new NotSupportedException("Only numeric literals are supported.")
237-
};
238-
}
239-
240-
protected internal override double VisitProgram(Program program)
241-
{
242-
if (program.Body.Count != 1)
243-
{
244-
throw new NotSupportedException("Only single expression programs are supported");
245-
}
246-
247-
return Visit(program.Body[0]);
248-
}
249-
250-
protected internal override double VisitExpressionStatement(ExpressionStatement expressionStatement)
251-
{
252-
return Visit(expressionStatement.Expression);
253-
}
254-
255-
protected internal override double VisitBinaryExpression(BinaryExpression binaryExpression)
256-
{
257-
var left = Visit(binaryExpression.Left);
258-
var right = Visit(binaryExpression.Right);
259-
260-
return binaryExpression.Operator switch
261-
{
262-
BinaryOperator.Plus => left + right,
263-
BinaryOperator.Minus => left - right,
264-
BinaryOperator.Times => left * right,
265-
BinaryOperator.Divide => left / right,
266-
_ => throw new NotSupportedException($"Operator {binaryExpression.Operator} is not supported.")
267-
};
268-
}
269-
}
270215
}

test/Esprima.Tests/AstVisitorTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,54 @@ public static IEnumerable<object[]> SourceFiles(string relativePath)
8989
{
9090
return Fixtures.SourceFiles(relativePath);
9191
}
92+
93+
[Fact]
94+
public void CanVisitGenericAstVisitor()
95+
{
96+
var parser = new JavaScriptParser();
97+
var module = parser.ParseModule("10 + 5");
98+
99+
var visitor = new CalculatorVisitor();
100+
var result = visitor.Visit(module);
101+
102+
Assert.Equal(15, result);
103+
}
104+
105+
private class CalculatorVisitor : AstVisitor<double>
106+
{
107+
protected internal override double VisitLiteral(Literal literal)
108+
{
109+
return literal.NumericValue ?? throw new NotSupportedException("Only numeric literals are supported.");
110+
}
111+
112+
protected internal override double VisitProgram(Program program)
113+
{
114+
if (program.Body.Count != 1)
115+
{
116+
throw new NotSupportedException("Only single expression programs are supported");
117+
}
118+
119+
return Visit(program.Body[0]);
120+
}
121+
122+
protected internal override double VisitExpressionStatement(ExpressionStatement expressionStatement)
123+
{
124+
return Visit(expressionStatement.Expression);
125+
}
126+
127+
protected internal override double VisitBinaryExpression(BinaryExpression binaryExpression)
128+
{
129+
var left = Visit(binaryExpression.Left);
130+
var right = Visit(binaryExpression.Right);
131+
132+
return binaryExpression.Operator switch
133+
{
134+
BinaryOperator.Plus => left + right,
135+
BinaryOperator.Minus => left - right,
136+
BinaryOperator.Times => left * right,
137+
BinaryOperator.Divide => left / right,
138+
_ => throw new NotSupportedException($"Operator {binaryExpression.Operator} is not supported.")
139+
};
140+
}
141+
}
92142
}

0 commit comments

Comments
 (0)