Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: add a DiffJSON api #193

Closed
xhd2015 opened this issue Jun 5, 2024 · 1 comment
Closed

Testing: add a DiffJSON api #193

xhd2015 opened this issue Jun 5, 2024 · 1 comment
Labels

Comments

@xhd2015
Copy link
Owner

xhd2015 commented Jun 5, 2024

It's tempting to embed this function in xgo, though it's just a wrapper around git, but it feels powerful when combining with reflect.DeepEqual():

Usage:

package dosomething

import (
	"reflect"
	"testing"
)

func TestDoSomething(t *testing.T) {
	type args struct {
             ...
	}
	tests := []struct {
		name    string
		args    args
		want    ...
		wantErr bool
	}{
            ...
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			got, err := DoSomething(tt.args)
			if (err != nil) != tt.wantErr {
				t.Errorf("DoSomething() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("DoSomething() = %v, want %v, diff: %s", got, tt.want, DiffJSON(tt.want, got))
			}
		})
	}
}

Assert message:

dosomething_test.go:109: DoSomething() = map[a.go:map[2:4-0:0:0xc0003cee40]], want map[a.go:map[2:4-0:0:0xc0003ced00]], diff: diff --git a/expected b/actual
    index 77721f7..9e95f61 100755
    --- a/expected
    +++ b/actual
    @@ -1,7 +1,7 @@
     {
       "a.go": {
         "2:4-0:0": {
    -      "package": "github.com/xhd2015/xgo",
    +      "package": "github.com/xhd2015/xgo/.",
           "name": "Test",
           "file": "a.go",
           "line": 2,

Implementation:

package assert

import (
	"encoding/json"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"

	"github.com/xhd2015/xgo/support/cmd"
)

func DiffJSON(expected interface{}, actual interface{}) string {
	diff, err := diffJSON(expected, actual)
	if err != nil {
		sep := ""
		if diff != "" {
			sep = ", "
		}
		diff += sep + "err: " + err.Error()
	}
	return diff
}
func diffJSON(expected interface{}, actual interface{}) (string, error) {
	tmpDir, err := os.MkdirTemp("", "diff")
	if err != nil {
		return "", err
	}
	defer os.RemoveAll(tmpDir)

	jsonExpected, err := json.MarshalIndent(expected, "", "  ")
	if err != nil {
		return "", err
	}
	jsonAcutal, err := json.MarshalIndent(actual, "", "  ")
	if err != nil {
		return "", err
	}

	err = ioutil.WriteFile(filepath.Join(tmpDir, "expected"), jsonExpected, 0755)
	if err != nil {
		return "", err
	}
	err = ioutil.WriteFile(filepath.Join(tmpDir, "actual"), jsonAcutal, 0755)
	if err != nil {
		return "", err
	}

	diff, err := cmd.Dir(tmpDir).Output("git", "diff", "--no-index", "--", "expected", "actual")
	if err != nil {
		if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() != 0 {
			err = nil
		}
	}
	return diff, err
}
@xhd2015 xhd2015 added the testing label Jun 5, 2024
@xhd2015
Copy link
Owner Author

xhd2015 commented Jul 22, 2024

This has been added in #249

Source: https://github.com/xhd2015/xgo/blob/master/support/assert/diff.go

@xhd2015 xhd2015 closed this as completed Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant