blob: 7bb5e9be5d629485722d7e68af7ba294ed62a641 [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 }{
Joe Tsai2c6f0982018-12-13 18:37:25 -080013 {"", ""},
Damien Neilc7d07d92018-08-22 13:46:02 -070014 {"one", "One"},
15 {"one_two", "OneTwo"},
16 {"_my_field_name_2", "XMyFieldName_2"},
17 {"Something_Capped", "Something_Capped"},
18 {"my_Name", "My_Name"},
19 {"OneTwo", "OneTwo"},
20 {"_", "X"},
21 {"_a_", "XA_"},
Damien Neil3863ee52018-10-09 13:24:04 -070022 {"one.two", "OneTwo"},
23 {"one.Two", "One_Two"},
24 {"one_two.three_four", "OneTwoThreeFour"},
25 {"one_two.Three_four", "OneTwo_ThreeFour"},
Damien Neilc7d07d92018-08-22 13:46:02 -070026 {"_one._two", "XOne_XTwo"},
27 {"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"},
28 {"double__underscore", "Double_Underscore"},
Joe Tsaib6405bd2018-11-15 14:44:37 -080029 {"camelCase", "CamelCase"},
30 {"go2proto", "Go2Proto"},
31 {"世界", "世界"},
32 {"x世界", "X世界"},
33 {"foo_bar世界", "FooBar世界"},
Damien Neilc7d07d92018-08-22 13:46:02 -070034 }
35 for _, tc := range tests {
36 if got := camelCase(tc.in); got != tc.want {
37 t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
38 }
39 }
40}
Joe Tsaib6405bd2018-11-15 14:44:37 -080041
42func TestCleanGoName(t *testing.T) {
43 tests := []struct {
44 in, want, wantExported string
45 }{
Joe Tsai2c6f0982018-12-13 18:37:25 -080046 {"", "_", "X"},
47 {"boo", "boo", "Boo"},
48 {"Boo", "Boo", "Boo"},
49 {"ßoo", "ßoo", "Xßoo"},
50 {"default", "_default", "Default"},
Joe Tsaib6405bd2018-11-15 14:44:37 -080051 {"hello", "hello", "Hello"},
52 {"hello-world!!", "hello_world__", "Hello_world__"},
53 {"hello-\xde\xad\xbe\xef\x00", "hello_____", "Hello_____"},
54 {"hello 世界", "hello_世界", "Hello_世界"},
55 {"世界", "世界", "X世界"},
56 }
57 for _, tc := range tests {
58 if got := cleanGoName(tc.in, false); got != tc.want {
59 t.Errorf("cleanGoName(%q, false) = %q, want %q", tc.in, got, tc.want)
60 }
61 if got := cleanGoName(tc.in, true); got != tc.wantExported {
62 t.Errorf("cleanGoName(%q, true) = %q, want %q", tc.in, got, tc.wantExported)
63 }
64 }
65}