errors_test.go 803 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package errors
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. func TestNewPrefix(t *testing.T) {
  10. e1 := New("abc")
  11. got := e1.Error()
  12. if !strings.HasPrefix(got, "proto:") {
  13. t.Errorf("missing \"proto:\" prefix in %q", got)
  14. }
  15. if !strings.Contains(got, "abc") {
  16. t.Errorf("missing text \"abc\" in %q", got)
  17. }
  18. e2 := New("%v", e1)
  19. got = e2.Error()
  20. if !strings.HasPrefix(got, "proto:") {
  21. t.Errorf("missing \"proto:\" prefix in %q", got)
  22. }
  23. // Test to make sure prefix is removed from the embedded error.
  24. if strings.Contains(strings.TrimPrefix(got, "proto:"), "proto:") {
  25. t.Errorf("prefix \"proto:\" not elided in embedded error: %q", got)
  26. }
  27. }