mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-25 02:02:37 +02:00
test dump
This commit is contained in:
parent
8697fa0956
commit
ea907da224
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -31,14 +32,14 @@ func (c *MethodTryCommand) Execute(args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := p.Post(c.MethodName, params, &result); err != nil {
|
if err := p.Post(c.MethodName, params, &result); err != nil {
|
||||||
debug.Dump(params)
|
fmt.Println(debug.Dump(params))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.Dump(map[string]interface{}{
|
fmt.Println(debug.Dump(map[string]interface{}{
|
||||||
"params": params,
|
"params": params,
|
||||||
"result": result,
|
"result": result,
|
||||||
})
|
}))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,14 +7,15 @@ package debug
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Dump an interface to the stdout
|
Dump an interface to the stdout
|
||||||
*/
|
*/
|
||||||
func Dump(v interface{}) error {
|
func Dump(v interface{}) string {
|
||||||
d := json.NewEncoder(os.Stdout)
|
result, err := json.MarshalIndent(v, "", " ")
|
||||||
d.SetIndent("", " ")
|
if err != nil {
|
||||||
return d.Encode(v)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
debug.Dump(RawResult)
|
fmt.Println(debug.Dump(RawResult))
|
||||||
}
|
}
|
||||||
|
|
||||||
if Result.Stat != "ok" {
|
if Result.Stat != "ok" {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user