mirror of
https://github.com/golang/go
synced 2024-11-22 03:04:41 -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:
parent
1cfae8bcbf
commit
4417bc3742
@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"go/scanner"
|
||||
"go/token"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -76,34 +77,46 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
var (
|
||||
filename string
|
||||
src []byte
|
||||
err error
|
||||
name string
|
||||
r io.Reader
|
||||
)
|
||||
switch flag.NArg() {
|
||||
case 0:
|
||||
filename = "<stdin>"
|
||||
src, err = ioutil.ReadAll(os.Stdin)
|
||||
name, r = "<stdin>", os.Stdin
|
||||
case 1:
|
||||
filename = flag.Arg(0)
|
||||
src, err = ioutil.ReadFile(filename)
|
||||
name = flag.Arg(0)
|
||||
default:
|
||||
usage()
|
||||
}
|
||||
if err != nil {
|
||||
report(err)
|
||||
}
|
||||
|
||||
if filepath.Ext(filename) == ".html" || bytes.Index(src, open) >= 0 {
|
||||
src = extractEBNF(src)
|
||||
}
|
||||
|
||||
grammar, err := ebnf.Parse(filename, bytes.NewBuffer(src))
|
||||
if err != nil {
|
||||
report(err)
|
||||
}
|
||||
|
||||
if err = ebnf.Verify(grammar, *start); err != nil {
|
||||
if err := verify(name, *start, r); err != nil {
|
||||
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)
|
||||
}
|
||||
|
16
src/pkg/exp/ebnflint/ebnflint_test.go
Normal file
16
src/pkg/exp/ebnflint/ebnflint_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user