test dump

This commit is contained in:
Celogeek 2022-01-02 17:46:18 +01:00
parent 8697fa0956
commit ea907da224
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A
4 changed files with 82 additions and 9 deletions

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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" {