golang.org/x/tools@v0.21.0/godoc/static/gen_test.go (about)

     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  
     5  package static
     6  
     7  import (
     8  	"bytes"
     9  	"os"
    10  	"runtime"
    11  	"strconv"
    12  	"testing"
    13  	"unicode"
    14  )
    15  
    16  func TestStaticIsUpToDate(t *testing.T) {
    17  	if runtime.GOOS == "android" {
    18  		t.Skip("files not available on android")
    19  	}
    20  	oldBuf, err := os.ReadFile("static.go")
    21  	if err != nil {
    22  		t.Errorf("error while reading static.go: %v\n", err)
    23  	}
    24  
    25  	newBuf, err := Generate()
    26  	if err != nil {
    27  		t.Errorf("error while generating static.go: %v\n", err)
    28  	}
    29  
    30  	if !bytes.Equal(oldBuf, newBuf) {
    31  		t.Error(`static.go is stale.  Run:
    32    $ go generate golang.org/x/tools/godoc/static
    33    $ git diff
    34  to see the differences.`)
    35  
    36  	}
    37  }
    38  
    39  // TestAppendQuote ensures that AppendQuote produces a valid literal.
    40  func TestAppendQuote(t *testing.T) {
    41  	var in, out bytes.Buffer
    42  	for r := rune(0); r < unicode.MaxRune; r++ {
    43  		in.WriteRune(r)
    44  	}
    45  	appendQuote(&out, in.Bytes())
    46  	in2, err := strconv.Unquote(out.String())
    47  	if err != nil {
    48  		t.Fatalf("AppendQuote produced invalid string literal: %v", err)
    49  	}
    50  	if got, want := in2, in.String(); got != want {
    51  		t.Fatal("AppendQuote modified string") // no point printing got/want: huge
    52  	}
    53  }