pp

package
v0.123.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2023 License: Apache-2.0 Imports: 17 Imported by: 0

README

Pretty Print (PP)

the pp package provides you with a set of tools that pretty print any Go value.

usage

PP / Format
package main

import (
	"bytes"
	"encoding/json"
	"github.com/adamluzsi/testcase/pp"
)

type ExampleStruct struct {
	A string
	B int
}

func main() {
	var buf bytes.Buffer
	bs, _ := json.Marshal(ExampleStruct{
		A: "The Answer",
		B: 42,
	})
	buf.Write(bs)

	pp.PP(buf)
}

output

bytes.Buffer{
        buf: []byte(`{"A":"The Answer","B":42}`),
        off: 0,
        lastRead: 0,
}
Diff
package main

import (
	"fmt"
	"github.com/adamluzsi/testcase/pp"
)

type ExampleStruct struct {
	A string
	B int
}

func main(t *testing.T) {
	fmt.Println(pp.Diff(ExampleStruct{
		A: "The Answer",
		B: 42,
	}, ExampleStruct{
		A: "The Question",
		B: 42,
	}))
}

output in GNU diff side-by-side style

pp_test.ExampleStruct{     pp_test.ExampleStruct{
  A: "The Answer",      |    A: "The Question",
  B: 42,                     B: 42,
}   

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(v1, v2 any) string

Diff format the values in pp.Format and compare the results line by line in a side-by-side style.

Example
package main

import (
	"github.com/adamluzsi/testcase/pp"
)

type ExampleStruct struct {
	A string
	B int
}

func main() {
	_ = pp.Diff(ExampleStruct{
		A: "The Answer",
		B: 42,
	}, ExampleStruct{
		A: "The Question",
		B: 42,
	})
}

func DiffString

func DiffString(val, oth string) string

DiffString compare strings line by line in a side-by-side style. The diff style is similar to GNU "diff -y".

Example
package main

import (
	"github.com/adamluzsi/testcase/pp"
)

func main() {
	_ = pp.Diff("aaa\nbbb\nccc\n", "aaa\nccc\n")
}

func FPP added in v0.101.0

func FPP(w io.Writer, vs ...any) (int, error)

func Format

func Format(v any) string
Example
package main

import (
	"github.com/adamluzsi/testcase/pp"
)

type ExampleStruct struct {
	A string
	B int
}

func main() {
	_ = pp.Format(ExampleStruct{
		A: "The Answer",
		B: 42,
	})
}

func PP added in v0.101.0

func PP(vs ...any)
Example (UnexportedFields)
package main

import (
	"bytes"
	"encoding/json"
	"github.com/adamluzsi/testcase/pp"
)

type ExampleStruct struct {
	A string
	B int
}

func main() {
	var buf bytes.Buffer
	bs, _ := json.Marshal(ExampleStruct{
		A: "The Answer",
		B: 42,
	})
	buf.Write(bs)

	pp.PP(buf)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL