summaryrefslogtreecommitdiff
path: root/hclsyntax/token_test.go
blob: 2ce05479107493c0cc49b0b2436191ec08fc3b48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package hclsyntax

import (
	"testing"

	"github.com/hashicorp/hcl/v2"
)

func TestCheckInvalidTokensTest(t *testing.T) {
	tests := []struct {
		Input       string
		WantSummary string
		WantDetail  string
	}{
		{
			`block “invalid” {}`,
			`Invalid character`,
			`"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".`,
		},
		{
			`block 'invalid' {}`,
			`Invalid character`,
			`Single quotes are not valid. Use double quotes (") to enclose strings.`,
		},
		{
			"block `invalid` {}",
			`Invalid character`,
			"The \"`\" character is not valid. To create a multi-line string, use the \"heredoc\" syntax, like \"<<EOT\".",
		},
		{
			`foo = a & b`,
			`Unsupported operator`,
			`Bitwise operators are not supported. Did you mean boolean AND ("&&")?`,
		},
		{
			`foo = a | b`,
			`Unsupported operator`,
			`Bitwise operators are not supported. Did you mean boolean OR ("||")?`,
		},
		{
			`foo = ~a`,
			`Unsupported operator`,
			`Bitwise operators are not supported. Did you mean boolean NOT ("!")?`,
		},
	}

	for _, test := range tests {
		t.Run(test.Input, func(t *testing.T) {
			_, diags := LexConfig([]byte(test.Input), "", hcl.Pos{Line: 1, Column: 1})
			for _, diag := range diags {
				if diag.Severity == hcl.DiagError && diag.Summary == test.WantSummary && diag.Detail == test.WantDetail {
					return // success!
				}
			}
			// If we fall out here then we didn't find the diagnostic we were
			// looking for.
			t.Errorf("wrong errors\ngot:  %s\nwant: %s; %s", diags.Error(), test.WantSummary, test.WantDetail)
		})
	}
}