-
Notifications
You must be signed in to change notification settings - Fork 1
/
id_test.go
151 lines (113 loc) · 4.49 KB
/
id_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) 2024 Six After, Inc
//
// This source code is licensed under the Apache 2.0 License found in the
// LICENSE file in the root directory of this source tree.
package nanoid
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestID_String tests the String() method of the ID type.
// It verifies that the String() method returns the underlying string value.
func TestID_String(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize expected using Must()
expectedID := Must()
expected := expectedID.String()
// Actual is obtained by calling String() on the ID
actual := expectedID.String()
// Assert that actual equals expected
is.Equal(expected, actual, "ID.String() should return the underlying string")
}
// TestID_MarshalText tests the MarshalText() method of the ID type.
// It verifies that MarshalText() returns the correct byte slice representation of the ID.
func TestID_MarshalText(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize expected using Must()
expectedID := Must()
expectedBytes := []byte(expectedID.String())
// Actual is obtained by calling MarshalText()
actualBytes, err := expectedID.MarshalText()
// Assert no error occurred
is.NoError(err, "MarshalText() should not return an error")
// Assert that actual bytes match expected bytes
is.Equal(expectedBytes, actualBytes, "MarshalText() should return the correct byte slice")
}
// TestID_UnmarshalText tests the UnmarshalText() method of the ID type.
// It verifies that UnmarshalText() correctly parses the byte slice and assigns the value to the ID.
func TestID_UnmarshalText(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize expected using Must()
expectedID := Must()
inputBytes := []byte(expectedID.String())
// Initialize a zero-valued ID
var actualID ID
// Call UnmarshalText with inputBytes
err := actualID.UnmarshalText(inputBytes)
// Assert no error occurred
is.NoError(err, "UnmarshalText() should not return an error")
// Assert that actualID matches expectedID
is.Equal(expectedID, actualID, "UnmarshalText() should correctly assign the input value to ID")
}
// TestID_MarshalBinary tests the MarshalBinary() method of the ID type.
// It verifies that MarshalBinary() returns the correct byte slice representation of the ID.
func TestID_MarshalBinary(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize expected using Must()
expectedID := Must()
expectedBytes := []byte(expectedID.String())
// Actual is obtained by calling MarshalBinary()
actualBytes, err := expectedID.MarshalBinary()
// Assert no error occurred
is.NoError(err, "MarshalBinary() should not return an error")
// Assert that actual bytes match expected bytes
is.Equal(expectedBytes, actualBytes, "MarshalBinary() should return the correct byte slice")
}
// TestID_UnmarshalBinary tests the UnmarshalBinary() method of the ID type.
// It verifies that UnmarshalBinary() correctly parses the byte slice and assigns the value to the ID.
func TestID_UnmarshalBinary(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize expected using Must()
expectedID := Must()
inputBytes := []byte(expectedID.String())
// Initialize a zero-valued ID
var actualID ID
// Call UnmarshalBinary with inputBytes
err := actualID.UnmarshalBinary(inputBytes)
// Assert no error occurred
is.NoError(err, "UnmarshalBinary() should not return an error")
// Assert that actualID matches expectedID
is.Equal(expectedID, actualID, "UnmarshalBinary() should correctly assign the input value to ID")
}
// TestID_Compare tests the Compare() method of the ID type.
// It verifies that Compare() correctly compares two IDs and returns the expected result.
func TestID_Compare(t *testing.T) {
t.Parallel()
is := assert.New(t)
id1 := ID("FgEVN8QMTrnKGvBxFjtjw")
id2 := ID("zTxG5Nl21ZAoM8Fabqk3H")
// Case 1: id1 < id2
is.Equal(-1, id1.Compare(id2), "id1 should be less than id2")
// Case 2: id1 = id2
is.Equal(0, id1.Compare(id1), "id1 should be equal to id1")
// Case 3: id1 > id2
is.Equal(1, id2.Compare(id1), "id2 should be greater than id1")
}
// TestID_IsEmpty tests the IsEmpty() method of the ID type.
// It verifies that IsEmpty() correctly returns true for an empty ID and false for a non-empty ID.
func TestID_IsEmpty(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Initialize two IDs using Must()
id1 := Must()
id2 := EmptyID
// Case 1: id1 is not empty
is.False(id1.IsEmpty(), "id1 should not be empty")
// Case 2: id2 is empty
is.True(id2.IsEmpty(), "id2 should be empty")
}