refactor-for-testability (#3)
Co-authored-by: jdl <jdl@desktop> Co-authored-by: jdl <jdl@crumpington.com> Reviewed-on: #3
This commit is contained in:
57
peer/files_test.go
Normal file
57
peer/files_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package peer
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilePaths(t *testing.T) {
|
||||
confDir := configDir("netName")
|
||||
if filepath.Base(confDir) != "netName" {
|
||||
t.Fatal(confDir)
|
||||
}
|
||||
if filepath.Base(filepath.Dir(confDir)) != ".vppn" {
|
||||
t.Fatal(confDir)
|
||||
}
|
||||
|
||||
path := peerConfigPath("netName")
|
||||
if path != filepath.Join(confDir, "peer-config.json") {
|
||||
t.Fatal(path)
|
||||
}
|
||||
|
||||
path = peerStatePath("netName")
|
||||
if path != filepath.Join(confDir, "peer-state.json") {
|
||||
t.Fatal(path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreLoadJson(t *testing.T) {
|
||||
type Object struct {
|
||||
Name string
|
||||
Age int
|
||||
Price float64
|
||||
}
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
outPath := filepath.Join(tmpDir, "object.json")
|
||||
|
||||
obj := Object{
|
||||
Name: "Jason",
|
||||
Age: 22,
|
||||
Price: 123.534,
|
||||
}
|
||||
|
||||
if err := storeJson(obj, outPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
obj2 := Object{}
|
||||
if err := loadJson(outPath, &obj2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(obj, obj2) {
|
||||
t.Fatal(obj, obj2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user