This repository has been archived by the owner on Jun 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steamid_test.go
151 lines (123 loc) · 4.75 KB
/
steamid_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
package steamid_test
import (
"testing"
"github.com/nightexcessive/steamid"
)
var steamIDTests = []struct {
Universe uint8
Instance uint32
Type steamid.AccountType
ID uint32
ExpectedID2 string
ExpectedID3 string
}{
{steamid.UniverseUnspecified, 0, steamid.AccountTypeInvalid, 0, "UNKNOWN", "[I:0:0]"},
{steamid.UniverseUnspecified, 1, steamid.AccountTypeIndividual, 2, "STEAM_0:0:1", "[U:0:2]"},
{steamid.UniverseUnspecified, 2, steamid.AccountTypeIndividual, 2, "STEAM_0:0:1", "[U:0:2:2]"},
{steamid.UniverseUnspecified, 1, steamid.AccountTypeIndividual, 35812865, "STEAM_0:1:17906432", "[U:0:35812865]"},
}
func TestSteamID(t *testing.T) {
for _, details := range steamIDTests {
steamID := steamid.FromValues(details.Universe, details.Instance, details.Type, details.ID)
got2 := steamID.SteamID2()
if got2 != details.ExpectedID2 {
t.Errorf("SteamID2([Universe %d Instance %d Type %d ID %d]): got %q, expected %q", details.Universe, details.Instance, details.Type, details.ID, got2, details.ExpectedID2)
}
got3 := steamID.SteamID3()
if got3 != details.ExpectedID3 {
t.Errorf("SteamID3([Universe %d Instance %d Type %d ID %d]): got %q, expected %q", details.Universe, details.Instance, details.Type, details.ID, got3, details.ExpectedID3)
}
gotMarshal, err := steamID.MarshalText()
if err != nil {
t.Errorf("MarshalText([Universe %d Instance %d Type %d ID %d]): %s", details.Universe, details.Instance, details.Type, details.ID, err)
} else if string(gotMarshal) != details.ExpectedID2 && string(gotMarshal) != details.ExpectedID3 {
t.Errorf("MarshalText([Universe %d Instance %d Type %d ID %d]): got %q, expected %q or %q", details.Universe, details.Instance, details.Type, details.ID, string(gotMarshal), details.ExpectedID2, details.ExpectedID3)
}
}
}
func TestParseID(t *testing.T) {
for _, details := range steamIDTests {
_, err := steamid.Parse(details.ExpectedID3)
if err != nil {
t.Errorf("Parse(%q): error parsing: %s", details.ExpectedID3, err)
}
_, err = steamid.Parse(details.ExpectedID2)
if err != nil {
t.Errorf("Parse(%q): error parsing: %s", details.ExpectedID2, err)
}
}
}
func TestParseID2(t *testing.T) {
for _, details := range steamIDTests {
steamID, err := steamid.ParseV2(details.ExpectedID2)
if err != nil {
t.Errorf("ParseV2(%q): error parsing: %s", details.ExpectedID2, err)
continue
}
if u := steamID.Universe(); u != details.Universe {
t.Errorf("Universe(%q): got %d, expected %d", details.ExpectedID2, u, details.Universe)
}
typ := steamID.AccountType()
if inst := steamID.AccountInstance(); inst != details.Instance &&
!(typ == steamid.AccountTypeIndividual && details.Instance != 1) { // Version 2 IDs can't carry instance information
t.Errorf("AccountInstance(%q): got %d, expected %d", details.ExpectedID2, inst, details.Instance)
}
if typ != details.Type {
t.Errorf("AccountType(%q): got %d, expected %d", details.ExpectedID2, typ, details.Type)
}
if id := steamID.AccountID(); id != details.ID {
t.Errorf("AccountID(%q): got %d, expected %d", details.ExpectedID2, id, details.ID)
}
}
}
func TestParseID3(t *testing.T) {
for _, details := range steamIDTests {
steamID, err := steamid.ParseV3(details.ExpectedID3)
if err != nil {
t.Errorf("ParseV3(%q): error parsing: %s", details.ExpectedID3, err)
continue
}
if u := steamID.Universe(); u != details.Universe {
t.Errorf("Universe(%q): got %d, expected %d", details.ExpectedID3, u, details.Universe)
}
if inst := steamID.AccountInstance(); inst != details.Instance {
t.Errorf("AccountInstance(%q): got %d, expected %d", details.ExpectedID3, inst, details.Instance)
}
if typ := steamID.AccountType(); typ != details.Type {
t.Errorf("AccountType(%q): got %d, expected %d", details.ExpectedID3, typ, details.Type)
}
if id := steamID.AccountID(); id != details.ID {
t.Errorf("AccountID(%q): got %d, expected %d", details.ExpectedID3, id, details.ID)
}
}
}
func TestCommunityID(t *testing.T) {
const (
communityID = 76561197960287930
expected2 = "STEAM_0:0:11101"
expected3 = "[U:0:22202]"
)
id := steamid.ParseCommunityID(communityID, steamid.AccountTypeIndividual)
if id2 := id.SteamID2(); id2 != expected2 {
t.Errorf("SteamID2(%d): got %s, expected %s", communityID, id2, expected2)
}
if id3 := id.SteamID3(); id3 != expected3 {
t.Errorf("SteamID3(%d): got %s, expected %s", communityID, id3, expected3)
}
}
func BenchmarkParseID2(b *testing.B) {
const testID = "STEAM_0:0:1"
for i := 0; i < b.N; i++ {
if _, err := steamid.ParseV2(testID); err != nil {
b.Fatal(err)
}
}
}
func BenchmarkParseID3(b *testing.B) {
const testID = "[U:0:2]"
for i := 0; i < b.N; i++ {
if _, err := steamid.ParseV3(testID); err != nil {
b.Fatal(err)
}
}
}