Exemplo n.º 1
0
// The actual collection must contain all expected elements and nothing else.
// The order of elements is not significant.
func ContainsExactly(actual_ interface{}, expected_ interface{}) (match bool, pos Message, neg Message, err os.Error) {
	actual, err := toArray(actual_)
	if err != nil {
		return
	}
	expected, err := toArray(expected_)
	if err != nil {
		return
	}

	containsAll := true
	remaining := new(vector.Vector)
	remaining.AppendVector((*vector.Vector)(&actual))
	for i := 0; i < len(expected); i++ {
		if idx, found := findIndex(*remaining, expected[i]); found {
			remaining.Delete(idx)
		} else {
			containsAll = false
			break
		}
	}

	match = containsAll && remaining.Len() == 0
	pos = Messagef(actual, "contains exactly “%v”", expected)
	neg = Messagef(actual, "does NOT contain exactly “%v”", expected)
	return
}
Exemplo n.º 2
0
func Parse(buff []byte, sig string, index int) (vec *vector.Vector, bufIdx int, err os.Error) {
	vec = new(vector.Vector)
	bufIdx = index
	for sigIdx := 0; sigIdx < len(sig); {
		switch sig[sigIdx] {
		case 'b': // bool
			bufIdx = _Align(4, bufIdx)
			b, e := _GetBoolean(buff, bufIdx)
			if e != nil {
				err = e
				return
			}
			vec.Push(b)
			bufIdx += 4
			sigIdx++

		case 'y': // byte
			v, e := _GetByte(buff, bufIdx)
			if e != nil {
				err = e
				return
			}
			vec.Push(v)
			bufIdx++
			sigIdx++

		case 'n': // int16
			bufIdx = _Align(2, bufIdx)
			n, e := _GetInt16(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			vec.Push(n)
			bufIdx += 2
			sigIdx++

		case 'q': // uint16
			bufIdx = _Align(2, bufIdx)
			q, e := _GetUint16(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			vec.Push(q)
			bufIdx += 2
			sigIdx++

		case 'u': // uint32
			bufIdx = _Align(4, bufIdx)

			u, e := _GetUint32(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			vec.Push(u)
			bufIdx += 4
			sigIdx++

		case 's', 'o': // string, object
			bufIdx = _Align(4, bufIdx)

			size, e := _GetInt32(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			str, e := _GetString(buff, bufIdx+4, int(size))
			if e != nil {
				err = e
				return
			}

			vec.Push(str)
			bufIdx += (4 + int(size) + 1)
			sigIdx++

		case 'g': // signature
			size, e := _GetByte(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			str, e := _GetString(buff, bufIdx+1, int(size))
			if e != nil {
				err = e
				return
			}
			vec.Push(str)
			bufIdx += (1 + int(size) + 1)
			sigIdx++

		case 'a': // array
			startIdx := _Align(4, bufIdx)
			arySize, e := _GetInt32(buff, startIdx)
			if e != nil {
				err = e
				return
			}

			sigBlock, e := _GetSigBlock(sig, sigIdx+1)
			if e != nil {
				err = e
				return
			}

			aryIdx := startIdx + 4
			aryVec := new(vector.Vector)
			for aryIdx < (startIdx+4)+int(arySize) {
				retvec, retidx, e := Parse(buff, sigBlock, aryIdx)
				if e != nil {
					err = e
					return
				}

				aryVec.AppendVector(retvec)
				aryIdx = retidx
			}
			bufIdx = aryIdx
			sigIdx += (1 + len(sigBlock))
			vec.Push(aryVec)

		case '(': // struct
			idx := _Align(8, bufIdx)
			stSig, e := _GetStructSig(sig, sigIdx)
			if e != nil {
				err = e
				return
			}

			retvec, retidx, e := Parse(buff, stSig, idx)
			if e != nil {
				err = e
				return
			}

			bufIdx = retidx
			sigIdx += (len(stSig) + 2)
			vec.Push(retvec)

		case '{': // dict
			idx := _Align(8, bufIdx)
			stSig, e := _GetDictSig(sig, sigIdx)
			if e != nil {
				err = e
				return
			}

			retvec, retidx, e := Parse(buff, stSig, idx)
			if e != nil {
				err = e
				return
			}

			bufIdx = retidx
			sigIdx += (len(stSig) + 2)
			vec.Push(retvec)

		case 'v': // variant
			val, idx, e := _GetVariant(buff, bufIdx)
			if e != nil {
				err = e
				return
			}

			bufIdx = idx
			sigIdx++
			vec.AppendVector(val)

		default:
			fmt.Println(sig[sigIdx])
			return nil, index, os.NewError("unknown type")
		}
	}
	return
}
Exemplo n.º 3
0
//append a sequence to a vector
func AppendToVector(vec *vector.Vector, s Seq) {
	switch arg := s.(type) {
	case *SequentialSeq: vec.AppendVector((*vector.Vector)(arg))
	default: Do(s, func(el El){vec.Push(el)})
	}
}