pp

package
v0.142.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 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[T any](v1, v2 T)

Diff will pretty print two value and show side-by-side the difference between them.

Example
package main

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

type ExampleStruct struct {
	A string
	B int
}

func main() {
	pp.DiffFormat(ExampleStruct{
		A: "The Answer",
		B: 42,
	}, ExampleStruct{
		A: "The Question",
		B: 42,
	})
}
Output:

func DiffFormat added in v0.134.0

func DiffFormat[T any](v1, v2 T) string

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

Example
package main

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

type ExampleStruct struct {
	A string
	B int
}

func main() {
	fmt.Println(pp.DiffFormat(ExampleStruct{
		A: "The Answer",
		B: 42,
	}, ExampleStruct{
		A: "The Question",
		B: 42,
	}))
}
Output:

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.DiffFormat("aaa\nbbb\nccc\n", "aaa\nccc\n")
}
Output:

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,
	})
}
Output:

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)
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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