|
| 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