github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optgen/lang/parser_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package lang
    12  
    13  import (
    14  	"fmt"
    15  	"io"
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/cockroachdb/datadriven"
    20  )
    21  
    22  func TestParser(t *testing.T) {
    23  	datadriven.RunTest(t, "testdata/parser", func(t *testing.T, d *datadriven.TestData) string {
    24  		// Only parse command supported.
    25  		if d.Cmd != "parse" {
    26  			t.FailNow()
    27  		}
    28  
    29  		args := []string{"test.opt"}
    30  		for _, cmdArg := range d.CmdArgs {
    31  			// Add additional args.
    32  			args = append(args, cmdArg.String())
    33  		}
    34  
    35  		p := NewParser(args...)
    36  		p.SetFileResolver(func(name string) (io.Reader, error) {
    37  			if name == "test.opt" {
    38  				return strings.NewReader(d.Input), nil
    39  			}
    40  			return nil, fmt.Errorf("unknown file '%s'", name)
    41  		})
    42  
    43  		var actual string
    44  		root := p.Parse()
    45  		if root != nil {
    46  			actual = root.String() + "\n"
    47  		} else {
    48  			// Concatenate errors.
    49  			for _, err := range p.Errors() {
    50  				actual = fmt.Sprintf("%s%s\n", actual, err.Error())
    51  			}
    52  		}
    53  
    54  		return actual
    55  	})
    56  }