func TestPathParseErrors(t *testing.T) { assert := assert.New(t) test := func(str, expectError string) { p, err := ParsePath(str) assert.Equal(Path{}, p) if err != nil { assert.Equal(expectError, err.Error()) } else { assert.Fail("Expected " + expectError) } } test("", "Empty path") test(".", "Invalid field: ") test("[", "Path ends in [") test("]", "] is missing opening [") test(".#", "Invalid field: #") test(". ", "Invalid field: ") test(". invalid.field", "Invalid field: invalid.field") test(".foo.", "Invalid field: ") test(".foo.#invalid.field", "Invalid field: #invalid.field") test(".foo!", "Invalid operator: !") test(".foo!bar", "Invalid operator: !") test(".foo#", "Invalid operator: #") test(".foo#bar", "Invalid operator: #") test(".foo[", "Path ends in [") test(".foo[.bar", "[ is missing closing ]") test(".foo]", "] is missing opening [") test(".foo].bar", "] is missing opening [") test(".foo[]", "Empty index value") test(".foo[[]", "Invalid index: [") test(".foo[[]]", "Invalid index: [") test(".foo[42.1.2]", "Invalid index: 42.1.2") test(".foo[1f4]", "Invalid index: 1f4") test(".foo[hello]", "Invalid index: hello") test(".foo['hello']", "Invalid index: 'hello'") test(`.foo[\]`, `Invalid index: \`) test(`.foo[\\]`, `Invalid index: \\`) test(`.foo["hello]`, "[ is missing closing ]") test(`.foo["hello`, "[ is missing closing ]") test(`.foo["`, "[ is missing closing ]") test(`.foo["\`, "[ is missing closing ]") test(`.foo["]`, "[ is missing closing ]") test(".foo[#]", "Invalid hash: ") test(".foo[#invalid]", "Invalid hash: invalid") test(`.foo["hello\nworld"]`, `Only " and \ can be escaped`) test(".foo[42]bar", "Invalid operator: b") test("#foo", "Invalid operator: #") test("!foo", "Invalid operator: !") test("@foo", "Invalid operator: @") test("@key", "Invalid operator: @") test(fmt.Sprintf(".foo[#%s]@soup", hash.FromData([]byte{42}).String()), "Unsupported annotation: @soup") }
// Wraps an error. The enclosing error has a default Error() that contains the error msg along // with a backtrace. The original error can be retrieved by calling err.Cause(). func Wrap(err error) WrappedError { if err == nil { return nil } if we, ok := err.(WrappedError); ok { return we } st := stackTracer{} assert := assert.New(&st) assert.Fail(err.Error()) return wrappedError{st.stackTrace, err} }
func TestPathParseErrors(t *testing.T) { assert := assert.New(t) test := func(str, expectError string) { p, err := NewPath().AddPath(str) assert.Equal(Path{}, p) if err != nil { assert.Equal(expectError, err.Error()) } else { assert.Fail("Expected " + expectError) } } test("", "Empty path") test("foo", "f is not a valid operator") test(".", "Invalid field ") test("[", "Path ends in [") test(".#", "Invalid field #") test(". ", "Invalid field ") test(". invalid.field", "Invalid field invalid.field") test(".foo.", "Invalid field ") test(".foo.#invalid.field", "Invalid field #invalid.field") test(".foo!", "! is not a valid operator") test(".foo!bar", "! is not a valid operator") test(".foo[", "Path ends in [") test(".foo[.bar", "[ is missing closing ]") test(".foo]", "] is missing opening [") test(".foo].bar", "] is missing opening [") test(".foo[]", "Invalid index ") test(".foo[[]", "Invalid index [") test(".foo[[]]", "Invalid index [") test(".foo[42.1.2]", "Invalid index 42.1.2") test(".foo[1f4]", "Invalid index 1f4") test(".foo[hello]", "Invalid index hello") test(".foo['hello']", "Invalid index 'hello'") test(`.foo[\]`, `Invalid index \`) test(`.foo[\\]`, `Invalid index \\`) test(`.foo["hello]`, "[ is missing closing ]") test(`.foo["hello`, "[ is missing closing ]") test(`.foo["`, "[ is missing closing ]") test(`.foo["\`, "[ is missing closing ]") test(`.foo["]`, "[ is missing closing ]") test(".foo[#sha1-invalid]", "Invalid index #sha1-invalid") test(`.foo["hello\nworld"]`, `Only " and \ can be escaped`) }
// Fail reports a failure through func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) { if !assert.Fail(t, failureMessage, msgAndArgs...) { t.FailNow() } }