Exemplo n.º 1
0
func Array(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.(btesting.Array)
	if !ok {
		e.T().Errorf("Unknown type (expecting array)")
		return
	}
	actualObj, ok := actual.([]interface{})
	if !ok {
		e.T().Errorf("Expecting to have a []interface{} but got %T, value: %v",
			actual, actual)
		return
	}

	expectingLen := len(obj)
	actualLen := len(actualObj)
	if expectingLen != actualLen {
		e.T().Errorf("Expecting both arrays to have equal length. But expecting has a length "+
			"of %v, actual has a length of %v.",
			expectingLen, actualLen)
		return
	}

	for index, v := range obj {
		e.Evaluate(v, actualObj[index])
		if e.T().Failed() {
			return
		}
	}
}
Exemplo n.º 2
0
func Eval(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	expFunction, ok := expecting.(btesting.EvalFunction)
	if !ok {
		e.T().Errorf("Unknown type")
		return
	}
	expFunction(e, actual)
}
Exemplo n.º 3
0
func Default(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	expectingString := fmt.Sprintf("%v", expecting)
	actualString := fmt.Sprintf("%v", actual)

	if expectingString != actualString {
		e.T().Errorf("Expecting to have something that gives string %v "+
			"but got something that gives string %v (Default Handler)",
			expectingString, actualString)
		return
	}
}
Exemplo n.º 4
0
func Object(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.(btesting.Object)
	if !ok {
		e.T().Errorf("Unknown type")
		return
	}
	actualObj, ok := actual.(map[string]interface{})
	if !ok {
		e.T().Errorf("Expecting to have a map[string]interface{} but got %T, value: %v",
			actual, actual)
		return
	}

	for k, v := range obj {
		actualValue := actualObj[k]
		e.Evaluate(v, actualValue)
		if e.T().Failed() {
			return
		}
	}
}
Exemplo n.º 5
0
func String(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.(string)
	if !ok {
		e.T().Errorf("Unknown type")
		return
	}
	actualObj, ok := actual.(string)
	if !ok {
		e.T().Errorf("Expecting to have a string but got %T, value: %v",
			actual, actual)
		return
	}

	if actualObj != obj {
		e.T().Errorf("Expecting string %v but got string %v", obj, actualObj)
	}
}
Exemplo n.º 6
0
func ByteSlice(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.([]byte)
	if !ok {
		e.T().Errorf("Unknown type, need []byte")
		return
	}
	actualObj, ok := actual.([]byte)
	if !ok {
		e.T().Errorf("Expecting to have a []byte but got %T, value: %v",
			actual, actual)
		return
	}
	if bytes.Compare(actualObj, obj) != 0 {
		e.T().Errorf("Given bytes %v do not match bytes %v", obj, actualObj)
		return
	}
}
Exemplo n.º 7
0
func Integer(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	_, _, inputAsString, inputType := toUint64OrInt64(expecting)
	if inputType == intType_notIntegerintType {
		e.T().Errorf("Input is not an integer")
		return
	}

	if actual == nil {
		e.T().Errorf("Expecting to have a integer %v but have nil",
			expecting)
		return
	}

	actualAsString := fmt.Sprintf("%v", actual)

	// Use "to string to compare"
	if inputAsString != actualAsString {
		e.T().Errorf("Expecting to have integer %v but got integer %v",
			inputAsString, actualAsString)
		return
	}
}
Exemplo n.º 8
0
func Key(e btesting.Evaluator, expecting interface{}, actual interface{}) {
	obj, ok := expecting.(typing.Key)
	if !ok {
		e.T().Errorf("Unknown type, need typing.Key")
		return
	}

	switch actual := actual.(type) {
	case [][]byte:
		e.Evaluate(obj.ToBinary(), actual)
	case []string:
		e.Evaluate(obj.ToBase32Slice(), actual)
	case []interface{}:
		if len(actual) != len(obj) {
			e.T().Errorf("Expecting a key with %v elements but have only %v elements",
				len(obj), len(actual))
			return
		}
		for index, actualElement := range actual {
			switch actualElement := actualElement.(type) {
			case string:
				binaryElement, err := typing.Base32Decode(actualElement)
				if err != nil {
					e.T().Errorf("Error decoding key element: %v", err)
					return
				}
				e.Evaluate(obj[index], binaryElement)
				if e.T().Failed() {
					return
				}
			case []byte:
				e.Evaluate(obj[index], actualElement)
				if e.T().Failed() {
					return
				}
			default:
				e.T().Errorf("Unknown key element type (expect string or []byte): %T", actualElement)
				return
			}
		}
	default:
		e.T().Errorf("Unknown 'actual' type. Need [][]byte or []string, have: %T", actual)
	}
}