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

Reflection Matchers #614

Closed
sksamuel opened this issue Jan 28, 2019 · 22 comments · Fixed by #894
Closed

Reflection Matchers #614

sksamuel opened this issue Jan 28, 2019 · 22 comments · Fixed by #894
Labels
enhancement ✨ Suggestions for adding new features or improving existing ones.
Milestone

Comments

@sksamuel
Copy link
Member

Would it be useful to have matchers like a.shouldHaveMethod("foo") and a.shouldHaveAnnotation("foo") and so on.

@sksamuel sksamuel added enhancement ✨ Suggestions for adding new features or improving existing ones. potential labels Jan 28, 2019
@LeoColman
Copy link
Member

These kind of things would be great if we could assert on the annotation too.

For example

@Table(name = "Foo")
class Klass {

}


Klass.shouldHaveAnnotation<Table>("Foo")  // Foo would be the first parameter. Annotation classes c an't be instantiated

@LeoColman
Copy link
Member

Or something similar

@sksamuel
Copy link
Member Author

klass.shouldHaveAnnotation<Foo>() { foo -> 
 ...
}

@LeoColman
Copy link
Member

That's way better

@LeoColman
Copy link
Member

One more matcher we would add a lambda to.

I don't think it's a big problem

@sksamuel
Copy link
Member Author

I think it could be a new pattern we could use to great effect.

Anything where we are testing a type, we allow the type to be used in the lambda.

a.shouldBeAnInstanceOf<Foo>() { foo -> }
a.shouldHaveType<Foo>() { foo -> }
aTry.shouldBeSuccess<Success>() { success -> }

@sksamuel sksamuel added this to the 3.3 milestone Jan 28, 2019
@LeoColman
Copy link
Member

But then, is should the right keyword here?

I suppose a more correct way would be to add an and matcher of some sort

a.shouldBeInstanceOf<Foo>().andShould { foo -> }

@sksamuel
Copy link
Member Author

What does the andShould give us over just accepting a lambda?

@LeoColman
Copy link
Member

I believe that shouldBeX looks like a "terminal" operation, and it doesn't seem to be a lambda operation.

But I don't mind

@sksamuel
Copy link
Member Author

sksamuel commented Jan 28, 2019 via email

@sksamuel sksamuel mentioned this issue Jan 30, 2019
20 tasks
@sksamuel sksamuel modified the milestones: 3.3, 3.4 Feb 5, 2019
@sksamuel sksamuel mentioned this issue Feb 16, 2019
88 tasks
@faogustavo
Copy link
Contributor

Does the development of this issue worth? Because annotations are not "business rules" to be validated on the test.

@sksamuel
Copy link
Member Author

sksamuel commented Jul 5, 2019 via email

@faogustavo
Copy link
Contributor

Okay. I can do this issue this weekend.

@sksamuel
Copy link
Member Author

sksamuel commented Jul 5, 2019 via email

@faogustavo
Copy link
Contributor

@sksamuel @Kerooker I already created these matchers:

Reflection
kclass.shouldHaveAnnotations() Asserts that the class has some annotation
kclass.shouldHaveAnnotations(n) Asserts that the class has exactly N annotation
kclass.shouldBeAnnotatedWith<T>() Asserts that the class is annotated with the given type
kclass.shouldBeAnnotatedWith<T> { block } Asserts that the class is annotated with the given type, and then, runs the block with the annotation
kclass.shouldHaveFunction(name) Asserts that the class have a function with the given name
kclass.shouldHaveFunction(name) { block } Asserts that the class have a function with the given name, and then, runs the block with the function
kfunction.shouldHaveAnnotations() Asserts that the function has some annotation
kfunction.shouldHaveAnnotations(n) Asserts that the function has exactly N annotation
kfunction.shouldBeAnnotatedWith<T>() Asserts that the function is annotated with the given type
kfunction.shouldBeAnnotatedWith<T> { block } Asserts that the function is annotated with the given type, and then, runs the block with the annotation
ktype.shouldBeOfType<T>() Asserts that the KType has the type T

Do you know others that can be useful?

@sksamuel
Copy link
Member Author

sksamuel commented Jul 7, 2019

A.shouldHaveMemberProperty.

Also the above that takes a lamba of KProperty to Unit

A.shouldHavePrimaryConstructor { constructor-> }

@sksamuel
Copy link
Member Author

sksamuel commented Jul 7, 2019

KFunction.shouldHaveReturnType

@faogustavo
Copy link
Contributor

faogustavo commented Jul 9, 2019

Okay, 'til now, I created the following matchers:

Reflection
kclass.shouldHaveAnnotations() Asserts that the class has some annotation
kclass.shouldHaveAnnotations(n) Asserts that the class has exactly N annotation
kclass.shouldBeAnnotatedWith<T>() Asserts that the class is annotated with the given type
kclass.shouldBeAnnotatedWith<T> { block } Asserts that the class is annotated with the given type, and then, runs the block with the annotation
kclass.shouldHaveFunction(name) Asserts that the class have a function with the given name
kclass.shouldHaveFunction(name) { block } Asserts that the class have a function with the given name, and then, runs the block with the function
kclass.shouldHaveMemberProperty(name) Asserts that the class have a member property with the given name
kclass.shouldHaveMemberProperty(name) { block } Asserts that the class have a member property with the given name, and then, runs the block with the function
kfunction.shouldHaveAnnotations() Asserts that the function has some annotation
kfunction.shouldHaveAnnotations(n) Asserts that the function has exactly N annotation
kfunction.shouldBeAnnotatedWith<T>() Asserts that the function is annotated with the given type
kfunction.shouldBeAnnotatedWith<T> { block } Asserts that the function is annotated with the given type, and then, runs the block with the annotation
kfunction.shouldHaveReturnType<T>() Asserts that the function returns the given type
kproperty.shouldBeOfType<T>() Asserts that the property is of the given type
kcallable.shouldHaveVisibility(visibility) Asserts that the member have the given visibility
kcallable.shouldBeFinal() Asserts that the member is final
ktype.shouldBeOfType<T>() Asserts that the KType has the type T

In my TODO List I still have those:

  • kclass.shouldBeSubtypeOf()
  • kclass.shouldBeData()
  • kclass.shouldBeSealed()
  • kclass.isCompanion()
  • kclass.shouldHavePrimaryConstructor()
  • kclass.shouldHaveVisibility(visibilty)
  • kfunction.shouldBeInline()
  • kfunction.shouldBeInfix()
  • kproperty.shouldBeConst()
  • kproperty.shouldBeLateInit()
  • kcallable.shouldBeOpen()
  • kcallable.shouldBeAbstract()
  • kcallable.shouldBeSuspend()
  • kcallable.shouldHaveParameters(listOf()) // Names of parameters
  • kcallable.shouldBeCalledWith(listOf()) // Types of parameter
  • kcallable.shouldHaveParameterWithName(name) // also accept lambda
  • kparameter.shouldBeOfType()
  • kparameter.shouldBeOptional()
  • kparameter.shouldBeVararg()
  • kparameter.shoudBeAtIndex(index) // check the position of the parameter

@sksamuel
Copy link
Member Author

sksamuel commented Jul 9, 2019 via email

@sksamuel
Copy link
Member Author

We can include this in 3.4 which we will release in the next few days, if it's ready @faogustavo

@sksamuel sksamuel mentioned this issue Jul 13, 2019
39 tasks
@faogustavo
Copy link
Contributor

We can include this in 3.4 which we will release in the next few days, if it's ready @faogustavo

I've been a little bit busy these last days. I'll open the PR to you start the review but there are a few methods missing.

@sksamuel
Copy link
Member Author

sksamuel commented Jul 16, 2019 via email

sksamuel pushed a commit that referenced this issue Jul 16, 2019
* added initial reflection matchers

* refactoring and split matchers into files

* added matchers for callable, function and property
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ✨ Suggestions for adding new features or improving existing ones.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants