Skip to content

Commit 80f4295

Browse files
authored
Merge pull request #9 from shinshin86/followup-emotion-option
chore: follow-up improvements for emotion option (#7)
2 parents c331136 + 63051bd commit 80f4295

3 files changed

Lines changed: 103 additions & 14 deletions

File tree

cmd/vpeak/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ func runSpeakCommand(args []string) {
5050
fmt.Println(" m2: Japanese Male 2")
5151
fmt.Println(" m3: Japanese Male 3")
5252
fmt.Println(" c: Japanese Female Child")
53-
fmt.Println("\nEmotion options:")
53+
fmt.Println("\nEmotion options (values 0-100, comma-separate multiple):")
5454
fmt.Println(" happy")
5555
fmt.Println(" fun")
5656
fmt.Println(" angry")
5757
fmt.Println(" sad")
58+
fmt.Println("\nEmotion examples:")
59+
fmt.Println(" happy (equivalent to happy=100)")
60+
fmt.Println(" happy=50")
61+
fmt.Println(" happy=40,fun=60")
5862
fmt.Println("\nDictionary commands:")
5963
fmt.Printf(" %s dict -h\n", os.Args[0])
6064
}

vpeak.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"runtime"
11-
"strings"
1211
"strconv"
12+
"strings"
1313
)
1414

1515
var VoicepeakPath string
@@ -58,20 +58,19 @@ type Emotion struct {
5858
}
5959

6060
func (e Emotion) String() string {
61-
values := map[string]int{
62-
"happy": e.Happy,
63-
"sad": e.Sad,
64-
"angry": e.Angry,
65-
"fun": e.Fun,
66-
}
67-
6861
var parts []string
69-
for k, v := range values {
70-
if v != 0 {
71-
parts = append(parts, fmt.Sprintf("%s=%d", k, v))
72-
}
62+
if e.Happy != 0 {
63+
parts = append(parts, fmt.Sprintf("happy=%d", e.Happy))
64+
}
65+
if e.Fun != 0 {
66+
parts = append(parts, fmt.Sprintf("fun=%d", e.Fun))
67+
}
68+
if e.Angry != 0 {
69+
parts = append(parts, fmt.Sprintf("angry=%d", e.Angry))
70+
}
71+
if e.Sad != 0 {
72+
parts = append(parts, fmt.Sprintf("sad=%d", e.Sad))
7373
}
74-
7574
return strings.Join(parts, ",")
7675
}
7776

vpeak_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package vpeak
2+
3+
import "testing"
4+
5+
func TestParseEmotion(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
input string
9+
want Emotion
10+
wantErr bool
11+
}{
12+
{"empty string", "", Emotion{}, false},
13+
{"single emotion without value", "happy", Emotion{Happy: 100}, false},
14+
{"single emotion with value", "happy=50", Emotion{Happy: 50}, false},
15+
{"multiple emotions", "happy=40,fun=60", Emotion{Happy: 40, Fun: 60}, false},
16+
{"all four emotions", "happy=10,fun=20,angry=30,sad=40", Emotion{Happy: 10, Fun: 20, Angry: 30, Sad: 40}, false},
17+
{"boundary zero", "happy=0", Emotion{Happy: 0}, false},
18+
{"boundary max", "happy=100", Emotion{Happy: 100}, false},
19+
{"duplicate keys last wins", "happy=30,happy=70", Emotion{Happy: 70}, false},
20+
{"invalid emotion name", "joyful=50", Emotion{}, true},
21+
{"non-numeric value", "happy=abc", Emotion{}, true},
22+
{"negative value", "happy=-1", Emotion{}, true},
23+
{"value over max", "happy=101", Emotion{}, true},
24+
{"empty value", "happy=", Emotion{}, true},
25+
{"trailing comma", "happy=50,", Emotion{}, true},
26+
{"leading comma", ",happy=50", Emotion{}, true},
27+
{"empty middle segment", "happy=50,,sad=50", Emotion{}, true},
28+
}
29+
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
got, err := ParseEmotion(tt.input)
33+
if (err != nil) != tt.wantErr {
34+
t.Errorf("ParseEmotion(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
35+
return
36+
}
37+
if !tt.wantErr && got != tt.want {
38+
t.Errorf("ParseEmotion(%q) = %+v, want %+v", tt.input, got, tt.want)
39+
}
40+
})
41+
}
42+
}
43+
44+
func TestEmotionString(t *testing.T) {
45+
tests := []struct {
46+
name string
47+
e Emotion
48+
want string
49+
}{
50+
{"zero value", Emotion{}, ""},
51+
{"only happy", Emotion{Happy: 50}, "happy=50"},
52+
{"happy and fun", Emotion{Happy: 40, Fun: 60}, "happy=40,fun=60"},
53+
{"all four in fixed order", Emotion{Happy: 10, Sad: 40, Angry: 30, Fun: 20}, "happy=10,fun=20,angry=30,sad=40"},
54+
{"zero values are omitted", Emotion{Happy: 0, Sad: 50}, "sad=50"},
55+
}
56+
57+
for _, tt := range tests {
58+
t.Run(tt.name, func(t *testing.T) {
59+
if got := tt.e.String(); got != tt.want {
60+
t.Errorf("Emotion.String() = %q, want %q", got, tt.want)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestEmotionIsZero(t *testing.T) {
67+
tests := []struct {
68+
name string
69+
e Emotion
70+
want bool
71+
}{
72+
{"all zero", Emotion{}, true},
73+
{"happy set", Emotion{Happy: 1}, false},
74+
{"sad set", Emotion{Sad: 1}, false},
75+
{"angry set", Emotion{Angry: 1}, false},
76+
{"fun set", Emotion{Fun: 1}, false},
77+
}
78+
79+
for _, tt := range tests {
80+
t.Run(tt.name, func(t *testing.T) {
81+
if got := tt.e.IsZero(); got != tt.want {
82+
t.Errorf("Emotion.IsZero() = %v, want %v", got, tt.want)
83+
}
84+
})
85+
}
86+
}

0 commit comments

Comments
 (0)