Exemplo n.º 1
0
func regexMatchGen(regex *syntax.Regexp) gopter.Gen {
	switch regex.Op {
	case syntax.OpLiteral:
		return Const(string(regex.Rune))
	case syntax.OpCharClass:
		gens := make([]gopter.Gen, 0, len(regex.Rune)/2)
		for i := 0; i+1 < len(regex.Rune); i += 2 {
			gens = append(gens, RuneRange(regex.Rune[i], regex.Rune[i+1]).Map(func(v interface{}) interface{} {
				return string(v.(rune))
			}))
		}
		return OneGenOf(gens...)
	case syntax.OpAnyChar:
		return Rune().Map(func(v interface{}) interface{} {
			return string(v.(rune))
		})
	case syntax.OpAnyCharNotNL:
		return RuneNoControl().Map(func(v interface{}) interface{} {
			return string(v.(rune))
		})
	case syntax.OpCapture:
		return regexMatchGen(regex.Sub[0])
	case syntax.OpStar:
		elementGen := regexMatchGen(regex.Sub[0])
		return SliceOf(elementGen).Map(func(v interface{}) interface{} {
			return strings.Join(v.([]string), "")
		})
	case syntax.OpPlus:
		elementGen := regexMatchGen(regex.Sub[0])
		return gopter.CombineGens(elementGen, SliceOf(elementGen)).Map(func(v interface{}) interface{} {
			vs := v.([]interface{})
			return vs[0].(string) + strings.Join(vs[1].([]string), "")
		})
	case syntax.OpQuest:
		elementGen := regexMatchGen(regex.Sub[0])
		return OneGenOf(Const(""), elementGen)
	case syntax.OpConcat:
		gens := make([]gopter.Gen, len(regex.Sub))
		for i, sub := range regex.Sub {
			gens[i] = regexMatchGen(sub)
		}
		return gopter.CombineGens(gens...).Map(func(v interface{}) interface{} {
			result := ""
			for _, str := range v.([]interface{}) {
				result += str.(string)
			}
			return result
		})
	case syntax.OpAlternate:
		gens := make([]gopter.Gen, len(regex.Sub))
		for i, sub := range regex.Sub {
			gens[i] = regexMatchGen(sub)
		}
		return OneGenOf(gens...)
	}
	return Const("")
}
Exemplo n.º 2
0
// Complex64 generate arbitrary complex64 numbers
func Complex64() gopter.Gen {
	return gopter.CombineGens(
		Float32(),
		Float32(),
	).Map(func(v interface{}) interface{} {
		values := v.([]interface{})
		return complex(values[0].(float32), values[1].(float32))
	}).WithShrinker(Complex64Shrinker)
}
Exemplo n.º 3
0
// Complex128Box generate complex128 numbers within a rectangle/box in the complex plane
func Complex128Box(min, max complex128) gopter.Gen {
	return gopter.CombineGens(
		Float64Range(real(min), real(max)),
		Float64Range(imag(min), imag(max)),
	).Map(func(v interface{}) interface{} {
		values := v.([]interface{})
		return complex(values[0].(float64), values[1].(float64))
	}).SuchThat(func(v interface{}) bool {
		c := v.(complex128)
		return real(c) >= real(min) && real(c) <= real(max) &&
			imag(c) >= imag(min) && imag(c) <= imag(max)
	}).WithShrinker(Complex128Shrinker)
}
Exemplo n.º 4
0
// Float32 generates arbitrary float32 numbers that do not contain NaN or Inf
func Float32() gopter.Gen {
	return gopter.CombineGens(
		Int32Range(0, 1),
		Int32Range(0, 0xfe),
		Int32Range(0, 0x7fffff),
	).Map(func(v interface{}) interface{} {
		values := v.([]interface{})
		sign := uint32(values[0].(int32))
		exponent := uint32(values[1].(int32))
		mantissa := uint32(values[2].(int32))

		return math.Float32frombits((sign << 31) | (exponent << 23) | mantissa)
	}).WithShrinker(Float32Shrinker)
}
Exemplo n.º 5
0
// Identifier generates an arbitrary identifier string
// Identitiers are supporsed to start with a lowercase letter and contain only
// letters and digits
func Identifier() gopter.Gen {
	return gopter.CombineGens(
		AlphaLowerChar(),
		SliceOf(AlphaNumChar()),
	).Map(func(v interface{}) interface{} {
		values := v.([]interface{})
		first := values[0].(rune)
		tail := values[1].([]rune)
		result := make([]rune, 0, len(tail)+1)
		return string(append(append(result, first), tail...))
	}).SuchThat(func(v interface{}) bool {
		str := v.(string)
		if len(str) < 1 || !unicode.IsLower(([]rune(str))[0]) {
			return false
		}
		for _, ch := range str {
			if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) {
				return false
			}
		}
		return true
	}).WithShrinker(StringShrinker)
}