blob: e2534292dc4ebb84209f8511a56dd9e88b1f09ec [file] [log] [blame]
Damien Neilc7d07d92018-08-22 13:46:02 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package protogen
6
7import "testing"
8
9func TestCamelCase(t *testing.T) {
10 tests := []struct {
Damien Neild9016772018-08-23 14:39:30 -070011 in, want string
Damien Neilc7d07d92018-08-22 13:46:02 -070012 }{
13 {"one", "One"},
14 {"one_two", "OneTwo"},
15 {"_my_field_name_2", "XMyFieldName_2"},
16 {"Something_Capped", "Something_Capped"},
17 {"my_Name", "My_Name"},
18 {"OneTwo", "OneTwo"},
19 {"_", "X"},
20 {"_a_", "XA_"},
Damien Neil3863ee52018-10-09 13:24:04 -070021 {"one.two", "OneTwo"},
22 {"one.Two", "One_Two"},
23 {"one_two.three_four", "OneTwoThreeFour"},
24 {"one_two.Three_four", "OneTwo_ThreeFour"},
Damien Neilc7d07d92018-08-22 13:46:02 -070025 {"_one._two", "XOne_XTwo"},
26 {"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"},
27 {"double__underscore", "Double_Underscore"},
Joe Tsaib6405bd2018-11-15 14:44:37 -080028 {"camelCase", "CamelCase"},
29 {"go2proto", "Go2Proto"},
30 {"世界", "世界"},
31 {"x世界", "X世界"},
32 {"foo_bar世界", "FooBar世界"},
Damien Neilc7d07d92018-08-22 13:46:02 -070033 }
34 for _, tc := range tests {
35 if got := camelCase(tc.in); got != tc.want {
36 t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
37 }
38 }
39}
Joe Tsaib6405bd2018-11-15 14:44:37 -080040
41func TestCleanGoName(t *testing.T) {
42 tests := []struct {
43 in, want, wantExported string
44 }{
45 {"", "", "X"},
46 {"hello", "hello", "Hello"},
47 {"hello-world!!", "hello_world__", "Hello_world__"},
48 {"hello-\xde\xad\xbe\xef\x00", "hello_____", "Hello_____"},
49 {"hello 世界", "hello_世界", "Hello_世界"},
50 {"世界", "世界", "X世界"},
51 }
52 for _, tc := range tests {
53 if got := cleanGoName(tc.in, false); got != tc.want {
54 t.Errorf("cleanGoName(%q, false) = %q, want %q", tc.in, got, tc.want)
55 }
56 if got := cleanGoName(tc.in, true); got != tc.wantExported {
57 t.Errorf("cleanGoName(%q, true) = %q, want %q", tc.in, got, tc.wantExported)
58 }
59 }
60}