Exemple #1
0
func parseAndEval(input string) (*big.Rat, error) {
	tokens, err := lex.Lex([]byte(input))
	if err != nil {
		return nil, err
	}
	tree, err := parse.Parse(tokens)
	if err != nil {
		return nil, err
	}
	result, err := eval.Eval(tree)
	if err != nil {
		return nil, err
	}
	return result, nil
}
Exemple #2
0
func testParseCasesWithFormat(t *testing.T, parseTestCases []parseTestCaseWithFormat) {
	for i, tc := range parseTestCases {
		tcInfo := fmt.Sprintf("test case: %d\ninput: %s", i, tc.input)
		tokens, err := lex.Lex([]byte(tc.input))
		require.NoError(t, err, tcInfo)
		output, err := Parse(tokens)
		if tc.expectedError != nil {
			require.Error(t, err, tcInfo)
			assert.Exactly(t, tc.expectedError, err, tcInfo)
		} else {
			require.NoError(t, err, tcInfo)
			assert.Exactly(t, tc.expectedOutput, output.Format(0), "%s\n\nExpected:\n%s\n\nGot:\n%s\n\n", tcInfo, tc.expectedOutput, output.Format(0))
		}
	}
}