Files
Ben Kraft 80687e7336 Support package-names with dashes in them (#232)
Support package-names with dashes in them

We were smart about aliasing if you have name-collisions, but not if
your package name is something that's not a valid identifier, like
`"path/to/my-package"`, which Go for better or worse allows. Now we
remove all the invalid characters (in practice mainly dashes, dots, and
leading digits).

Fixes #231.

Test plan: make check
2022-11-09 09:43:04 -08:00

43 lines
1.1 KiB
Go

package generate
import (
"go/token"
"testing"
)
func TestMakeIdentifier(t *testing.T) {
tests := []struct {
testName string
input string
expected string
}{
{"GoodIdentifier", "myIdent", "myIdent"},
{"GoodIdentifierNumbers", "myIdent1234", "myIdent1234"},
{"NumberPrefix", "1234myIdent", "myIdent"},
{"OnlyNumbers", "1234", "alias"},
{"Dashes", "my-ident", "myident"},
// Note: most Go implementations won't actually allow
// this package-path, but the spec is pretty vague
// so make sure to handle it.
{"JunkAnd", "my!!\\\\\nident", "myident"},
{"JunkOnly", "!!\\\\\n", "alias"},
{"Accents", "née", "née"},
{"Kanji", "日本", "日本"},
{"EmojiAnd", "ident👍", "ident"},
{"EmojiOnly", "👍", "alias"},
}
for _, test := range tests {
test := test
t.Run(test.testName, func(t *testing.T) {
actual := makeIdentifier(test.input)
if actual != test.expected {
t.Errorf("mismatch:\ngot: %s\nwant: %s", actual, test.expected)
}
if !token.IsIdentifier(actual) {
t.Errorf("not a valid identifier: %s", actual)
}
})
}
}