2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2021-11-13 14:46:06 -08:00
|
|
|
|
|
|
|
import (
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/mock"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2021-11-13 14:46:06 -08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSysInfo(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
ExpectedString string
|
|
|
|
ExpectDisabled bool
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo SystemInfo
|
2021-11-13 14:46:06 -08:00
|
|
|
Precision int
|
|
|
|
Template string
|
|
|
|
}{
|
2022-01-23 11:24:02 -08:00
|
|
|
{
|
|
|
|
Case: "physical mem",
|
|
|
|
ExpectedString: "50",
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo: SystemInfo{PhysicalPercentUsed: 50},
|
2022-01-23 11:24:02 -08:00
|
|
|
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "physical mem 2 digits",
|
|
|
|
ExpectedString: "60.51",
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo: SystemInfo{Precision: 2, PhysicalPercentUsed: 60.51},
|
2022-01-23 11:24:02 -08:00
|
|
|
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "physical meme rounded",
|
|
|
|
ExpectedString: "61",
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo: SystemInfo{Precision: 0, PhysicalPercentUsed: 61},
|
2022-01-23 11:24:02 -08:00
|
|
|
Template: "{{ round .PhysicalPercentUsed .Precision }}",
|
|
|
|
},
|
2021-11-13 14:46:06 -08:00
|
|
|
{
|
|
|
|
Case: "load",
|
|
|
|
ExpectedString: "0.22 0.12 0",
|
|
|
|
Template: "{{ round .Load1 .Precision }} {{round .Load5 .Precision }} {{round .Load15 .Precision }}",
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo: SystemInfo{Precision: 2, Load1: 0.22, Load5: 0.12, Load15: 0}},
|
|
|
|
{Case: "not enabled", ExpectDisabled: true, SysInfo: SystemInfo{PhysicalPercentUsed: 0, SwapPercentUsed: 0}},
|
2021-11-13 14:46:06 -08:00
|
|
|
{
|
|
|
|
Case: "2 physical cpus",
|
2022-02-01 05:07:58 -08:00
|
|
|
ExpectedString: "1200 1200",
|
2021-11-13 14:46:06 -08:00
|
|
|
Template: "{{range $cpu := .CPU}}{{round $cpu.Mhz 2 }} {{end}}",
|
2022-01-26 05:10:18 -08:00
|
|
|
SysInfo: SystemInfo{CPU: []cpu.InfoStat{{Mhz: 1200}, {Mhz: 1200}}},
|
2021-11-13 14:46:06 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2022-01-26 01:23:18 -08:00
|
|
|
env := new(mock.MockedEnvironment)
|
2022-01-12 14:39:34 -08:00
|
|
|
tc.SysInfo.env = env
|
2022-01-26 04:53:35 -08:00
|
|
|
tc.SysInfo.props = properties.Map{
|
2022-01-23 12:37:51 -08:00
|
|
|
Precision: tc.Precision,
|
2021-11-13 14:46:06 -08:00
|
|
|
}
|
2022-01-26 05:26:56 -08:00
|
|
|
enabled := tc.SysInfo.Enabled()
|
2021-11-13 14:46:06 -08:00
|
|
|
if tc.ExpectDisabled {
|
2022-01-23 11:24:02 -08:00
|
|
|
assert.Equal(t, false, enabled, tc.Case)
|
2021-11-13 14:46:06 -08:00
|
|
|
} else {
|
2022-01-23 12:37:51 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, renderTemplate(env, tc.Template, tc.SysInfo), tc.Case)
|
2021-11-13 14:46:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|