randx

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2023 License: BSD-2-Clause Imports: 11 Imported by: 0

README

randx

Provides methods for picking a random value that satisfy user-readable conditions.

$go install github.com/vienng/randx

Which value would you choose between the boundaries?

Beside boundaries, middle values are the most important inputs in Software Testing. The middle value is usually a valid value, we can pick any and hardcode in the test, if it works all remain work (assume).

Obviously, no tester ever thinks of testing all the possible values thus random one in each execution time would make sense and be popular. I do. But I get into inconvenience while generating random inputs for my test cases. That's why randx exists. Hopefully, this piece would become handy for people, especially testers.

What does randx offer?

Random a number

	generator :=  randx.NewXNumber(0, 1000, 1)
	x := generator.Random("number < 100 || number > 200")
	// outputs of 3 execution times: 2, 16, 553

Random a string slice

	generator :=  randx.NewXWord("etc/vietnamese")
	name1 := generator.Random("length == 4")
	name2 := generator.Random("begin == 'Nguyễn'")
	name3 := generator.Random("end == 'Vi'")
	// outputs:
	// name1 [Ngọc Phượng Hằng Thành]
	// name2 [Nguyễn Lý Phi]
	// name3 [Viên Thuận Vi]

Random a datetime

	generator :=  randx.NewXTime(time.Now().AddDate(-100, 0, 0), time.Now(), 24*time.Hour)
	birthDay := generator.Random("birthday > '1981-01-01' && birthday < '1996-12-31'") // millennials
	// output 1982-09-04

Random a string as regex

	generator :=  randx.NewXRegex()
	id := generator.Random("^[a-z]{6}[0-9]{2}$")
	// output yecrzi97

Random a user as your template

	// define your own template
	template := UserTemplate{
	Name:       NewXWords("etc/vietnamese"),
	Phone:      NewXRegex(), 
	Score:      NewXNumber(0, 1000, 1), 
	TaxPercent: NewXNumber(0, 1, 0.05), 
	DOB:        NewXTime(time.Now().AddDate(-100, 0, 0), time.Now(), 24*time.Hour),
	}
	template.Phone.SetFallback("^[+]84[0-9]{9}$")
	
	// customize your chain and try
	user1 := SomeOne(template)
	user2 := SomeOne(template).WithName("begin == 'Nguyễn'")
	user3 := SomeOne(template).
		WithTaxPercent("tax_fee > 0.1").
		WithScore("score > 100 && score < 300").
		WithName("length == 4").
		WithDOB(fmt.Sprintf("birthdate < '%s'", eighteenYearsAgo.Format(time.RFC3339)))
	// outputs:
	// user1 [Lý] +84004343908 187 0.04 1882-09-07
	// user2 [Nguyễn Trâm Thanh] +84025848714 531 0.92 2004-03-29
	// user3 [My Chúc Trần Đinh] +84688014056 247 0.90 1988-03-18

What should be noted?

TBD

Reference

Thanks to govaluate. randx based on a core of govaluate, the parsing expression. And thanks to goregen. randx.XRegex just be a wrapper of goregen functions.

License

This project is licensed under the BSD 2-Clause License. You're free to integrate, fork, and play with this code as you feel fit without consulting the author, as long as you provide proper credit to the author in your works.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type User

type User struct {
	Name       interface{}
	Phone      interface{}
	Score      interface{}
	TaxPercent interface{}
	DOB        interface{}
	// contains filtered or unexported fields
}

func SomeOne

func SomeOne(template UserTemplate) *User

func (*User) WithDOB

func (user *User) WithDOB(condition string) *User

func (*User) WithName

func (user *User) WithName(condition string) *User

func (*User) WithPhonePrefix

func (user *User) WithPhonePrefix(regex string) *User

func (*User) WithScore

func (user *User) WithScore(condition string) *User

func (*User) WithTaxPercent

func (user *User) WithTaxPercent(condition string) *User

type UserTemplate

type UserTemplate struct {
	Name       X
	Phone      X
	Score      X
	TaxPercent X
	DOB        X
}

func NewSampleTemplate

func NewSampleTemplate() UserTemplate

type X

type X interface {
	Random(expression string) interface{}
	BindOperator(expression string) XOP
	SetFallback(value interface{})
}

X is an interface providing the random method with user condition

func NewXNumber

func NewXNumber(min, max, step float64) X

NewXNumber creates a new instance for XNumber

func NewXRegex

func NewXRegex() X

NewXRegex makes a new instance for XRegex

func NewXTime

func NewXTime(min, max time.Time, step time.Duration) X

NewXTime makes a new instance for XTime

func NewXWords

func NewXWords(sourcePath string) X

NewXWords makes a new instance for XWords

type XNumber

type XNumber struct {
	// contains filtered or unexported fields
}

XNumber implements interface X, XNumber returns a random number satisfied inputted condition

func (XNumber) BindOperator

func (xn XNumber) BindOperator(expression string) XOP

BindOperator returns supported operator of XNumber

func (XNumber) Random

func (xn XNumber) Random(condition string) interface{}

Random generates a random number x that satisfies given conditions

func (*XNumber) SetFallback

func (xn *XNumber) SetFallback(value interface{})

type XOP

type XOP int

XOP defines operator of X

const (
	Unknown XOP = iota
	Any
	Invalid

	Length
	Begin
	End

	Constant
	FindX
	FindXs

	Regex
)

Represents all valid operator of X

func (XOP) String

func (op XOP) String() string

String method returns a string that describes the given XOP e.g., when passed the FindX XOP, this returns the string "FindX".

type XRegex

type XRegex struct {
	// contains filtered or unexported fields
}

XRegex implements interface X, random a string with inputted regex

func (XRegex) BindOperator

func (xr XRegex) BindOperator(regex string) XOP

BindOperator returns Regex XOP

func (XRegex) Random

func (xr XRegex) Random(regex string) interface{}

Random returns a random string generated from inputted regex

func (*XRegex) SetFallback

func (xr *XRegex) SetFallback(defaultRegex interface{})

type XTime

type XTime struct {
	// contains filtered or unexported fields
}

XTime implements interface X, XTime returns a random datetime satisfied inputted condition

func (XTime) BindOperator

func (xt XTime) BindOperator(expression string) XOP

BindOperator returns supported operator of XTime

func (XTime) Random

func (xt XTime) Random(expression string) interface{}

Random returns a random datetime satisfied inputted condition

func (*XTime) SetFallback

func (xt *XTime) SetFallback(value interface{})

type XWords

type XWords struct {
	// contains filtered or unexported fields
}

XWords implements interface X

func (XWords) BindOperator

func (xw XWords) BindOperator(expression string) XOP

BindOperator returns the detected operator of XWords in the input condition: "length (>=<) 4" "begin = 'Marry'" "end = 'Marry' "

func (XWords) Random

func (xw XWords) Random(xExpression string) interface{}

Random returns random words matching input condition

func (*XWords) SetFallback

func (xw *XWords) SetFallback(fallback interface{})

SetFallback sets the value to be returned when the random function troubled.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL