github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/exp/ebnf/ebnf_test.go (about)

     1  // Copyright 2009 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 ebnf
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  var goodGrammars = []string{
    13  	`Program = .`,
    14  
    15  	`Program = foo .
    16  	 foo = "foo" .`,
    17  
    18  	`Program = "a" | "b" "c" .`,
    19  
    20  	`Program = "a" … "z" .`,
    21  
    22  	`Program = Song .
    23  	 Song = { Note } .
    24  	 Note = Do | (Re | Mi | Fa | So | La) | Ti .
    25  	 Do = "c" .
    26  	 Re = "d" .
    27  	 Mi = "e" .
    28  	 Fa = "f" .
    29  	 So = "g" .
    30  	 La = "a" .
    31  	 Ti = ti .
    32  	 ti = "b" .`,
    33  }
    34  
    35  var badGrammars = []string{
    36  	`Program = | .`,
    37  	`Program = | b .`,
    38  	`Program = a … b .`,
    39  	`Program = "a" … .`,
    40  	`Program = … "b" .`,
    41  	`Program = () .`,
    42  	`Program = [] .`,
    43  	`Program = {} .`,
    44  }
    45  
    46  func checkGood(t *testing.T, src string) {
    47  	grammar, err := Parse("", bytes.NewBuffer([]byte(src)))
    48  	if err != nil {
    49  		t.Errorf("Parse(%s) failed: %v", src, err)
    50  		return
    51  	}
    52  	if err = Verify(grammar, "Program"); err != nil {
    53  		t.Errorf("Verify(%s) failed: %v", src, err)
    54  	}
    55  }
    56  
    57  func checkBad(t *testing.T, src string) {
    58  	_, err := Parse("", bytes.NewBuffer([]byte(src)))
    59  	if err == nil {
    60  		t.Errorf("Parse(%s) should have failed", src)
    61  	}
    62  }
    63  
    64  func TestGrammars(t *testing.T) {
    65  	for _, src := range goodGrammars {
    66  		checkGood(t, src)
    67  	}
    68  	for _, src := range badGrammars {
    69  		checkBad(t, src)
    70  	}
    71  }