protobuf-go/protogen/names_test.go
Joe Tsai 2c6f09887d protogen: simplify and fix naming derivation logic
We want to codify the naming logic to be understandable in a specification.
Clean up the camelCase function to be a little more readable.

Cleanup and fix the cleanGoName function when mustExport is true.
It is okay to change this behavior since this is new logic in v2 that has not
yet been exposed to the universe.

In the mustExport code-path, we do not need to check for conflicts with keywords
since Go keywords are never uppercase, so the uppercasing of the first letter
is sufficient to break a conflict. Also, we fix the logic for uppercasing
the first character since not every lowercase character has an uppercase form.

Change-Id: If14422d773bb89ed7038d874135e3dcd12683101
Reviewed-on: https://go-review.googlesource.com/c/154180
Reviewed-by: Damien Neil <dneil@google.com>
2018-12-14 18:30:52 +00:00

66 lines
1.8 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package protogen
import "testing"
func TestCamelCase(t *testing.T) {
tests := []struct {
in, want string
}{
{"", ""},
{"one", "One"},
{"one_two", "OneTwo"},
{"_my_field_name_2", "XMyFieldName_2"},
{"Something_Capped", "Something_Capped"},
{"my_Name", "My_Name"},
{"OneTwo", "OneTwo"},
{"_", "X"},
{"_a_", "XA_"},
{"one.two", "OneTwo"},
{"one.Two", "One_Two"},
{"one_two.three_four", "OneTwoThreeFour"},
{"one_two.Three_four", "OneTwo_ThreeFour"},
{"_one._two", "XOne_XTwo"},
{"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"},
{"double__underscore", "Double_Underscore"},
{"camelCase", "CamelCase"},
{"go2proto", "Go2Proto"},
{"世界", "世界"},
{"x世界", "X世界"},
{"foo_bar世界", "FooBar世界"},
}
for _, tc := range tests {
if got := camelCase(tc.in); got != tc.want {
t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestCleanGoName(t *testing.T) {
tests := []struct {
in, want, wantExported string
}{
{"", "_", "X"},
{"boo", "boo", "Boo"},
{"Boo", "Boo", "Boo"},
{"ßoo", "ßoo", "Xßoo"},
{"default", "_default", "Default"},
{"hello", "hello", "Hello"},
{"hello-world!!", "hello_world__", "Hello_world__"},
{"hello-\xde\xad\xbe\xef\x00", "hello_____", "Hello_____"},
{"hello 世界", "hello_世界", "Hello_世界"},
{"世界", "世界", "X世界"},
}
for _, tc := range tests {
if got := cleanGoName(tc.in, false); got != tc.want {
t.Errorf("cleanGoName(%q, false) = %q, want %q", tc.in, got, tc.want)
}
if got := cleanGoName(tc.in, true); got != tc.wantExported {
t.Errorf("cleanGoName(%q, true) = %q, want %q", tc.in, got, tc.wantExported)
}
}
}