From e6b30bf2f78149bbea1ef607321cdeed1010abc5 Mon Sep 17 00:00:00 2001 From: Julie Qiu Date: Wed, 23 Jun 2021 16:36:40 -0700 Subject: internal: support std@dev.fuzz It is now possible to fetch std@dev.fuzz and view /std@dev.fuzz on the frontend. For golang/go#46910 Change-Id: I2fd7259840ba32ff35ca323d07412988e533ffba Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/330413 Trust: Julie Qiu Run-TryBot: Julie Qiu TryBot-Result: kokoro Reviewed-by: Jonathan Amsterdam --- .../stdlib/testdata/dev.fuzz/src/errors/errors.go | 20 ++++++++ .../testdata/dev.fuzz/src/errors/errors_test.go | 53 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 internal/stdlib/testdata/dev.fuzz/src/errors/errors.go create mode 100644 internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go (limited to 'internal/stdlib/testdata/dev.fuzz/src/errors') diff --git a/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go b/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go new file mode 100644 index 00000000..b8a46921 --- /dev/null +++ b/internal/stdlib/testdata/dev.fuzz/src/errors/errors.go @@ -0,0 +1,20 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package errors implements functions to manipulate errors. +package errors + +// New returns an error that formats as the given text. +func New(text string) error { + return &errorString{text} +} + +// errorString is a trivial implementation of error. +type errorString struct { + s string +} + +func (e *errorString) Error() string { + return e.s +} diff --git a/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go b/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go new file mode 100644 index 00000000..cf4df90b --- /dev/null +++ b/internal/stdlib/testdata/dev.fuzz/src/errors/errors_test.go @@ -0,0 +1,53 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package errors_test + +import ( + "errors" + "fmt" + "testing" +) + +func TestNewEqual(t *testing.T) { + // Different allocations should not be equal. + if errors.New("abc") == errors.New("abc") { + t.Errorf(`New("abc") == New("abc")`) + } + if errors.New("abc") == errors.New("xyz") { + t.Errorf(`New("abc") == New("xyz")`) + } + + // Same allocation should be equal to itself (not crash). + err := errors.New("jkl") + if err != err { + t.Errorf(`err != err`) + } +} + +func TestErrorMethod(t *testing.T) { + err := errors.New("abc") + if err.Error() != "abc" { + t.Errorf(`New("abc").Error() = %q, want %q`, err.Error(), "abc") + } +} + +func ExampleNew() { + err := errors.New("emit macho dwarf: elf header corrupted") + if err != nil { + fmt.Print(err) + } + // Output: emit macho dwarf: elf header corrupted +} + +// The fmt package's Errorf function lets us use the package's formatting +// features to create descriptive error messages. +func ExampleNew_errorf() { + const name, id = "bimmler", 17 + err := fmt.Errorf("user %q (id %d) not found", name, id) + if err != nil { + fmt.Print(err) + } + // Output: user "bimmler" (id 17) not found +} -- cgit v1.3