testingutils

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 5 Imported by: 11

README

Pretty Json Diff

func PrettyJsonDiff(expected interface{}, actual interface{}) (r string)

It convert the two objects into pretty json, and diff them, output the result.

	type Company struct {
	    Name string
	}
	type People struct {
	    Name    string
	    Age     int
	    Company Company
	}
	
	p1 := People{
	    Name: "Felix",
	    Age:  20,
	    Company: Company{
	        Name: "The Plant",
	    },
	}
	p2 := People{
	    Name: "Tom",
	    Age:  21,
	    Company: Company{
	        Name: "Microsoft",
	    },
	}
	
	fmt.Println(PrettyJsonDiff(p1, p2))
	//Output:
	// --- Expected
	// +++ Actual
	// @@ -1,7 +1,7 @@
	//  {
	// -	"Name": "Felix",
	// -	"Age": 20,
	// +	"Name": "Tom",
	// +	"Age": 21,
	//  	"Company": {
	// -		"Name": "The Plant"
	// +		"Name": "Microsoft"
	//  	}
	//  }

Println Json

func PrintlnJson(vals ...interface{})

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrettyJsonDiff

func PrettyJsonDiff(expected interface{}, actual interface{}) (r string)

It convert the two objects into pretty json, and diff them, output the result.

Example
package main

import (
	"fmt"
	"testing"
)

func main() {
	type Company struct {
		Name string
	}
	type People struct {
		Name    string
		Age     int
		Company Company
	}

	p1 := People{
		Name: "Felix",
		Age:  20,
		Company: Company{
			Name: "The Plant",
		},
	}
	p2 := People{
		Name: "Tom",
		Age:  21,
		Company: Company{
			Name: "Microsoft",
		},
	}

	fmt.Println(PrettyJsonDiff(p1, p2))
}

func main(t *testing.T) {
	str1 := `{
"Active": true,
	"Addresses": [],	"Age": 0,
		"Avatar": "",
"Company": "",
		"CreditCard": {
			"Number": "411111111111",
			"Issuer": "VISA"
		},
		"ID": 1,
	"Languages": null,
		"Name": "jinzhu",
		"Profile": {
			"ID": 0,
			"Name": "jinzhu",
			"Phone": {
				"ID": 0,
				"Num": "110"
			},
			"Sex": "male"
		},
		"RegisteredAt": "2017-01-01 00:00",
		"Role": ""
	}`

	str2 := `{
		"Active": true,
		"Addresses": [],
		"Age": 0,
		"Avatar": "",
		"Company": "",
		"ID": 1,
		"Languages": null,
		"Name": "jinzhu",
		"Profile": {
			"ID": 0,
			"Name": "jinzhu",
			"Phone": {
				"ID": 0,
				"Num": "110"
			},
			"Sex": "male"
		},
		"RegisteredAt": "2017-01-01 00:00",
		"Role": ""
	}`

	fmt.Println(PrettyJsonDiff(str1, str2))
	// Output
	//  	"Age": 0,
	//  	"Avatar": "",
	//  	"Company": "",
	// -	"CreditCard": {
	// -		"Number": "411111111111",
	// -		"Issuer": "VISA"
	// -	},
	//  	"ID": 1,
	//  	"Languages": null,
	//  	"Name": "jinzhu",
}
Output:

--- Expected
+++ Actual
@@ -1,7 +1,7 @@
 {
-	"Name": "Felix",
-	"Age": 20,
+	"Name": "Tom",
+	"Age": 21,
 	"Company": {
-		"Name": "The Plant"
+		"Name": "Microsoft"
 	}
 }

func PrintlnJson

func PrintlnJson(vals ...interface{})

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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