Skip to content

Commit 9b0bd0f

Browse files
test: GetMemInfo test added using dependency injection
1 parent f2757bb commit 9b0bd0f

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

pkg/cpuInfo_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,21 +175,21 @@ core id : 5
175175
cpu cores : 6
176176
`
177177

178-
type spyLoader struct{}
178+
type spyCpuLoader struct{}
179179

180-
func (l *spyLoader) Load() (string, error) {
180+
func (l *spyCpuLoader) Load() (string, error) {
181181
return cpuinfo, nil
182182
}
183183
func TestGetCpuInfo(t *testing.T) {
184-
var _ Loader = (*spyLoader)(nil)
185-
got, _ := getCpuInfo(&spyLoader{})
184+
var _ Loader = (*spyCpuLoader)(nil)
185+
got, _ := getCpuInfo(&spyCpuLoader{})
186186
expected := CpuInfo{
187187
NumCores: 12,
188188
VendorId: "GenuineIntel",
189189
ModelName: "Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz",
190190
CacheSize: "12288 KB",
191191
CpuMHZ: 1233.7074,
192192
}
193-
assert.Equal(t, expected, got, "The two words should be the same.")
193+
assert.Equal(t, expected, got)
194194

195195
}

pkg/memInfo_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package psutils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
var memInfo string = `MemTotal: 16239040 kB
10+
MemFree: 1313720 kB
11+
MemAvailable: 8580468 kB
12+
Buffers: 292824 kB
13+
Cached: 7358304 kB
14+
SwapCached: 24172 kB
15+
Active: 6177748 kB
16+
Inactive: 6296272 kB
17+
`
18+
19+
type spyMemLoader struct{}
20+
21+
func (l *spyMemLoader) Load() (string, error) {
22+
return memInfo, nil
23+
}
24+
func TestGetMemInfo(t *testing.T) {
25+
var _ Loader = (*spyMemLoader)(nil)
26+
got, _ := getMemInfo(&spyMemLoader{})
27+
expected := MemInfo{
28+
TotalMemoryKB: 16239040,
29+
UsedMemoryKB: 14925320,
30+
AvailableMemoryKB: 8580468,
31+
}
32+
assert.Equal(t, expected, got)
33+
34+
}

0 commit comments

Comments
 (0)