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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
package main
import "testing"
func TestValidIdentifier(t *testing.T) {
tests := []struct {
key string
want bool
}{
// Valid Identifiers
{"dotted", true},
{"dotted123", true},
{"_under_scores", true},
{"ಠ_ಠ", true},
// Invalid chars
{"is-quoted", false},
{"Definitely quoted!", false},
// Reserved words
{"true", false},
{"else", false},
{"null", false},
// Empty string
{"", false},
}
for _, test := range tests {
have := validIdentifier(test.key)
if have != test.want {
t.Errorf("Want %t for validIdentifier(%s); have %t", test.want, test.key, have)
}
}
}
func TestValidFirstRune(t *testing.T) {
tests := []struct {
in rune
want bool
}{
{'r', true},
{'ಠ', true},
{'4', false},
{'-', false},
}
for _, test := range tests {
have := validFirstRune(test.in)
if have != test.want {
t.Errorf("Want %t for validFirstRune(%#U); have %t", test.want, test.in, have)
}
}
}
func TestValidSecondaryRune(t *testing.T) {
tests := []struct {
in rune
want bool
}{
{'r', true},
{'ಠ', true},
{'4', true},
{'-', false},
}
for _, test := range tests {
have := validSecondaryRune(test.in)
if have != test.want {
t.Errorf("Want %t for validSecondaryRune(%#U); have %t", test.want, test.in, have)
}
}
}
func BenchmarkValidIdentifier(b *testing.B) {
for i := 0; i < b.N; i++ {
validIdentifier("must-be-quoted")
}
}
func BenchmarkValidIdentifierUnquoted(b *testing.B) {
for i := 0; i < b.N; i++ {
validIdentifier("canbeunquoted")
}
}
func BenchmarkValidIdentifierReserved(b *testing.B) {
for i := 0; i < b.N; i++ {
validIdentifier("function")
}
}
|