github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/token_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hclsyntax
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hashicorp/hcl/v2"
    10  )
    11  
    12  func TestCheckInvalidTokensTest(t *testing.T) {
    13  	tests := []struct {
    14  		Input       string
    15  		WantSummary string
    16  		WantDetail  string
    17  	}{
    18  		{
    19  			`block “invalid” {}`,
    20  			`Invalid character`,
    21  			`"Curly quotes" are not valid here. These can sometimes be inadvertently introduced when sharing code via documents or discussion forums. It might help to replace the character with a "straight quote".`,
    22  		},
    23  		{
    24  			`block 'invalid' {}`,
    25  			`Invalid character`,
    26  			`Single quotes are not valid. Use double quotes (") to enclose strings.`,
    27  		},
    28  		{
    29  			"block `invalid` {}",
    30  			`Invalid character`,
    31  			"The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<<EOT\".",
    32  		},
    33  		{
    34  			`foo = a & b`,
    35  			`Unsupported operator`,
    36  			`Bitwise operators are not supported. Did you mean boolean AND ("&&")?`,
    37  		},
    38  		{
    39  			`foo = a | b`,
    40  			`Unsupported operator`,
    41  			`Bitwise operators are not supported. Did you mean boolean OR ("||")?`,
    42  		},
    43  		{
    44  			`foo = ~a`,
    45  			`Unsupported operator`,
    46  			`Bitwise operators are not supported. Did you mean boolean NOT ("!")?`,
    47  		},
    48  	}
    49  
    50  	for _, test := range tests {
    51  		t.Run(test.Input, func(t *testing.T) {
    52  			_, diags := LexConfig([]byte(test.Input), "", hcl.Pos{Line: 1, Column: 1})
    53  			for _, diag := range diags {
    54  				if diag.Severity == hcl.DiagError && diag.Summary == test.WantSummary && diag.Detail == test.WantDetail {
    55  					return // success!
    56  				}
    57  			}
    58  			// If we fall out here then we didn't find the diagnostic we were
    59  			// looking for.
    60  			t.Errorf("wrong errors\ngot:  %s\nwant: %s; %s", diags.Error(), test.WantSummary, test.WantDetail)
    61  		})
    62  	}
    63  }