github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/builtins/help_test.go (about)

     1  // Copyright 2017 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 builtins
    12  
    13  import (
    14  	"regexp"
    15  	"strings"
    16  	"testing"
    17  	"unicode"
    18  
    19  	"github.com/cockroachdb/cockroach/pkg/sql/parser"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    21  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    22  )
    23  
    24  func TestHelpFunctions(t *testing.T) {
    25  	defer leaktest.AfterTest(t)()
    26  	// This test checks that all the built-in functions receive contextual help.
    27  	for f := range builtins {
    28  		if unicode.IsUpper(rune(f[0])) {
    29  			continue
    30  		}
    31  		t.Run(f, func(t *testing.T) {
    32  			_, err := parser.Parse("select " + f + "(??")
    33  			if err == nil {
    34  				t.Errorf("parser didn't trigger error")
    35  				return
    36  			}
    37  			if !strings.HasPrefix(err.Error(), "help token in input") {
    38  				t.Fatal(err)
    39  			}
    40  			pgerr := pgerror.Flatten(err)
    41  			if !strings.HasPrefix(pgerr.Hint, "help:\n") {
    42  				t.Errorf("expected 'help: ' prefix, got %q", pgerr.Hint)
    43  				return
    44  			}
    45  			help := pgerr.Hint[6:]
    46  			pattern := "Function:\\s+" + f + "\n"
    47  			if m, err := regexp.MatchString(pattern, help); err != nil || !m {
    48  				if err != nil {
    49  					t.Errorf("pattern match failure: %v", err)
    50  					return
    51  				}
    52  				t.Errorf("help text didn't match %q:\n%s", pattern, help)
    53  			}
    54  		})
    55  	}
    56  }