github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/public_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 TestValidIdentifier(t *testing.T) { 13 tests := []struct { 14 Input string 15 Want bool 16 }{ 17 {"", false}, 18 {"hello", true}, 19 {"hello.world", false}, 20 {"hello ", false}, 21 {" hello", false}, 22 {"hello\n", false}, 23 {"hello world", false}, 24 {"aws_instance", true}, 25 {"aws.instance", false}, 26 {"foo-bar", true}, 27 {"foo--bar", true}, 28 {"foo_", true}, 29 {"foo-", true}, 30 {"_foobar", true}, 31 {"-foobar", false}, 32 {"blah1", true}, 33 {"blah1blah", true}, 34 {"1blah1blah", false}, 35 {"héllo", true}, // combining acute accent 36 {"Χαίρετε", true}, 37 {"звать", true}, 38 {"今日は", true}, 39 {"\x80", false}, // UTF-8 continuation without an introducer 40 {"a\x80", false}, // UTF-8 continuation after a non-introducer 41 } 42 43 for _, test := range tests { 44 t.Run(test.Input, func(t *testing.T) { 45 got := ValidIdentifier(test.Input) 46 if got != test.Want { 47 t.Errorf("wrong result %#v; want %#v", got, test.Want) 48 } 49 }) 50 } 51 } 52 53 var T Tokens 54 55 func BenchmarkLexConfig(b *testing.B) { 56 src := []byte("module \"once\" {\n source = \"../modules/foo\"\n}\n\nmodule \"twice\" {\n source = \"../modules/foo\"\n}\n") 57 filename := "testdata/dave/main.tf" 58 start := hcl.Pos{Line: 1, Column: 1, Byte: 0} 59 60 var tokens Tokens 61 62 for i := 0; i < b.N; i++ { 63 tokens, _ = LexConfig(src, filename, start) 64 } 65 66 T = tokens 67 }