Example #1
0
// This example demonstrates how to use a ConfigState.
func ExampleConfigState() {
	// Modify the indent level of the ConfigState only.  The global
	// configuration is not modified.
	scs := spew.ConfigState{Indent: "\t"}

	// Output using the ConfigState instance.
	v := map[string]int{"one": 1}
	scs.Printf("v: %v\n", v)
	scs.Dump(v)

	// Output:
	// v: map[one:1]
	// (map[string]int) (len=1) {
	// 	(string) (len=3) "one": (int) 1
	// }
}
Example #2
0
func TestDumpSortedKeys(t *testing.T) {
	cfg := spew.ConfigState{SortKeys: true}
	s := cfg.Sdump(map[int]string{1: "1", 3: "3", 2: "2"})
	expected := "(map[int]string) (len=3) {\n(int) 1: (string) (len=1) " +
		"\"1\",\n(int) 2: (string) (len=1) \"2\",\n(int) 3: (string) " +
		"(len=1) \"3\"\n" +
		"}\n"
	if s != expected {
		t.Errorf("Sorted keys mismatch:\n  %v %v", s, expected)
	}

	s = cfg.Sdump(map[stringer]int{"1": 1, "3": 3, "2": 2})
	expected = "(map[spew_test.stringer]int) (len=3) {\n" +
		"(spew_test.stringer) (len=1) stringer 1: (int) 1,\n" +
		"(spew_test.stringer) (len=1) stringer 2: (int) 2,\n" +
		"(spew_test.stringer) (len=1) stringer 3: (int) 3\n" +
		"}\n"
	if s != expected {
		t.Errorf("Sorted keys mismatch:\n  %v %v", s, expected)
	}

	s = cfg.Sdump(map[pstringer]int{pstringer("1"): 1, pstringer("3"): 3, pstringer("2"): 2})
	expected = "(map[spew_test.pstringer]int) (len=3) {\n" +
		"(spew_test.pstringer) (len=1) stringer 1: (int) 1,\n" +
		"(spew_test.pstringer) (len=1) stringer 2: (int) 2,\n" +
		"(spew_test.pstringer) (len=1) stringer 3: (int) 3\n" +
		"}\n"
	if spew.UnsafeDisabled {
		expected = "(map[spew_test.pstringer]int) (len=3) {\n" +
			"(spew_test.pstringer) (len=1) \"1\": (int) 1,\n" +
			"(spew_test.pstringer) (len=1) \"2\": (int) 2,\n" +
			"(spew_test.pstringer) (len=1) \"3\": (int) 3\n" +
			"}\n"
	}
	if s != expected {
		t.Errorf("Sorted keys mismatch:\n  %v %v", s, expected)
	}

	s = cfg.Sdump(map[customError]int{customError(1): 1, customError(3): 3, customError(2): 2})
	expected = "(map[spew_test.customError]int) (len=3) {\n" +
		"(spew_test.customError) error: 1: (int) 1,\n" +
		"(spew_test.customError) error: 2: (int) 2,\n" +
		"(spew_test.customError) error: 3: (int) 3\n" +
		"}\n"
	if s != expected {
		t.Errorf("Sorted keys mismatch:\n  %v %v", s, expected)
	}

}
Example #3
0
// This example demonstrates how to use ConfigState.Dump to dump variables to
// stdout
func ExampleConfigState_Dump() {
	// See the top-level Dump example for details on the types used in this
	// example.

	// Create two ConfigState instances with different indentation.
	scs := spew.ConfigState{Indent: "\t"}
	scs2 := spew.ConfigState{Indent: " "}

	// Setup some sample data structures for the example.
	bar := Bar{uintptr(0)}
	s1 := Foo{bar, map[interface{}]interface{}{"one": true}}

	// Dump using the ConfigState instances.
	scs.Dump(s1)
	scs2.Dump(s1)

	// Output:
	// (spew_test.Foo) {
	// 	unexportedField: (spew_test.Bar) {
	// 		data: (uintptr) <nil>
	// 	},
	// 	ExportedField: (map[interface {}]interface {}) (len=1) {
	//		(string) (len=3) "one": (bool) true
	// 	}
	// }
	// (spew_test.Foo) {
	//  unexportedField: (spew_test.Bar) {
	//   data: (uintptr) <nil>
	//  },
	//  ExportedField: (map[interface {}]interface {}) (len=1) {
	//   (string) (len=3) "one": (bool) true
	//  }
	// }
	//
}