Commit 42c8fd5b authored by Boris Mühmer's avatar Boris Mühmer
Browse files

added x2x test with JSON and YAML

parent 7da0c6c1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
module repositories.muehmer.net/bsmrgo/x2x

go 1.19

require gopkg.in/yaml.v3 v3.0.1

go.sum

0 → 100644
+4 −0
Original line number Diff line number Diff line
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+89 −3
Original line number Diff line number Diff line
package x2x

import "testing"
import (
	"encoding/json"
	"fmt"
	"reflect"
	"testing"

func TestSkip(t *testing.T) {
	t.Skip("not implemented")
	"gopkg.in/yaml.v3"
)

type Foo struct {
	F  string
	O1 string
	O2 string
}

type Bar struct {
	B string
	A string
	R string
}

type Example struct {
	Name string
	Foos []Foo
	Bars []Bar
}

func createExample() *Example {
	e := &Example{
		Name: "Example",
		Foos: []Foo{},
		Bars: []Bar{},
	}

	for i := 0; i < 3; i++ {
		f := Foo{
			F:  fmt.Sprintf("F %d", i),
			O1: fmt.Sprintf("O1 %d", i),
			O2: fmt.Sprintf("O2 %d", i),
		}
		e.Foos = append(e.Foos, f)
		b := Bar{
			B: fmt.Sprintf("B %d", i),
			A: fmt.Sprintf("A %d", i),
			R: fmt.Sprintf("R %d", i),
		}
		e.Bars = append(e.Bars, b)
	}

	return e
}

func TestX2X(t *testing.T) {
	e0 := createExample()

	js, err := json.MarshalIndent(e0, "", "  ")
	if err != nil {
		t.Fatalf("json.MarshalIndent() failed with: %s", err)
	}

	t.Logf("JSON:\n%s", js)

	var e1 Example
	if err := json.Unmarshal(js, &e1); err != nil {
		t.Fatalf("json.Unmarshal() failed with: %s", err)
	}

	ys, err := yaml.Marshal(e1)
	if err != nil {
		t.Fatalf("yaml.Marshal() failed with: %s", err)
	}

	t.Logf("YAML:\n%s", ys)

	var e2 Example
	if err := yaml.Unmarshal(ys, &e2); err != nil {
		t.Fatalf("yaml.Unmarshal() failed with: %s", err)
	}

	if !reflect.DeepEqual(e0, e1) {
		t.Errorf("reflect.DeepEqual(e0, e1) are not equal")
	}

	if !reflect.DeepEqual(e0, e2) {
		t.Errorf("reflect.DeepEqual(e0, e2) are not equal")
	}

	if !reflect.DeepEqual(e1, e2) {
		t.Errorf("reflect.DeepEqual(e1, e2) are not equal")
	}
}