This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils_test.go
75 lines (64 loc) · 1.76 KB
/
utils_test.go
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
package sleet
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestAmountToString(t *testing.T) {
t.Run("convert", func(t *testing.T) {
actual := AmountToString(&Amount{
Amount: 100,
Currency: "USD",
})
if !cmp.Equal(actual, "100") {
t.Error("string does not match expected")
}
})
}
func TestAmountToDecimalString(t *testing.T) {
t.Run("convert", func(t *testing.T) {
actual := AmountToDecimalString(&Amount{
Amount: 100,
Currency: "USD",
})
if !cmp.Equal(actual, "1.00") {
t.Error("string does not match expected")
}
})
}
func TestTruncateString(t *testing.T) {
const str = "Test string"
t.Run("Truncate length less than str length", func(t *testing.T) {
truncated := TruncateString(str, 4)
if !cmp.Equal(truncated, "Test") {
t.Error("Truncated string does not match expected")
}
})
t.Run("Truncate length equals str length", func(t *testing.T) {
truncated := TruncateString(str, len(str))
if !cmp.Equal(truncated, str) {
t.Error("Truncated string does not match expected")
}
})
t.Run("Truncate length greater than str length", func(t *testing.T) {
truncated := TruncateString(str, len(str)+5)
if !cmp.Equal(truncated, str) {
t.Error("Truncated string does not match expected")
}
})
}
func TestDefaultIfEmpty(t *testing.T) {
const fallback = "fallback"
t.Run("Defaults to fallback when primary is empty", func(t *testing.T) {
nonEmpty := DefaultIfEmpty("", fallback)
if !cmp.Equal(nonEmpty, fallback) {
t.Error("Truncated string does not match expected")
}
})
t.Run("Uses primary when non-empty", func(t *testing.T) {
primary := "primary"
nonEmpty := DefaultIfEmpty(primary, fallback)
if !cmp.Equal(nonEmpty, primary) {
t.Error("Truncated string does not match expected")
}
})
}