Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package protogen |
| 6 | |
| 7 | import "testing" |
| 8 | |
| 9 | func TestCamelCase(t *testing.T) { |
| 10 | tests := []struct { |
Damien Neil | d901677 | 2018-08-23 14:39:30 -0700 | [diff] [blame] | 11 | in, want string |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 12 | }{ |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 13 | {"", ""}, |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 14 | {"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 Neil | 3863ee5 | 2018-10-09 13:24:04 -0700 | [diff] [blame] | 22 | {"one.two", "OneTwo"}, |
| 23 | {"one.Two", "One_Two"}, |
| 24 | {"one_two.three_four", "OneTwoThreeFour"}, |
| 25 | {"one_two.Three_four", "OneTwo_ThreeFour"}, |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 26 | {"_one._two", "XOne_XTwo"}, |
| 27 | {"SCREAMING_SNAKE_CASE", "SCREAMING_SNAKE_CASE"}, |
| 28 | {"double__underscore", "Double_Underscore"}, |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 29 | {"camelCase", "CamelCase"}, |
| 30 | {"go2proto", "Go2Proto"}, |
| 31 | {"世界", "世界"}, |
| 32 | {"x世界", "X世界"}, |
| 33 | {"foo_bar世界", "FooBar世界"}, |
Damien Neil | c7d07d9 | 2018-08-22 13:46:02 -0700 | [diff] [blame] | 34 | } |
| 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 Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 41 | |
| 42 | func TestCleanGoName(t *testing.T) { |
| 43 | tests := []struct { |
| 44 | in, want, wantExported string |
| 45 | }{ |
Joe Tsai | 2c6f098 | 2018-12-13 18:37:25 -0800 | [diff] [blame^] | 46 | {"", "_", "X"}, |
| 47 | {"boo", "boo", "Boo"}, |
| 48 | {"Boo", "Boo", "Boo"}, |
| 49 | {"ßoo", "ßoo", "Xßoo"}, |
| 50 | {"default", "_default", "Default"}, |
Joe Tsai | b6405bd | 2018-11-15 14:44:37 -0800 | [diff] [blame] | 51 | {"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 | } |