Skip to content

Commit

Permalink
commandline parse test
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bretterklieber committed May 31, 2024
1 parent 3acfb20 commit e827dee
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
1 change: 1 addition & 0 deletions commandline.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewCommandLine(logger *slog.Logger, backend backend.Backend) *CommandLine {
return &CommandLine{
log: logger,
backend: backend,
Listen: defaultListen,
}
}

Expand Down
111 changes: 109 additions & 2 deletions commandline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,119 @@ package main
import (
"bytes"
"errors"
"flag"
"github.com/mbretter/go-mmcli-svr/backend"
"github.com/stretchr/testify/assert"
"log/slog"
"os"
"testing"
)

func TestCommandLine_New(t *testing.T) {
var buff bytes.Buffer
logger := slog.New(slog.NewTextHandler(&buff, nil))

backendMock := backend.NewBackendMock(t)
cmd := NewCommandLine(logger, backendMock)

assert.Equal(t, defaultListen, cmd.Listen)
assert.Equal(t, defaultGpsRefresh, cmd.GpsRefresh)
assert.Empty(t, cmd.LocationGatherings)
assert.Same(t, logger, cmd.log)
assert.Same(t, backendMock, cmd.backend)
}

func TestCommandLine_Parse(t *testing.T) {
tests := []struct {
name string
args []string
settings *CommandLine
error string
}{
{
name: "No args",
args: []string{},
settings: &CommandLine{
Listen: defaultListen,
},
},
{
name: "Listen",
args: []string{"-listen=192.168.1.22:12345"},
settings: &CommandLine{
Listen: "192.168.1.22:12345",
},
},
{
name: "GPS refresh",
args: []string{"-gps-refresh=5"},
settings: &CommandLine{
Listen: defaultListen,
GpsRefresh: 5,
},
},
{
name: "Location enable agps-msa",
args: []string{"-location-enable=agps-msa"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"agps-msa", "gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable gps-nmea",
args: []string{"-location-enable=gps-nmea"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable agps-msa",
args: []string{"-location-enable=agps-msb"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"agps-msb", "gps-nmea"}, // assistet gps needs gps-nmea
},
},
{
name: "Location enable multi",
args: []string{"-location-enable=agps-msa,gps-nmea,gps-raw,3gpp"},
settings: &CommandLine{
Listen: defaultListen,
LocationGatherings: []string{"3gpp", "agps-msa", "gps-nmea", "gps-raw"},
},
},
{
name: "Unknown location",
args: []string{"-location-enable=xxx"},
settings: &CommandLine{
Listen: defaultListen,
},
error: "unknown location type: xxx",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

os.Args = append([]string{"cmd"}, tt.args...)
// reset flag, otherwise redefinition errors might be thrown
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) //flags are now reset

cmd := &CommandLine{}
err := cmd.Parse()

assert.Equal(t, tt.settings, cmd)
if len(tt.error) > 0 {
assert.Equal(t, tt.error, err.Error())
} else {
assert.Nil(t, err)
}
})
}
}

func TestCommandLine_Activate(t *testing.T) {
tests := []struct {
name string
Expand All @@ -18,7 +125,7 @@ func TestCommandLine_Activate(t *testing.T) {
settings *CommandLine
}{
{
name: "GPS refresh success",
name: "GPS refresh",
execArgs: []string{"--location-set-gps-refresh-rate=10"},
logs: []string{`level=INFO msg="Set gps-refresh" refresh=10`},
settings: &CommandLine{
Expand All @@ -35,7 +142,7 @@ func TestCommandLine_Activate(t *testing.T) {
},
},
{
name: "Location 3gpp enable success",
name: "Location 3gpp enable",
execArgs: []string{"--location-enable-3gpp"},
logs: []string{`level=INFO msg="Enable location gatherings" location=3gpp`, `level=INFO msg=mmcli message=success`},
settings: &CommandLine{
Expand Down

0 comments on commit e827dee

Please sign in to comment.