Пример #1
0
// DisorderedSubset attempts to find all the given subsets in the list of actuals.
// Does not allow one actual to match more than one subset, be warray of the
// possibility of insufficiently specific subsets.
func DisorderedSubset(t Fataler, a, s interface{}, extra ...interface{}) {
	actuals := toInterfaceSlice(a)
	subsets := toInterfaceSlice(s)

	used := make([]bool, len(actuals))
	matches := 0
	for _, subset := range subsets {
		for i, actual := range actuals {
			if used[i] {
				continue
			}
			if subsetp.Check(subset, actual) {
				matches++
				used[i] = true
				break
			}
		}
	}
	if matches != len(subsets) {
		fatal(cond{
			Fataler:    t,
			Format:     "expected subsets not found:\nACTUAL:\n%s\nEXPECTED SUBSET\n%s",
			FormatArgs: []interface{}{spew.Sdump(actuals), tsdump(subsets)},
			Extra:      extra,
		})
	}
}
Пример #2
0
// Subset ensures actual matches subset.
func Subset(t Fataler, actual, subset interface{}, a ...interface{}) {
	if !subsetp.Check(subset, actual) {
		fatal(cond{
			Fataler:    t,
			Format:     "expected subset not found:\nACTUAL:\n%s\nEXPECTED SUBSET\n%s",
			FormatArgs: []interface{}{spew.Sdump(actual), tsdump(subset)},
			Extra:      a,
		})
	}
}
Пример #3
0
// DeepEqual ensures actual and expected are equal. It does so using
// reflect.DeepEqual.
func DeepEqual(t Fataler, actual, expected interface{}, a ...interface{}) {
	if !reflect.DeepEqual(actual, expected) {
		fatal(cond{
			Fataler:    t,
			Format:     "expected these to be equal:\nACTUAL:\n%s\nEXPECTED:\n%s",
			FormatArgs: []interface{}{spew.Sdump(actual), tsdump(expected)},
			Extra:      a,
		})
	}
}
Пример #4
0
// PanicDeepEqual ensures a panic occurs and the recovered value is DeepEqual
// to the expected value.
func PanicDeepEqual(t Fataler, expected interface{}, a ...interface{}) {
	if expected == nil {
		panic("can't pass nil to ensure.PanicDeepEqual")
	}
	actual := recover()
	if !reflect.DeepEqual(actual, expected) {
		fatal(cond{
			Fataler:           t,
			Format:            "expected these to be equal:\nACTUAL:\n%s\nEXPECTED:\n%s",
			FormatArgs:        []interface{}{spew.Sdump(actual), tsdump(expected)},
			Extra:             a,
			DisableDeleteSelf: true,
		})
	}
}
Пример #5
0
// TestSpew executes all of the tests described by spewTests.
func TestSpew(t *testing.T) {
	initSpewTests()

	t.Logf("Running %d tests", len(spewTests))
	for i, test := range spewTests {
		buf := new(bytes.Buffer)
		switch test.f {
		case fCSFdump:
			test.cs.Fdump(buf, test.in)

		case fCSFprint:
			test.cs.Fprint(buf, test.in)

		case fCSFprintf:
			test.cs.Fprintf(buf, test.format, test.in)

		case fCSFprintln:
			test.cs.Fprintln(buf, test.in)

		case fCSPrint:
			b, err := redirStdout(func() { test.cs.Print(test.in) })
			if err != nil {
				t.Errorf("%v #%d %v", test.f, i, err)
				continue
			}
			buf.Write(b)

		case fCSPrintln:
			b, err := redirStdout(func() { test.cs.Println(test.in) })
			if err != nil {
				t.Errorf("%v #%d %v", test.f, i, err)
				continue
			}
			buf.Write(b)

		case fCSSdump:
			str := test.cs.Sdump(test.in)
			buf.WriteString(str)

		case fCSSprint:
			str := test.cs.Sprint(test.in)
			buf.WriteString(str)

		case fCSSprintf:
			str := test.cs.Sprintf(test.format, test.in)
			buf.WriteString(str)

		case fCSSprintln:
			str := test.cs.Sprintln(test.in)
			buf.WriteString(str)

		case fCSErrorf:
			err := test.cs.Errorf(test.format, test.in)
			buf.WriteString(err.Error())

		case fCSNewFormatter:
			fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in))

		case fErrorf:
			err := spew.Errorf(test.format, test.in)
			buf.WriteString(err.Error())

		case fFprint:
			spew.Fprint(buf, test.in)

		case fFprintln:
			spew.Fprintln(buf, test.in)

		case fPrint:
			b, err := redirStdout(func() { spew.Print(test.in) })
			if err != nil {
				t.Errorf("%v #%d %v", test.f, i, err)
				continue
			}
			buf.Write(b)

		case fPrintln:
			b, err := redirStdout(func() { spew.Println(test.in) })
			if err != nil {
				t.Errorf("%v #%d %v", test.f, i, err)
				continue
			}
			buf.Write(b)

		case fSdump:
			str := spew.Sdump(test.in)
			buf.WriteString(str)

		case fSprint:
			str := spew.Sprint(test.in)
			buf.WriteString(str)

		case fSprintf:
			str := spew.Sprintf(test.format, test.in)
			buf.WriteString(str)

		case fSprintln:
			str := spew.Sprintln(test.in)
			buf.WriteString(str)

		default:
			t.Errorf("%v #%d unrecognized function", test.f, i)
			continue
		}
		s := buf.String()
		if test.want != s {
			t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want)
			continue
		}
	}
}
Пример #6
0
// tsdump is Sdump without the trailing newline.
func tsdump(a ...interface{}) string {
	return strings.TrimSpace(spew.Sdump(a...))
}