diff --git a/app.go b/app.go index 55271fc..51bca01 100644 --- a/app.go +++ b/app.go @@ -1,15 +1,15 @@ package main import ( - "github.com/kacpermalachowski/marshal-controller/pkg/station" - "github.com/kacpermalachowski/marshal-controller/pkg/td2" "context" "crypto/sha256" "fmt" "os" + "github.com/kacpermalachowski/marshal-controller/pkg/station" + "github.com/kacpermalachowski/marshal-controller/pkg/td2" + "github.com/wailsapp/wails/v2/pkg/runtime" - "gopkg.in/yaml.v3" ) type App struct { @@ -50,8 +50,7 @@ func (a *App) LoadStationFile() station.Definition { return a.station } - var station station.Definition - err = yaml.Unmarshal(data, &station) + station, err := station.ParseStationDefinition(data) if err != nil { runtime.LogError(a.ctx, fmt.Sprint(err)) return a.station diff --git a/pkg/station/station.go b/pkg/station/station.go index 1500ad8..33518b8 100644 --- a/pkg/station/station.go +++ b/pkg/station/station.go @@ -1,5 +1,11 @@ package station +import ( + "fmt" + + "gopkg.in/yaml.v3" +) + type Definition struct { Hills []Hill `json:"hills" yaml:"hills"` } @@ -8,3 +14,17 @@ type Hill struct { Signal string `json:"signal" yaml:"signal"` Repeaters []string `json:"repeaters" yaml:"repeaters"` } + +func ParseStationDefinition(data []byte) (Definition, error) { + var station Definition + err := yaml.Unmarshal(data, &station) + if err != nil { + return Definition{}, err + } + + if len(station.Hills) == 0 { + return Definition{}, fmt.Errorf("the program is unusable without hills defined") + } + + return station, err +} diff --git a/pkg/station/station_test.go b/pkg/station/station_test.go new file mode 100644 index 0000000..839c5b6 --- /dev/null +++ b/pkg/station/station_test.go @@ -0,0 +1,94 @@ +package station_test + +import ( + "reflect" + "testing" + + "github.com/kacpermalachowski/marshal-controller/pkg/station" +) + +func TestParseStationDefinition(t *testing.T) { + tc := []struct { + Name string + Data string + Expected station.Definition + ExpectErr bool + }{ + { + Name: "No hills", + Data: `hills:`, + ExpectErr: true, + }, + { + Name: "Single hill and signal", + Data: `hills: + - signal: Tr1`, + Expected: station.Definition{ + Hills: []station.Hill{ + { + Signal: "Tr1", + }, + }, + }, + }, + { + Name: "Single hill with repeaters", + Data: `hills: + - signal: Tr1 + repeaters: + - Tr2`, + Expected: station.Definition{ + Hills: []station.Hill{ + { + Signal: "Tr1", + Repeaters: []string{ + "Tr2", + }, + }, + }, + }, + }, + { + Name: "Multiple hill with repeaters", + Data: `hills: + - signal: Tr1 + repeaters: + - Tr2 + - signal: Tr3 + repeaters: + - Tr4`, + Expected: station.Definition{ + Hills: []station.Hill{ + { + Signal: "Tr1", + Repeaters: []string{ + "Tr2", + }, + }, + { + Signal: "Tr3", + Repeaters: []string{ + "Tr4", + }, + }, + }, + }, + }, + } + + for _, c := range tc { + t.Run(c.Name, func(t *testing.T) { + def, err := station.ParseStationDefinition([]byte(c.Data)) + if err != nil && !c.ExpectErr { + t.Errorf("Unexpected error occured: %s", err) + } + if c.ExpectErr && err == nil { + t.Error("Expected error, but no one occured") + } + + if !reflect.DeepEqual(c.Expected, def) { + t.Errorf("Expected %v, but got %v", c.Expected, def) + } + }) + } +}