mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-24 17:52:36 +02:00
test dump
This commit is contained in:
parent
8697fa0956
commit
ea907da224
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
71
internal/debug/dump_test.go
Normal file
71
internal/debug/dump_test.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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" {
|
||||
|
Loading…
x
Reference in New Issue
Block a user