six2six / fixture-factory

Generator fake objects from a template
Apache License 2.0
445 stars 88 forks source link

Suportar listas de enum #9

Closed bbcbruno closed 11 years ago

bbcbruno commented 12 years ago

Alteração para suportar listas de enums conforme issue #8.

nykolaslima commented 11 years ago

@BrunoBCampos Before you change the implementation, please merge with master because we had a lot of changes to support immutable objects.

nykolaslima commented 11 years ago

@aparra @ahirata @BrunoBCampos I was thinking about this implementation, this is actually a random from a Enum. Isn't better to implement a random function that receives a quantity param and a Enum class?

Fixture.of(Empresa.class).addTemplate("valid", new Rule() {{
        add("modalidades", random(Enum.class, 2))
}});
aparra commented 11 years ago

I think this is better DSL:

Fixture.of(Empresa.class).addTemplate("valid", new Rule() {{ add("modalidades", has(2).of(Enum.class)); }});

What do you think?

nykolaslima commented 11 years ago

I was talking with @ahirata another day about this. The use of has function make it look better.

Maybe we can receive an additional varagrs with the Enum values that we want.

add("modalidades", has(2).of(Enum.class)); //return 2 random enum values
add("modalidades", has(2).of(Enum.class, Enum.VALID)); //return 2 Enum.VALID
add("modalidades", has(2).of(Enum.class, Enum.VALID, Enum.INVALID)); //return 2 Enum.VALID and Enum.INVALID
add("modalidades", has(3).of(Enum.class, Enum.VALID, Enum.INVALID)); //throw IllegalArgumentException because the quantity and enum values quantity are not the same 
aparra commented 11 years ago

I believe that function add("property", has(2).of(Enum.class)) will be more used. The other cases are unusual. Initially we´ll implement the first case. Then, if necessary, we´ll improve it.

What do you think?

bbcbruno commented 11 years ago

Just like @aparra, i think add("property", has(2).of(Enum.class)) is better. All of other cases are necessary in too specific situations.

nykolaslima commented 11 years ago

Ok.

@BrunoBCampos will you make this?

bbcbruno commented 11 years ago

@nykolaslima it is already done unless you do want to change something.

nykolaslima commented 11 years ago

@BrunoBCampos Did you make the changes that I suggest in the previous comment?

And merge your branch with master because we got a lot of changes.

bbcbruno commented 11 years ago

@nykolaslima I will merge my branch with master but like I already said to you before, is not possible to change just like you said because FixtureFunction and ObjectFactory are not in the same package, so I can't use a protected method.

Any sugestion?

nykolaslima commented 11 years ago

@BrunoBCampos maybe we can create a "gimme" method that only receives a quantity parameter and throws an IllegalArgumentException if the template class isn't Enum.

public List gimme(Integer quantity); {
  if(!templateHolder.getClazz().isEnum()) {
    throw new IllegalArgumentException("This method can only be used with Enum classes");
  }
  ...
}

What do you think @aparra and @BrunoBCampos ?

aparra commented 11 years ago

Something was lost along the way. We´ve talked about this feature:

Fixture.of(Empresa.class).addTemplate("valid", new Rule() {{ add("modalidades", has(2).of(Enum.class)); }});

This feature don't have side effects in ObjectFactory class, we only need to create a function that returns a List in RelationFunction.

The feature Fixture.from(Enum.class).gimme(xpto ...) is awkward.

bbcbruno commented 11 years ago

Ok, i will solve this issue with other solution. Please close this PR.