bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/conf/rule/parse/parse_test.go (about)

     1  package parse
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func isValid(fname string, t *testing.T) (bool, error) {
    11  	b, err := ioutil.ReadFile(fname)
    12  	if err != nil {
    13  		return false, nil
    14  	}
    15  	_, err = Parse(fname, string(b))
    16  	return err == nil, err
    17  }
    18  
    19  func testDir(dirname string, valid bool, t *testing.T) {
    20  	files, _ := ioutil.ReadDir(dirname)
    21  	for _, f := range files {
    22  		p := filepath.Join(dirname, f.Name())
    23  		if got, err := isValid(p, t); valid != got {
    24  			t.Fatalf("%v: expected %v: %v", p, valid, err)
    25  		}
    26  	}
    27  }
    28  
    29  func TestLex(t *testing.T) {
    30  	testDir("test_valid", true, t)
    31  	testDir("test_invalid", false, t)
    32  }
    33  
    34  func _TestPrint(t *testing.T) {
    35  	fname := "test_valid/4"
    36  	b, err := ioutil.ReadFile(fname)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	c, err := Parse(fname, string(b))
    41  	if err != nil {
    42  		t.Error(err)
    43  	} else {
    44  		fmt.Print(c.Root)
    45  	}
    46  }