public void ParseTreeTests(string json, ExpectedJsonTree parseTree, JsonErrorCode[] expectedErrors) { RootJsonSyntax rootSyntax = JsonParser.Parse(json); AssertParseTree(parseTree, null, 0, rootSyntax.Syntax); // Assert expected errors. Assert.Collection( rootSyntax.Errors, Enumerable.Range(0, expectedErrors.Length) .Select <int, Action <JsonErrorInfo> >(i => errorInfo => Assert.Equal(expectedErrors[i], errorInfo.ErrorCode)) .ToArray()); }
private static int AssertParseTree(ExpectedJsonTree expectedParseTree, JsonSyntax expectedParent, int expectedStart, JsonSyntax actualParseTree) { Assert.IsType(expectedParseTree.ExpectedType, actualParseTree); Assert.Same(expectedParent, actualParseTree.ParentSyntax); Assert.Equal(expectedStart, actualParseTree.Start); int expectedChildCount = expectedParseTree.ChildNodes.Count; Assert.Equal(expectedChildCount, actualParseTree.ChildCount); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChild(-1)); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChild(expectedChildCount)); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChildStartPosition(-1)); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChildStartPosition(expectedChildCount)); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChildStartOrEndPosition(-1)); Assert.Throws <IndexOutOfRangeException>(() => actualParseTree.GetChildStartOrEndPosition(expectedChildCount + 1)); int length = 0; if (expectedChildCount == 0) { if (actualParseTree.Length > 0) { Assert.True(actualParseTree.IsTerminalSymbol(out IJsonSymbol jsonSymbol)); length = jsonSymbol.Length; } else { Assert.False(actualParseTree.IsTerminalSymbol(out _)); } } else { Assert.False(actualParseTree.IsTerminalSymbol(out _)); for (int i = 0; i < expectedChildCount; i++) { Assert.Equal(length, actualParseTree.GetChildStartOrEndPosition(i)); length += AssertParseTree(expectedParseTree.ChildNodes[i], actualParseTree, length, actualParseTree.GetChild(i)); } } Assert.Equal(length, actualParseTree.GetChildStartOrEndPosition(expectedChildCount)); return(length); }