Example #1
0
func runSteps(t *testing.T, singleton bool, steps []step) {
	logOut := &bytes.Buffer{}
	logOut.WriteByte('\n')
	logW := &iotool.SafeWriter{W: logOut}
	defer func() {
		logW.SetWriter(nil) // disable writes to avoid races
		t.Log(logOut)
	}()

	dataOut := &bytes.Buffer{}
	sim := simulator.New(singleton, dataOut, logW)
	defer sim.Close()

	for _, step := range steps {
		if step.JSON != nil {
			jsonData, _ := json.Marshal(step.JSON)
			step.Cmd = fmt.Sprintf("%s %s", step.Cmd, string(jsonData))
		}

		fmt.Fprintf(logW, "== %s\n", step.Cmd)
		sim.RunCommand(step.Cmd)
		if step.Check != nil {
			actual := reflect.New(reflect.Indirect(reflect.ValueOf(step.Check)).Type()).Interface()
			if err := json.Unmarshal(dataOut.Bytes(), actual); err != nil {
				t.Fatal("json decode error", err)
			}
			if diff := pretty.Compare(step.Check, actual); diff != "" {
				t.Fatalf("check failed:\n%s", diff)
			}
		}
		dataOut.Reset()
	}
}
Example #2
0
func ExampleCompare() {
	type ShipManifest struct {
		Name     string
		Crew     map[string]string
		Androids int
		Stolen   bool
	}

	reported := &ShipManifest{
		Name: "Spaceship Heart of Gold",
		Crew: map[string]string{
			"Zaphod Beeblebrox": "Galactic President",
			"Trillian":          "Human",
			"Ford Prefect":      "A Hoopy Frood",
			"Arthur Dent":       "Along for the Ride",
		},
		Androids: 1,
		Stolen:   true,
	}

	expected := &ShipManifest{
		Name: "Spaceship Heart of Gold",
		Crew: map[string]string{
			"Rowan Artosok": "Captain",
		},
		Androids: 1,
		Stolen:   false,
	}

	fmt.Println(pretty.Compare(reported, expected))
	// Output:
	//  {
	//   Name:     "Spaceship Heart of Gold",
	//   Crew:     {
	// -            Arthur Dent:       "Along for the Ride",
	// -            Ford Prefect:      "A Hoopy Frood",
	// -            Trillian:          "Human",
	// -            Zaphod Beeblebrox: "Galactic President",
	// +            Rowan Artosok: "Captain",
	//             },
	//   Androids: 1,
	// - Stolen:   true,
	// + Stolen:   false,
	//  }
}