Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofixture's ".with()" syntax #3

Open
Pacane opened this issue May 22, 2014 · 2 comments
Open

Autofixture's ".with()" syntax #3

Pacane opened this issue May 22, 2014 · 2 comments

Comments

@Pacane
Copy link

Pacane commented May 22, 2014

Hey! I thought of another feature of .Net AutoFixture that I'm really missing...

I don't know if it would be possible to use something similar .with() feature, without the lambdas (or maybe with them, in Java8, I don't know)

in Java 8

.create().with(x -> x.propertyName, value)

and I don't know about Java 7...

what do you think?

@grzesiek-galezowski
Copy link
Owner

Hi,

Actually, I think it would require not only support for lambdas, but also for expressions. If it was for lambdas only, we could just use an anonymous class instead - this was the way to achieve "labmdaness" in Java prior to version 8.

So the reason this kind of syntax works in .NET is that the lambda is really accepted as an Expression<Action<...>> or Expression<Func<...>>, not as an Action<...> or Func<...>. When we have an expression that consists of a single call, we can extract information about this call (e.g. method name) from the expression (this is how Moq works as well, byt the way).

There could be another way of dealing with this, like creating a runtime proxy and recording methods called (but this would leave out fields, since they're not polymorphic, and final methods). I wonder whether the new method reference syntax (ClassName::MethodName) could prove useful. Anyway, even if this was possible, it would leave out Java 7. So for now, I have no idea whether this is fully achievable in Java. Sure, we could use strings for method names in the with() calls, but that would scare off any potential user :-D.

By the way, there is a workaround, but with less syntactic sugar. One can always write:

MyType instance = fixture.create(new InstanceOf<MyType>() {});
instance.propertyName = value;

Anyway, if you're hungry for the feature and know a way to extract method/field name from lambda expression or anonymous class (or any other kind of representation that would make sense), then please let me know.

@grzesiek-galezowski
Copy link
Owner

Also, another point I find troubling is that Java lacks properties that .NET has. So even if we had a way to determine method call from lambda or anonymous class instance, it would not look so fluent for private fields that only have getters and setters:

fixture.build(new InstanceOf<MyObject>() {}).with(o -> o.setY(12));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants