Skip to content

Commit fbe9e19

Browse files
authored
Merge pull request #27 from keep-network/dash-or-not-to-dash
Fix ethereum command function names
2 parents 86a57ad + d965040 commit fbe9e19

File tree

2 files changed

+120
-4
lines changed

2 files changed

+120
-4
lines changed

tools/generators/ethereum/contract_parsing.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func buildContractInfo(
9595
payableMethods := make(map[string]struct{})
9696
for _, methodPayableInfo := range payableInfo {
9797
if methodPayableInfo.Payable {
98-
normalizedName := toCamelCase(methodPayableInfo.Name)
98+
normalizedName := camelCase(methodPayableInfo.Name)
9999
_, ok := payableMethods[normalizedName]
100100
for idx := 0; ok; idx++ {
101101
normalizedName = fmt.Sprintf("%s%d", normalizedName, idx)
@@ -138,7 +138,7 @@ func buildMethodInfo(
138138
constMethods = make([]methodInfo, 0, len(methodsByName))
139139

140140
for name, method := range methodsByName {
141-
normalizedName := toCamelCase(name)
141+
normalizedName := camelCase(name)
142142
dashedName := strings.ToLower(string(shortVarRegexp.ReplaceAll(
143143
[]byte(normalizedName),
144144
[]byte("-$0"),
@@ -275,19 +275,25 @@ func buildEventInfo(eventsByName map[string]abi.Event) []eventInfo {
275275
}
276276

277277
func uppercaseFirst(str string) string {
278+
if len(str) == 0 {
279+
return str
280+
}
278281
return strings.ToUpper(str[0:1]) + str[1:]
279282
}
280283

281284
func lowercaseFirst(str string) string {
285+
if len(str) == 0 {
286+
return str
287+
}
282288
return strings.ToLower(str[0:1]) + str[1:]
283289
}
284290

285-
func toCamelCase(input string) string {
291+
func camelCase(input string) string {
286292
parts := strings.Split(input, "_")
287293
for i, s := range parts {
288294
if len(s) > 0 {
289295
parts[i] = strings.ToUpper(s[:1]) + s[1:]
290296
}
291297
}
292-
return strings.Join(parts, "")
298+
return lowercaseFirst(strings.Join(parts, ""))
293299
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestLowercaseFirst(t *testing.T) {
6+
var tests = map[string]struct {
7+
input string
8+
expected string
9+
}{
10+
"empty string": {
11+
input: "",
12+
expected: "",
13+
},
14+
"first lower case": {
15+
input: "helloWorld",
16+
expected: "helloWorld",
17+
},
18+
"first upper case": {
19+
input: "HelloWorld",
20+
expected: "helloWorld",
21+
},
22+
}
23+
24+
for testName, test := range tests {
25+
t.Run(testName, func(t *testing.T) {
26+
actual := lowercaseFirst(test.input)
27+
if actual != test.expected {
28+
t.Errorf(
29+
"unexpected output\nexpected: [%v]\nactual: [%v]",
30+
test.expected,
31+
actual,
32+
)
33+
}
34+
})
35+
}
36+
}
37+
38+
func TestUppercaseFirst(t *testing.T) {
39+
var tests = map[string]struct {
40+
input string
41+
expected string
42+
}{
43+
"empty string": {
44+
input: "",
45+
expected: "",
46+
},
47+
"first upper case": {
48+
input: "HelloWorld",
49+
expected: "HelloWorld",
50+
},
51+
"first lower case": {
52+
input: "helloWorld",
53+
expected: "HelloWorld",
54+
},
55+
}
56+
57+
for testName, test := range tests {
58+
t.Run(testName, func(t *testing.T) {
59+
actual := uppercaseFirst(test.input)
60+
if actual != test.expected {
61+
t.Errorf(
62+
"unexpected output\nexpected: [%v]\nactual: [%v]",
63+
test.expected,
64+
actual,
65+
)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestCamelCase(t *testing.T) {
72+
var tests = map[string]struct {
73+
input string
74+
expected string
75+
}{
76+
"empty string": {
77+
input: "",
78+
expected: "",
79+
},
80+
"no underscores": {
81+
input: "HelloWorld",
82+
expected: "helloWorld",
83+
},
84+
"with underscores": {
85+
input: "hello_world",
86+
expected: "helloWorld",
87+
},
88+
"one underscore first": {
89+
input: "_beacon_callback",
90+
expected: "beaconCallback",
91+
},
92+
"multiple underscores first": {
93+
input: "__beacon_callback",
94+
expected: "beaconCallback",
95+
},
96+
}
97+
98+
for testName, test := range tests {
99+
t.Run(testName, func(t *testing.T) {
100+
actual := camelCase(test.input)
101+
if actual != test.expected {
102+
t.Errorf(
103+
"unexpected output\nexpected: [%v]\nactual: [%v]",
104+
test.expected,
105+
actual,
106+
)
107+
}
108+
})
109+
}
110+
}

0 commit comments

Comments
 (0)