Skip to content

Commit

Permalink
Use parser function from station package
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperMalachowski committed Mar 3, 2024
1 parent 52ba2ce commit 7a68d6e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pkg/station/station.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package station

import (
"fmt"

"gopkg.in/yaml.v3"
)

type Definition struct {
Hills []Hill `json:"hills" yaml:"hills"`
}
Expand All @@ -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
}
94 changes: 94 additions & 0 deletions pkg/station/station_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 7a68d6e

Please sign in to comment.