package parse

import (
	"fmt"
	"testing"

	"git.makaay.nl/mauricem/go-parsekit/parse"
)

type parseTest struct {
	input         interface{}
	expected      string
	expectedError string
}

func testParse(t *testing.T, p *parser, handler parse.Handler, test parseTest) {
	var err error
	defer func() {
		recovered := recover()
		if recovered != nil {
			err = fmt.Errorf("Panic: %s", recovered.(string))
		}
		if err != nil && test.expectedError == "" {
			t.Errorf("Unexpected error for input %q: %s", test.input, err)
		} else if err != nil && test.expectedError != err.Error() {
			t.Errorf("Unexpected error for input %q:\nexpected: %s\nactual: %s\n", test.input, test.expectedError, err.Error())
		} else {
			result := p.doc.Root.String()
			if test.expected != result {
				t.Errorf("Unexpected result for input %q:\nexpected: %s\nactual: %s\n", test.input, test.expected, result)
			}
		}
	}()
	err = parse.New(handler)(test.input)
}
