1
0
mirror of https://github.com/golang/go synced 2024-11-05 16:46:10 -07:00
go/godoc/static/gen_test.go
Alan Donovan e10d6c9a84 godoc/static: use a stable quote function
Printf(%q) aka strconv.AppendQuote depends on the Unicode tables du
jour, which means gen_test breaks after a Go release in which Unicode
evolves. This change uses a very dumb quotation function that emits
only ASCII and is independent of the Go release.

Perhaps an even better fix would have been to parse the generated file.

Change-Id: I197942f1c36a8b53d6a37be4bb2b1e63a208f7e2
Reviewed-on: https://go-review.googlesource.com/119157
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-06-18 16:36:09 +00:00

50 lines
1.1 KiB
Go

// Copyright 2018 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 static
import (
"bytes"
"io/ioutil"
"strconv"
"testing"
"unicode"
)
func TestStaticIsUpToDate(t *testing.T) {
oldBuf, err := ioutil.ReadFile("static.go")
if err != nil {
t.Errorf("error while reading static.go: %v\n", err)
}
newBuf, err := Generate()
if err != nil {
t.Errorf("error while generating static.go: %v\n", err)
}
if bytes.Compare(oldBuf, newBuf) != 0 {
t.Error(`static.go is stale. Run:
$ go generate golang.org/x/tools/godoc/static
$ git diff
to see the differences.`)
}
}
// TestAppendQuote ensures that AppendQuote produces a valid literal.
func TestAppendQuote(t *testing.T) {
var in, out bytes.Buffer
for r := rune(0); r < unicode.MaxRune; r++ {
in.WriteRune(r)
}
appendQuote(&out, in.Bytes())
in2, err := strconv.Unquote(out.String())
if err != nil {
t.Fatalf("AppendQuote produced invalid string literal: %v", err)
}
if got, want := in2, in.String(); got != want {
t.Fatal("AppendQuote modified string") // no point printing got/want: huge
}
}