1
0
mirror of https://github.com/golang/go synced 2024-11-25 07:17:56 -07:00

exp/ebnflint: test spec during 'go test'

This avoids the need for a custom Makefile.

R=gri
CC=golang-dev
https://golang.org/cl/5575045
This commit is contained in:
Russ Cox 2012-01-23 16:35:25 -05:00
parent 1cfae8bcbf
commit 4417bc3742
2 changed files with 49 additions and 20 deletions

View File

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"go/scanner" "go/scanner"
"go/token" "go/token"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -76,34 +77,46 @@ func main() {
flag.Parse() flag.Parse()
var ( var (
filename string name string
src []byte r io.Reader
err error
) )
switch flag.NArg() { switch flag.NArg() {
case 0: case 0:
filename = "<stdin>" name, r = "<stdin>", os.Stdin
src, err = ioutil.ReadAll(os.Stdin)
case 1: case 1:
filename = flag.Arg(0) name = flag.Arg(0)
src, err = ioutil.ReadFile(filename)
default: default:
usage() usage()
} }
if err != nil {
report(err)
}
if filepath.Ext(filename) == ".html" || bytes.Index(src, open) >= 0 { if err := verify(name, *start, r); err != nil {
src = extractEBNF(src)
}
grammar, err := ebnf.Parse(filename, bytes.NewBuffer(src))
if err != nil {
report(err)
}
if err = ebnf.Verify(grammar, *start); err != nil {
report(err) report(err)
} }
} }
func verify(name, start string, r io.Reader) error {
if r == nil {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
r = f
}
src, err := ioutil.ReadAll(r)
if err != nil {
return err
}
if filepath.Ext(name) == ".html" || bytes.Index(src, open) >= 0 {
src = extractEBNF(src)
}
grammar, err := ebnf.Parse(name, bytes.NewBuffer(src))
if err != nil {
return err
}
return ebnf.Verify(grammar, start)
}

View File

@ -0,0 +1,16 @@
// Copyright 2012 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 main
import (
"runtime"
"testing"
)
func TestSpec(t *testing.T) {
if err := verify(runtime.GOROOT()+"/doc/go_spec.html", "SourceFile", nil); err != nil {
t.Fatal(err)
}
}