summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mailru/easyjson/gen/generator_test.go
blob: 0c9d278458602b3a2d9eac6a45040de174d4373b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package gen

import (
	"testing"
)

func TestCamelToSnake(t *testing.T) {
	for i, test := range []struct {
		In, Out string
	}{
		{"", ""},
		{"A", "a"},
		{"SimpleExample", "simple_example"},
		{"internalField", "internal_field"},

		{"SomeHTTPStuff", "some_http_stuff"},
		{"WriteJSON", "write_json"},
		{"HTTP2Server", "http2_server"},
		{"Some_Mixed_Case", "some_mixed_case"},
		{"do_nothing", "do_nothing"},

		{"JSONHTTPRPCServer", "jsonhttprpc_server"}, // nothing can be done here without a dictionary
	} {
		got := camelToSnake(test.In)
		if got != test.Out {
			t.Errorf("[%d] camelToSnake(%s) = %s; want %s", i, test.In, got, test.Out)
		}
	}
}

func TestCamelToLowerCamel(t *testing.T) {
	for i, test := range []struct {
		In, Out string
	}{
		{"", ""},
		{"A", "a"},
		{"SimpleExample", "simpleExample"},
		{"internalField", "internalField"},

		{"SomeHTTPStuff", "someHTTPStuff"},
		{"WriteJSON", "writeJSON"},
		{"HTTP2Server", "http2Server"},

		{"JSONHTTPRPCServer", "jsonhttprpcServer"}, // nothing can be done here without a dictionary
	} {
		got := lowerFirst(test.In)
		if got != test.Out {
			t.Errorf("[%d] lowerFirst(%s) = %s; want %s", i, test.In, got, test.Out)
		}
	}
}

func TestJoinFunctionNameParts(t *testing.T) {
	for i, test := range []struct {
		keepFirst bool
		parts     []string
		out       string
	}{
		{false, []string{}, ""},
		{false, []string{"a"}, "A"},
		{false, []string{"simple", "example"}, "SimpleExample"},
		{true, []string{"first", "example"}, "firstExample"},
		{false, []string{"some", "UPPER", "case"}, "SomeUPPERCase"},
		{false, []string{"number", "123"}, "Number123"},
	} {
		got := joinFunctionNameParts(test.keepFirst, test.parts...)
		if got != test.out {
			t.Errorf("[%d] joinFunctionNameParts(%v) = %s; want %s", i, test.parts, got, test.out)
		}
	}
}

func TestFixVendorPath(t *testing.T) {
	for i, test := range []struct {
		In, Out string
	}{
		{"", ""},
		{"time", "time"},
		{"project/vendor/subpackage", "subpackage"},
	} {
		got := fixPkgPathVendoring(test.In)
		if got != test.Out {
			t.Errorf("[%d] fixPkgPathVendoring(%s) = %s; want %s", i, test.In, got, test.Out)
		}
	}

}