Added test for minimal example. Omit optional values when not set. Added messages for route info, production drop and route end.

This commit is contained in:
Oliver Jakoubek 2020-05-29 11:02:33 +02:00
parent 9525e7b97f
commit df964e0ecf
9 changed files with 283 additions and 8 deletions

View file

@ -1,6 +1,26 @@
# feragstring # feragstring
*feragstring* is a Go package for creating a Ferag string file from a JSON data structure. If you don't know what Ferag (the company) or a Ferag string is you probably don't need this package ;-) *feragstring* is a Go package for creating a FERAG string file from a JSON data structure. If you don't know what Ferag (the company) or a Ferag string is you probably don't need this package ;-)
## The shortest possible FERAG string
According to FERAG's documentation this is the shortest possible FERAG string:
```
%2440+40DEMO2009!
%2401+11E1_ROUTE_100 !
%2402+11E1_ROUTE_100 +590+91000000+20E1 !
%2403+12R100RE001 +1300123!
%2406+11E1_ROUTE_100 !
%2441+40DEMO2009!
```
The variable values are:
- the title is 'DEMO2009'
- a route named 'E1_ROUTE_100'
- an edition called 'E1'
- a production drop 'R100RE001' with 123 copies
## Usage ## Usage

View file

@ -13,6 +13,9 @@ type FeragString struct {
ProductReferencesNr int ProductReferencesNr int
RouteListEntries []*RouteListEntry RouteListEntries []*RouteListEntry
RouteListEntryCount int RouteListEntryCount int
RouteInfoEntries []*RouteInfo
ProductionDropEntries []*ProductionDrop
RouteEndEntries []*RouteEnd
} }
func NewFeragString() *FeragString { func NewFeragString() *FeragString {
@ -39,6 +42,18 @@ func (fs *FeragString) PrintOut() string {
info += rl.Message() info += rl.Message()
} }
for _, ri := range fs.RouteInfoEntries {
info += ri.Message()
}
for _, pd := range fs.ProductionDropEntries {
info += pd.Message()
}
for _, re := range fs.RouteEndEntries {
info += re.Message()
}
info += fs.TitleEnd.Message() info += fs.TitleEnd.Message()
return info return info
} }
@ -55,9 +70,24 @@ func (fs *FeragString) AddProductReference(pr *ProductReference) error {
func (fs *FeragString) AddRouteListEntry(rl *RouteListEntry) error { func (fs *FeragString) AddRouteListEntry(rl *RouteListEntry) error {
fs.RouteListEntryCount++ fs.RouteListEntryCount++
if rl.routeCode == 0 { //if rl.routeCode == 0 {
rl.SetRouteCode(fs.RouteListEntryCount) // rl.SetRouteCode(fs.RouteListEntryCount)
} //}
fs.RouteListEntries = append(fs.RouteListEntries, rl) fs.RouteListEntries = append(fs.RouteListEntries, rl)
return nil return nil
} }
func (fs *FeragString) AddRouteInfo(ri *RouteInfo) error {
fs.RouteInfoEntries = append(fs.RouteInfoEntries, ri)
return nil
}
func (fs *FeragString) AddProductionDrop(pd *ProductionDrop) error {
fs.ProductionDropEntries = append(fs.ProductionDropEntries, pd)
return nil
}
func (fs *FeragString) AddRouteEnd(re *RouteEnd) error {
fs.RouteEndEntries = append(fs.RouteEndEntries, re)
return nil
}

View file

@ -1,6 +1,9 @@
package feragstring package feragstring
import "fmt" import (
"fmt"
"strings"
)
type FeragMessage struct { type FeragMessage struct {
messageStart string messageStart string
@ -21,7 +24,7 @@ func (fm *FeragMessage) MessageTemplate() MessageTemplateFunc {
return func(fm *FeragMessage, s string) string { return func(fm *FeragMessage, s string) string {
message := fm.getMessageStart() message := fm.getMessageStart()
message += s message += s
message += fm.getMessageEnd() + linebreak message += fm.getMessageEnd()
return message return strings.TrimSpace(message) + linebreak
} }
} }

View file

@ -3,9 +3,59 @@ package feragstring
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings"
"testing" "testing"
) )
func TestShortestFeragString(t *testing.T) {
fs := NewFeragString()
fs.SetTitleName("DEMO2009")
rl := NewRouteListEntry()
rl.SetRouteName("E1_ROUTE_100")
fs.AddRouteListEntry(rl)
ri := NewRouteInfo()
ri.SetRouteName("E1_ROUTE_100")
ri.SetEditionName("E1")
fs.AddRouteInfo(ri)
pd := NewProductionDrop()
pd.SetAgentName("R100RE001")
pd.SetNumberOfCopies(123)
fs.AddProductionDrop(pd)
re := NewRouteEnd()
re.SetRouteName("E1_ROUTE_100")
fs.AddRouteEnd(re)
producedContent := fs.PrintOut()
want := getTestFileContent("D:\\TEMP\\Feragstring\\minimal_want.txt")
fmt.Println("--WANT-----------------")
fmt.Println(want)
fmt.Println("-----------------------")
fmt.Println("--PRODUCED-------------")
fmt.Println(producedContent)
fmt.Println("-----------------------")
fmt.Println("=======================")
filename := "D:\\TEMP\\Feragstring\\minimal_test.txt"
err := ioutil.WriteFile(filename, []byte(producedContent), 0644)
if err != nil {
panic(err)
}
fmt.Printf("COMPARE: %d", strings.Compare(producedContent, want))
if strings.Compare(producedContent, want) != 0 {
//if strings.TrimSpace(producedContent) != strings.TrimSpace(want) {
t.Errorf("Produced result does not equal to minimal example")
}
}
func TestNewFeragString(t *testing.T) { func TestNewFeragString(t *testing.T) {
fs := NewFeragString() fs := NewFeragString()
fs.SetTitleName("MOP10629") fs.SetTitleName("MOP10629")
@ -59,7 +109,7 @@ func TestNewFeragString(t *testing.T) {
testcontent := getTestFileContent("D:\\TEMP\\Feragstring\\test.txt") testcontent := getTestFileContent("D:\\TEMP\\Feragstring\\test.txt")
fmt.Println("=======================") fmt.Println("=======================")
fmt.Println("TEST:") fmt.Println("WANT:")
fmt.Println(testcontent) fmt.Println(testcontent)
fmt.Println("-----------------------") fmt.Println("-----------------------")
fmt.Println("PRODUCED:") fmt.Println("PRODUCED:")

46
production_drop.go Normal file
View file

@ -0,0 +1,46 @@
package feragstring
import "fmt"
type ProductionDrop struct {
FeragMessage
agentName string
numberOfCopies int
}
func (pd *ProductionDrop) NumberOfCopies() string {
return fmt.Sprintf("+13%05d", pd.numberOfCopies)
}
func (pd *ProductionDrop) SetNumberOfCopies(numberOfCopies int) {
pd.numberOfCopies = numberOfCopies
}
func (pd *ProductionDrop) AgentName() string {
return fmt.Sprintf("+12%-10s", pd.agentName)
}
func (pd *ProductionDrop) SetAgentName(agentName string) {
pd.agentName = agentName
}
func NewProductionDrop() *ProductionDrop {
pd := ProductionDrop{
FeragMessage: FeragMessage{
messageStart: "2403",
messageEnd: "!",
},
}
return &pd
}
func (pd *ProductionDrop) Payload() string {
data := pd.AgentName()
data += pd.NumberOfCopies()
return data
}
func (pd *ProductionDrop) Message() string {
message := pd.FeragMessage.MessageTemplate()
return message(&pd.FeragMessage, pd.Payload())
}

36
route_end.go Normal file
View file

@ -0,0 +1,36 @@
package feragstring
import "fmt"
type RouteEnd struct {
FeragMessage
routeName string
}
func (re *RouteEnd) RouteName() string {
return fmt.Sprintf("+11%-13s", re.routeName)
}
func (re *RouteEnd) SetRouteName(routeName string) {
re.routeName = routeName
}
func NewRouteEnd() *RouteEnd {
re := RouteEnd{
FeragMessage: FeragMessage{
messageStart: "2406",
messageEnd: "!",
},
}
return &re
}
func (re *RouteEnd) Payload() string {
data := re.RouteName()
return data
}
func (re *RouteEnd) Message() string {
message := re.FeragMessage.MessageTemplate()
return message(&re.FeragMessage, re.Payload())
}

66
route_info.go Normal file
View file

@ -0,0 +1,66 @@
package feragstring
import "fmt"
type RouteInfo struct {
FeragMessage
routeName string
topsheetMarker int
eaAddressDefinition int
editionName string
}
func (ri *RouteInfo) EditionName() string {
return fmt.Sprintf("+20%-30s", ri.editionName)
}
func (ri *RouteInfo) SetEditionName(editionName string) {
ri.editionName = editionName
}
func (ri *RouteInfo) EaAddressDefinition() string {
return fmt.Sprintf("+91%06d", ri.eaAddressDefinition)
}
func (ri *RouteInfo) SetEaAddressDefinition(eaAddressDefinition int) {
ri.eaAddressDefinition = eaAddressDefinition
}
func (ri *RouteInfo) TopsheetMarker() string {
return fmt.Sprintf("+59%1d", ri.topsheetMarker)
}
func (ri *RouteInfo) SetTopsheetMarker(topsheetMarker int) {
ri.topsheetMarker = topsheetMarker
}
func (ri *RouteInfo) SetRouteName(routeName string) {
ri.routeName = routeName
}
func (ri *RouteInfo) RouteName() string {
return fmt.Sprintf("+11%-13s", ri.routeName)
}
func NewRouteInfo() *RouteInfo {
ri := RouteInfo{
FeragMessage: FeragMessage{
messageStart: "2402",
messageEnd: "!",
},
}
return &ri
}
func (ri *RouteInfo) Payload() string {
data := ri.RouteName()
data += ri.TopsheetMarker()
data += ri.EaAddressDefinition()
data += ri.EditionName()
return data
}
func (ri *RouteInfo) Message() string {
message := ri.FeragMessage.MessageTemplate()
return message(&ri.FeragMessage, ri.Payload())
}

View file

@ -11,6 +11,9 @@ type RouteListEntry struct {
} }
func (r *RouteListEntry) CopiesInRoute() string { func (r *RouteListEntry) CopiesInRoute() string {
if r.copiesInRoute == 0 {
return ""
}
return fmt.Sprintf("+23%06d", r.copiesInRoute) return fmt.Sprintf("+23%06d", r.copiesInRoute)
} }
@ -19,6 +22,9 @@ func (r *RouteListEntry) SetCopiesInRoute(copiesInRoute int) {
} }
func (r *RouteListEntry) RampNumber() string { func (r *RouteListEntry) RampNumber() string {
if r.rampNumber == 0 {
return ""
}
return fmt.Sprintf("+25%02d", r.rampNumber) return fmt.Sprintf("+25%02d", r.rampNumber)
} }
@ -27,6 +33,9 @@ func (r *RouteListEntry) SetRampNumber(rampNumber int) {
} }
func (r *RouteListEntry) RouteCode() string { func (r *RouteListEntry) RouteCode() string {
if r.routeCode == 0 {
return ""
}
return fmt.Sprintf("+79%05d", r.routeCode) return fmt.Sprintf("+79%05d", r.routeCode)
} }

View file

@ -20,6 +20,9 @@ func (ti *TitleInfo) SetPrintObjectName(printObjectName string) {
} }
func (ti *TitleInfo) AdditionalInfo() string { func (ti *TitleInfo) AdditionalInfo() string {
if ti.additionalInfo == "" {
return ""
}
return fmt.Sprintf("+08%-50s", ti.additionalInfo) return fmt.Sprintf("+08%-50s", ti.additionalInfo)
} }
@ -28,6 +31,9 @@ func (ti *TitleInfo) SetAdditionalInfo(additionalInfo string) {
} }
func (ti *TitleInfo) PrintObjectColor() string { func (ti *TitleInfo) PrintObjectColor() string {
if ti.printObjectColor == "" {
return ""
}
return fmt.Sprintf("+94%-8s", ti.printObjectColor) return fmt.Sprintf("+94%-8s", ti.printObjectColor)
} }
@ -36,6 +42,9 @@ func (ti *TitleInfo) SetPrintObjectColor(printObjectColor string) {
} }
func (ti *TitleInfo) CountryCode() string { func (ti *TitleInfo) CountryCode() string {
if ti.countryCode == "" {
return ""
}
return fmt.Sprintf("+97%-2s", ti.countryCode) return fmt.Sprintf("+97%-2s", ti.countryCode)
} }
@ -52,6 +61,9 @@ func (ti *TitleInfo) SetPublicationDate(publicationDateString string) {
} }
func (ti *TitleInfo) PublicationDate() string { func (ti *TitleInfo) PublicationDate() string {
if ti.publicationDate.IsZero() {
return ""
}
return fmt.Sprintf("+95%-6s", ti.publicationDate.Format(dateOutputFormat)) return fmt.Sprintf("+95%-6s", ti.publicationDate.Format(dateOutputFormat))
} }
@ -72,6 +84,9 @@ func (ti *TitleInfo) Payload() string {
} }
func (ti *TitleInfo) PrintObjectName() string { func (ti *TitleInfo) PrintObjectName() string {
if ti.printObjectName == "" {
return ""
}
return fmt.Sprintf("+93%-12s", ti.printObjectName) return fmt.Sprintf("+93%-12s", ti.printObjectName)
} }