Reactivate tests and write additional ones for protocol
Move two functions into globals as they are benefitial in other tests aswell and probably later in the implementation too.
This commit is contained in:
parent
e318067d90
commit
680d64fc21
|
@ -6,8 +6,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.mmo.to/ppForge/ppforge/cop"
|
"gitea.mmo.to/ppForge/ppforge/cop"
|
||||||
|
@ -15,29 +13,10 @@ import (
|
||||||
"gitea.mmo.to/ppForge/ppforge/protocol"
|
"gitea.mmo.to/ppForge/ppforge/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getCurrentDir() string {
|
|
||||||
_, filename, _, _ := runtime.Caller(0)
|
|
||||||
return path.Dir(filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRepoDir(start string) (string, error) {
|
|
||||||
var repoPath string
|
|
||||||
entries, err := os.ReadDir(start)
|
|
||||||
for _, v := range entries {
|
|
||||||
if strings.HasSuffix(v.Name(), ".git") {
|
|
||||||
repoPath = start
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if repoPath == "" {
|
|
||||||
return getRepoDir(path.Dir(start))
|
|
||||||
}
|
|
||||||
return repoPath, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupSuite(t *testing.T) func(t *testing.T) {
|
func setupSuite(t *testing.T) func(t *testing.T) {
|
||||||
// setup
|
// setup
|
||||||
// setup testing home dir
|
// setup testing home dir
|
||||||
repo, err := getRepoDir(getCurrentDir())
|
repo, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -63,7 +42,7 @@ func TestOpen(t *testing.T) {
|
||||||
defer td(t)
|
defer td(t)
|
||||||
|
|
||||||
fcop := cop.FileCOP{}
|
fcop := cop.FileCOP{}
|
||||||
wd, err := getRepoDir(getCurrentDir())
|
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Os error: %s", err)
|
fmt.Printf("Os error: %s", err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -83,7 +62,7 @@ func TestClosed(t *testing.T) {
|
||||||
defer td(t)
|
defer td(t)
|
||||||
|
|
||||||
fcop := cop.FileCOP{}
|
fcop := cop.FileCOP{}
|
||||||
wd, err := getRepoDir(getCurrentDir())
|
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Os error: %s", err)
|
fmt.Printf("Os error: %s", err)
|
||||||
t.Fail()
|
t.Fail()
|
||||||
|
@ -118,7 +97,7 @@ func TestGlobalCOP(t *testing.T) {
|
||||||
fmt.Println("Length != 1")
|
fmt.Println("Length != 1")
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
wd, _ := getRepoDir(getCurrentDir())
|
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||||
fcop, err := cop.NewFileCOP(path.Join(wd, "test", "cop"))
|
fcop, err := cop.NewFileCOP(path.Join(wd, "test", "cop"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error on loading new FileCOP", err)
|
fmt.Println("Error on loading new FileCOP", err)
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
package cop
|
package cop
|
||||||
|
|
||||||
|
/*
|
||||||
|
The COP struct is the base for all collections of protocols, it does not implement the COPer interface itself, because it shouldn't be instantiated at all, only in it's specializations.
|
||||||
|
*/
|
||||||
|
|
||||||
import "gitea.mmo.to/ppForge/ppforge/protocol"
|
import "gitea.mmo.to/ppForge/ppforge/protocol"
|
||||||
|
|
||||||
// COP represents a Collection of Protocols
|
// COP represents the base for Collection of Protocols
|
||||||
type COP struct {
|
type COP struct {
|
||||||
Type string
|
Type string
|
||||||
Protocols map[string]ProtocolCollectionEntry
|
Protocols map[string]ProtocolCollectionEntry
|
||||||
|
@ -14,3 +18,6 @@ type ProtocolCollectionEntry struct {
|
||||||
Path string
|
Path string
|
||||||
Protocol protocol.ProtocolMeta
|
Protocol protocol.ProtocolMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A collection entry has a path, which is meant to be a filesystem path, but doesn't necessarily have to be.
|
||||||
|
// If a collection is implemented as a database, this could also be the unique id. It is there to uniquely identify the entry.
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
package globals
|
package globals
|
||||||
|
|
||||||
import "os/user"
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Global variables, Mocks, etc.
|
// Global variables, Mocks, etc.
|
||||||
// This package shall not have any dependency towards the application itself!
|
// This package shall not have any dependency towards the application itself!
|
||||||
|
@ -31,3 +38,29 @@ type Marshaler interface {
|
||||||
fromJSON() error
|
fromJSON() error
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// GetCurrentDir returns the current directory of the called function
|
||||||
|
func GetCurrentDir() string {
|
||||||
|
_, filename, _, _ := runtime.Caller(0)
|
||||||
|
return path.Dir(filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRepoDir tries to find the parent git repository path
|
||||||
|
func GetRepoDir(start string) (string, error) {
|
||||||
|
var repoPath string
|
||||||
|
entries, err := os.ReadDir(start)
|
||||||
|
for _, v := range entries {
|
||||||
|
if strings.HasSuffix(v.Name(), ".git") {
|
||||||
|
repoPath = start
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// linux
|
||||||
|
if start == "/" {
|
||||||
|
return "", errors.New("Parent repository dir couldn't be found")
|
||||||
|
}
|
||||||
|
// windows, how?
|
||||||
|
if repoPath == "" {
|
||||||
|
return GetRepoDir(path.Dir(start))
|
||||||
|
}
|
||||||
|
return repoPath, err
|
||||||
|
}
|
||||||
|
|
|
@ -2,8 +2,8 @@ package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -37,6 +37,13 @@ func (prot *Protocol) AppendField(field Field) int {
|
||||||
return i + 1
|
return i + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendFields to protocol
|
||||||
|
func (prot *Protocol) AppendFields(fields ...Field) {
|
||||||
|
for _, e := range fields {
|
||||||
|
prot.AppendField(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// AddField to protocol
|
// AddField to protocol
|
||||||
func (prot *Protocol) AddField(index int, field *Field) {
|
func (prot *Protocol) AddField(index int, field *Field) {
|
||||||
if len(prot.Structure) == index {
|
if len(prot.Structure) == index {
|
||||||
|
@ -69,7 +76,7 @@ func (prot *Protocol) UpdateFieldByElement(element int, field *Field) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveFieldByName from the protocol
|
// RemoveFieldByName from the protocol
|
||||||
func (prot *Protocol) RemoveFieldByName(name string) {
|
func (prot *Protocol) RemoveFieldByName(name string) error {
|
||||||
element := -1
|
element := -1
|
||||||
for i, f := range prot.Structure {
|
for i, f := range prot.Structure {
|
||||||
if f.Name == name {
|
if f.Name == name {
|
||||||
|
@ -77,9 +84,10 @@ func (prot *Protocol) RemoveFieldByName(name string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if element == -1 {
|
if element == -1 {
|
||||||
return
|
return errors.New("Element not found")
|
||||||
}
|
}
|
||||||
prot.RemoveFieldByElement(element)
|
prot.RemoveFieldByElement(element)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveFieldByElement from the protocol
|
// RemoveFieldByElement from the protocol
|
||||||
|
@ -110,7 +118,7 @@ func (prot *Protocol) Save(path string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = os.WriteFile(path, data, fs.ModeAppend)
|
err = os.WriteFile(path, data, 0644)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.mmo.to/ppForge/ppforge/globals"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdateMetaData(t *testing.T) {
|
func TestUpdateMetaData(t *testing.T) {
|
||||||
|
@ -57,10 +60,7 @@ func GenerateFields() []Field {
|
||||||
func GenerateStructure() *Protocol {
|
func GenerateStructure() *Protocol {
|
||||||
p := NewProtocolStructure()
|
p := NewProtocolStructure()
|
||||||
slc := GenerateFields()
|
slc := GenerateFields()
|
||||||
for i, e := range slc {
|
p.AppendFields(slc...)
|
||||||
p.AppendField(e)
|
|
||||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
|
||||||
}
|
|
||||||
for i, e := range p.Structure {
|
for i, e := range p.Structure {
|
||||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||||
}
|
}
|
||||||
|
@ -86,22 +86,35 @@ func TestFieldAppend(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func TestFieldAdd(t *testing.T) {
|
||||||
func TestUpdateFieldByName(t *testing.T) {
|
f := NewField(
|
||||||
p := GenerateStructure()
|
"testfield",
|
||||||
f := NewField(
|
"Description",
|
||||||
"UpdatedField", "", "", 42, nil, false, true,
|
"",
|
||||||
)
|
8,
|
||||||
UpdateFieldByName(p, "testfield3", f)
|
nil,
|
||||||
for i, e := range p.Structure {
|
false,
|
||||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
false,
|
||||||
}
|
)
|
||||||
if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 {
|
p := NewProtocolStructure()
|
||||||
t.Log("Update field by Ref failed!")
|
p.AddField(0, f)
|
||||||
t.Fail()
|
p.AddField(0, f)
|
||||||
}
|
}
|
||||||
} //
|
|
||||||
*/
|
func TestUpdateFieldByName(t *testing.T) {
|
||||||
|
p := GenerateStructure()
|
||||||
|
f := NewField(
|
||||||
|
"UpdatedField", "", "", 42, nil, false, true,
|
||||||
|
)
|
||||||
|
p.UpdateFieldByName("testfield3", f)
|
||||||
|
for i, e := range p.Structure {
|
||||||
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||||
|
}
|
||||||
|
if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 {
|
||||||
|
t.Log("Update field by Ref failed!")
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
} //
|
||||||
func TestUpdateFieldByElement(t *testing.T) {
|
func TestUpdateFieldByElement(t *testing.T) {
|
||||||
p := GenerateStructure()
|
p := GenerateStructure()
|
||||||
f := NewField(
|
f := NewField(
|
||||||
|
@ -117,19 +130,18 @@ func TestUpdateFieldByElement(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
func TestRemoveFieldByName(t *testing.T) {
|
||||||
func TestRemoveFieldByName(t *testing.T) {
|
p := GenerateStructure()
|
||||||
p := GenerateStructure()
|
p.RemoveFieldByName("testfield3")
|
||||||
RemoveFieldByName(p, "testfield3")
|
for i, e := range p.Structure {
|
||||||
for i, e := range p.Structure {
|
fmt.Printf("%d %s\n", i, e.ToJson())
|
||||||
fmt.Printf("%d %s\n", i, e.ToJson())
|
}
|
||||||
}
|
if p.Structure[3].Name != "testfield4" {
|
||||||
if p.Structure[3].Name != "testfield4" {
|
t.Log("Remove by name failed!")
|
||||||
t.Log("Remove by name failed!")
|
t.Fail()
|
||||||
t.Fail()
|
}
|
||||||
}
|
} //
|
||||||
}//
|
|
||||||
*/
|
|
||||||
func TestRemoveFieldByElement(t *testing.T) {
|
func TestRemoveFieldByElement(t *testing.T) {
|
||||||
p := GenerateStructure()
|
p := GenerateStructure()
|
||||||
p.RemoveFieldByElement(3)
|
p.RemoveFieldByElement(3)
|
||||||
|
@ -146,3 +158,22 @@ func TestToJson(t *testing.T) {
|
||||||
p := GenerateStructure()
|
p := GenerateStructure()
|
||||||
fmt.Print(p.ToJSON())
|
fmt.Print(p.ToJSON())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSaveAndLoad(t *testing.T) {
|
||||||
|
p := GenerateStructure()
|
||||||
|
wd, err := globals.GetRepoDir(globals.GetCurrentDir())
|
||||||
|
if err != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
path := path.Join(wd, "test", "protocolsavetest.protocoljson")
|
||||||
|
p.Save(path)
|
||||||
|
p2 := NewProtocolStructure()
|
||||||
|
p2.Load(path)
|
||||||
|
if len(p2.Structure) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
p3, err := LoadNew(path)
|
||||||
|
if len(p3.Structure) == 0 {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue