| 1 |
package gen |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
) |
| 6 |
|
| 7 |
func TestCamelToSnake(t *testing.T) { |
| 8 |
for i, test := range []struct { |
| 9 |
In, Out string |
| 10 |
}{ |
| 11 |
{"", ""}, |
| 12 |
{"A", "a"}, |
| 13 |
{"SimpleExample", "simple_example"}, |
| 14 |
{"internalField", "internal_field"}, |
| 15 |
|
| 16 |
{"SomeHTTPStuff", "some_http_stuff"}, |
| 17 |
{"WriteJSON", "write_json"}, |
| 18 |
{"HTTP2Server", "http2_server"}, |
| 19 |
{"Some_Mixed_Case", "some_mixed_case"}, |
| 20 |
{"do_nothing", "do_nothing"}, |
| 21 |
|
| 22 |
{"JSONHTTPRPCServer", "jsonhttprpc_server"}, // nothing can be done here without a dictionary |
| 23 |
} { |
| 24 |
got := camelToSnake(test.In) |
| 25 |
if got != test.Out { |
| 26 |
t.Errorf("[%d] camelToSnake(%s) = %s; want %s", i, test.In, got, test.Out) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestCamelToLowerCamel(t *testing.T) { |
| 32 |
for i, test := range []struct { |
| 33 |
In, Out string |
| 34 |
}{ |
| 35 |
{"", ""}, |
| 36 |
{"A", "a"}, |
| 37 |
{"SimpleExample", "simpleExample"}, |
| 38 |
{"internalField", "internalField"}, |
| 39 |
|
| 40 |
{"SomeHTTPStuff", "someHTTPStuff"}, |
| 41 |
{"WriteJSON", "writeJSON"}, |
| 42 |
{"HTTP2Server", "http2Server"}, |
| 43 |
|
| 44 |
{"JSONHTTPRPCServer", "jsonhttprpcServer"}, // nothing can be done here without a dictionary |
| 45 |
} { |
| 46 |
got := lowerFirst(test.In) |
| 47 |
if got != test.Out { |
| 48 |
t.Errorf("[%d] lowerFirst(%s) = %s; want %s", i, test.In, got, test.Out) |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestJoinFunctionNameParts(t *testing.T) { |
| 54 |
for i, test := range []struct { |
| 55 |
keepFirst bool |
| 56 |
parts []string |
| 57 |
out string |
| 58 |
}{ |
| 59 |
{false, []string{}, ""}, |
| 60 |
{false, []string{"a"}, "A"}, |
| 61 |
{false, []string{"simple", "example"}, "SimpleExample"}, |
| 62 |
{true, []string{"first", "example"}, "firstExample"}, |
| 63 |
{false, []string{"some", "UPPER", "case"}, "SomeUPPERCase"}, |
| 64 |
{false, []string{"number", "123"}, "Number123"}, |
| 65 |
} { |
| 66 |
got := joinFunctionNameParts(test.keepFirst, test.parts...) |
| 67 |
if got != test.out { |
| 68 |
t.Errorf("[%d] joinFunctionNameParts(%v) = %s; want %s", i, test.parts, got, test.out) |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func TestFixVendorPath(t *testing.T) { |
| 74 |
for i, test := range []struct { |
| 75 |
In, Out string |
| 76 |
}{ |
| 77 |
{"", ""}, |
| 78 |
{"time", "time"}, |
| 79 |
{"project/vendor/subpackage", "subpackage"}, |
| 80 |
} { |
| 81 |
got := fixPkgPathVendoring(test.In) |
| 82 |
if got != test.Out { |
| 83 |
t.Errorf("[%d] fixPkgPathVendoring(%s) = %s; want %s", i, test.In, got, test.Out) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
} |