blob: 6f03cc91c3ba25a7331a23bdf78da67e26dfd390 [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 {
Joe Tsai40692112019-02-27 20:25:51 -080044 in, want string
Joe Tsaib6405bd2018-11-15 14:44:37 -080045 }{
Joe Tsai40692112019-02-27 20:25:51 -080046 {"", "_"},
47 {"boo", "boo"},
48 {"Boo", "Boo"},
49 {"ßoo", "ßoo"},
50 {"default", "_default"},
51 {"hello", "hello"},
52 {"hello-world!!", "hello_world__"},
53 {"hello-\xde\xad\xbe\xef\x00", "hello_____"},
54 {"hello 世界", "hello_世界"},
55 {"世界", "世界"},
Joe Tsaib6405bd2018-11-15 14:44:37 -080056 }
57 for _, tc := range tests {
Joe Tsai40692112019-02-27 20:25:51 -080058 if got := cleanGoName(tc.in); got != tc.want {
59 t.Errorf("cleanGoName(%q) = %q, want %q", tc.in, got, tc.want)
Joe Tsaib6405bd2018-11-15 14:44:37 -080060 }
61 }
62}