leanovate / gopter

GOlang Property TestER
MIT License
598 stars 40 forks source link

[]Struct with SuchThat clause within a Struct triggers index out of bounds exception on master #62

Closed anisjonischkeit closed 4 years ago

anisjonischkeit commented 4 years ago

when you have a generator that looks like this:

genStruct({
  a: genSlice(genStruct({
    b: genString().SuchThat(...SomeCondition),
    c: genString().SuchThat(...SomeOtherCondition)
  }))
})

An index out of bounds exception is thrown here: https://github.com/leanovate/gopter/blob/cdb2f0efc0017d6af9f9668577a4cffbbfe31b3b/derived_gen.go#L67

it happens because for some reason baseSieve only has a single value (SomeCondition) but ups has two values (b and c) in it. So when you loop over ups and grab the corresponding baseSieve item for the up (at i) you get that panic.

This doesn't happen on the latest release but does happen on master.

minimal example:

type TestBook struct {
    Title   string
    Content string
}

func genTestBook() gopter.Gen {
    return gen.Struct(reflect.TypeOf(&TestBook{}), map[string]gopter.Gen{
        "Title": gen.AlphaString().SuchThat(func(s string) bool {
            fmt.Println("NonEmptyString1")
            return s != ""
        }),
        "Content": gen.AlphaString().SuchThat(func(s string) bool {
            fmt.Println("NonEmptyString2")
            return s != ""
        }),
    })
}

type TestLibrary struct {
    Books []TestBook
}

func genTestLibrary() gopter.Gen {
    return gen.Struct(reflect.TypeOf(&TestLibrary{}), map[string]gopter.Gen{
        "Books": gen.SliceOf(genTestBook()),
    })
}

This should probably block #60

untoldwind commented 4 years ago

Good find ... there indeed has been some refactoring for the struct derive to be unified with the more generic BiMapper.

... and as usual it was just a single line

Please re-check with the current version, before I create a new release.

anisjonischkeit commented 4 years ago

that looks to have fixed it, thank you very much!