Skip to content

Commit bf7dfa6

Browse files
committed
Handle empty string in uppercaseFirst and lowercaseFirst
This change allows to handle Solidity fallback function properly: function() external payable
1 parent 86a57ad commit bf7dfa6

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tools/generators/ethereum/contract_parsing.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,18 @@ 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+
}
281+
278282
return strings.ToUpper(str[0:1]) + str[1:]
279283
}
280284

281285
func lowercaseFirst(str string) string {
286+
if len(str) == 0 {
287+
return str
288+
}
289+
282290
return strings.ToLower(str[0:1]) + str[1:]
283291
}
284292

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

0 commit comments

Comments
 (0)