2020-02-18 01:40:16 +08:00
|
|
|
package gotop
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-24 03:07:08 +08:00
|
|
|
"github.com/xxxserxxx/gotop/v4/widgets"
|
2020-02-18 01:40:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
i string
|
|
|
|
f func(c Config, e error)
|
|
|
|
}{
|
|
|
|
{
|
2020-06-08 05:40:03 +08:00
|
|
|
i: "graphhorizontalscale",
|
2020-02-18 01:40:16 +08:00
|
|
|
f: func(c Config, e error) {
|
2020-06-08 05:40:03 +08:00
|
|
|
assert.Error(t, e, "invalid graphhorizontalscale syntax")
|
2020-02-18 01:40:16 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-08 05:40:03 +08:00
|
|
|
i: "helpvisible=true=false",
|
2020-02-18 01:40:16 +08:00
|
|
|
f: func(c Config, e error) {
|
|
|
|
assert.NotNil(t, e)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-08 05:40:03 +08:00
|
|
|
i: "GRAPHHORIZONTALSCALE=1\nhelpVisible=true",
|
2020-02-18 01:40:16 +08:00
|
|
|
f: func(c Config, e error) {
|
|
|
|
assert.Nil(t, e, "unexpected error")
|
2020-06-08 05:40:03 +08:00
|
|
|
assert.Equal(t, 1, c.GraphHorizontalScale)
|
2020-02-18 01:40:16 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: "graphhorizontalscale=a",
|
|
|
|
f: func(c Config, e error) {
|
|
|
|
assert.Error(t, e, "expected invalid value for graphhorizontalscale")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
i: "helpvisible=a",
|
|
|
|
f: func(c Config, e error) {
|
|
|
|
assert.Error(t, e, "expected invalid value for helpvisible")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-06-08 05:40:03 +08:00
|
|
|
i: "helpvisible=true\nupdateinterval=30\naveragecpu=true\nPerCPULoad=true\ntempscale=F\nstatusbar=true\nnetinterface=eth0\nlayout=minimal\nmaxlogsize=200",
|
2020-02-18 01:40:16 +08:00
|
|
|
f: func(c Config, e error) {
|
|
|
|
assert.Nil(t, e, "unexpected error")
|
|
|
|
assert.Equal(t, true, c.HelpVisible)
|
|
|
|
assert.Equal(t, time.Duration(30), c.UpdateInterval)
|
|
|
|
assert.Equal(t, true, c.AverageLoad)
|
|
|
|
assert.Equal(t, true, c.PercpuLoad)
|
2020-06-08 05:40:03 +08:00
|
|
|
assert.Equal(t, widgets.TempScale(70), c.TempScale)
|
2020-02-18 01:40:16 +08:00
|
|
|
assert.Equal(t, true, c.Statusbar)
|
|
|
|
assert.Equal(t, "eth0", c.NetInterface)
|
|
|
|
assert.Equal(t, "minimal", c.Layout)
|
|
|
|
assert.Equal(t, int64(200), c.MaxLogSize)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
in := strings.NewReader(tc.i)
|
2020-06-08 05:40:03 +08:00
|
|
|
c := NewConfig()
|
2020-04-24 02:38:22 +08:00
|
|
|
e := load(in, &c)
|
2020-02-18 01:40:16 +08:00
|
|
|
tc.f(c, e)
|
|
|
|
}
|
|
|
|
}
|