From ea907da22496dec9ff29d4e9c48cbd13b90a1eb4 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 2 Jan 2022 17:46:18 +0100 Subject: [PATCH] test dump --- cmd/piwigo-cli/method_try.go | 7 ++-- internal/debug/dump.go | 11 +++--- internal/debug/dump_test.go | 71 ++++++++++++++++++++++++++++++++++++ internal/piwigo/post.go | 2 +- 4 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 internal/debug/dump_test.go diff --git a/cmd/piwigo-cli/method_try.go b/cmd/piwigo-cli/method_try.go index 1a46d67..7cc4b30 100644 --- a/cmd/piwigo-cli/method_try.go +++ b/cmd/piwigo-cli/method_try.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "net/url" "strings" @@ -31,14 +32,14 @@ func (c *MethodTryCommand) Execute(args []string) error { } if err := p.Post(c.MethodName, params, &result); err != nil { - debug.Dump(params) + fmt.Println(debug.Dump(params)) return err } - debug.Dump(map[string]interface{}{ + fmt.Println(debug.Dump(map[string]interface{}{ "params": params, "result": result, - }) + })) return nil } diff --git a/internal/debug/dump.go b/internal/debug/dump.go index 3975ab2..54435d1 100644 --- a/internal/debug/dump.go +++ b/internal/debug/dump.go @@ -7,14 +7,15 @@ package debug import ( "encoding/json" - "os" ) /* Dump an interface to the stdout */ -func Dump(v interface{}) error { - d := json.NewEncoder(os.Stdout) - d.SetIndent("", " ") - return d.Encode(v) +func Dump(v interface{}) string { + result, err := json.MarshalIndent(v, "", " ") + if err != nil { + return "" + } + return string(result) } diff --git a/internal/debug/dump_test.go b/internal/debug/dump_test.go new file mode 100644 index 0000000..0aa281d --- /dev/null +++ b/internal/debug/dump_test.go @@ -0,0 +1,71 @@ +package debug_test + +import ( + "fmt" + "math" + "testing" + "time" + + "github.com/celogeek/piwigo-cli/internal/debug" + "github.com/celogeek/piwigo-cli/internal/piwigo/piwigotools" +) + +func TestHelloWorldDump(t *testing.T) { + var test struct { + Hello string `json:"hello"` + World string `json:"world"` + } + test.Hello = "abc" + test.World = "def" + + want := `{ + "hello": "abc", + "world": "def" +}` + received := debug.Dump(test) + if received != want { + t.Fatalf("Dump hello world failed!\nReceive:\n\"%s\"\nWant:\n\"%s\"\n", received, want) + } +} + +func TestDumpTimeResult(t *testing.T) { + var test struct { + CreatedAt *piwigotools.TimeResult + } + now := time.Now() + tr := piwigotools.TimeResult(now) + test.CreatedAt = &tr + + want := fmt.Sprintf(`{ + "CreatedAt": "%s" +}`, now.Format("2006-01-02 15:04:05")) + + received := debug.Dump(test) + if received != want { + t.Fatalf("Dump TimeResult failed!\nReceive:\n\"%s\"\nWant:\n\"%s\"\n", received, want) + } +} + +func TestDumpNullTimeResult(t *testing.T) { + var test struct { + CreatedAt *piwigotools.TimeResult + } + + want := fmt.Sprint(`{ + "CreatedAt": null +}`) + + received := debug.Dump(test) + if received != want { + t.Fatalf("Dump TimeResult failed!\nReceive:\n\"%s\"\nWant:\n\"%s\"\n", received, want) + } +} + +func TestDumpError(t *testing.T) { + test := math.Inf(1) + want := "" + received := debug.Dump(test) + if received != want { + t.Fatalf("Dump TimeResult failed!\nReceive:\n\"%s\"\nWant:\n\"%s\"\n", received, want) + } +} diff --git a/internal/piwigo/post.go b/internal/piwigo/post.go index 9349c6a..27f0756 100644 --- a/internal/piwigo/post.go +++ b/internal/piwigo/post.go @@ -99,7 +99,7 @@ func (p *Piwigo) Post(method string, form *url.Values, resp interface{}) error { return err } - debug.Dump(RawResult) + fmt.Println(debug.Dump(RawResult)) } if Result.Stat != "ok" {