assertJ allows you to check for element-wise list equality while ignoring user specified fields like so
assertThat(listOfObjects)
.usingElementComparatorIgnoringFields("field1", "field2", ...)
.containsExactly(object1, object2, ...)
I have not been able to find any equivalent function in kotest. Is there an equivalent?
There is no such assertion at collection level, though you can perform similar assertion at individual object using shouldBeEqualToIgnoringFields matcher.
https://github.com/kotest/kotest/blob/1f4069d78faead65a0d7e8c7f1b689b417a655d2/kotest-assertions/kotest-assertions-core/src/jvmMain/kotlin/io/kotest/matchers/equality/reflection.kt#L127
https://kotest.io/docs/assertions/core-matchers.html
Related
How do I verify basic set operations such as intersect, union and difference in JML tool like OpenJML in which keywords like "\intersect \set_minus \set_union" are not supported?
The Java interfaces on which I want to do validation on looks like this:
MySetInterface<S> intersect (MySetInterface<S> set)
MySetInterface<S> union (MySetInterface<S> set)
MySetInterface<S> difference (MySetInterface<S> set)
You would have to find a functional description for these operations, as for all methods that don't have a direct correspondence in the JML-supported theories. The mathematical definition of the union of two sets, for example, is "the set of all objects that are members of either the one or the other set". I.e., the method union could be specified roughly as
/*# public normal_behavior
# ensures this.containsAll(\old(this)) &&
# this.containsAll(\old(set)); */
and similarly for the other methods, assuming that there is a pure containsAll method available. Otherwise, you could use quantified expressions and a pure contains method. If you don't have such pure query methods, you would have to expose implementation details like an underlying field, which would make less sense for specifying an interface.
Does this clarify the problem for you?
I'm trying to write a google-test utility function which performs some setup steps base on it's input arguments and finally compares the contents of two containers irrespective of order. I would then like to return a ::testing::AssertionResult from this function based on whether the container contents are equal.
For the comparison itself I would like to use functionalities provided by google-mock in order to avoid errors in my own test-code and reduce developement overhead. Something like the following would work:
EXPECT_THAT(actual_container,
testing::UnorderedElementsAreArray(expected_container))
<< "Container content equal.";
The trouble with this is that I'm not sure if it is even possible to
obtain an AssertionResult which can be returned from a function in a similar manner.
Does anyone have a solution to this or a suggestion for an alternative approach?
I need to verify I method call with a specific parameters
How can I do?
Im work in java with mockito, and use junit.
You can simply provide the parameters you require in the verify statement, assuming that the classes for those parameters have the equals method properly defined.
verify(myMock).myMethodCall(someParmValue1, someParmValue2);
If equals is not the criteria you wish to use, you can use Matchers on the arguments, but note that if you use a Matcher for any argument then you have to use matchers for all arguments. So as an example if you want to ensure that the arguments in your verify were actually the same instances as (object identity instead of equality), you could use the Matchers.same() Matcher:
verify(myMock).myMethodCall( same(someParmValue1), same(someParmValue2));
I am curious as to why it is possible, with the CriteriaBuilder class of JPA 2, to create such queries. Suppose I have a User class with a persisted String called name as attribute. Why can I write this?
CriteriaBuilder builder = mgr.getCriteriaBuilder();
CriteriaQuery<User> crit = builder.createQuery(User.class);
Root<User> user = crit.from(User.class); // 1
crit.select(user)
.where(builder.equal(user.get(User_.name), 2.5)); // 2
First, at Marker 1: Why must I indicate User.class again? Isn't my CriteriaQuery supposed to know I'm interested in users anyway? Doesn't it break type safety to possibly inject another class here?
Second, at Marker 2: The name property is a String. Why can I compile nonsense like this, comparing a String with a double? In other words, why is the signature of the called equal method this:
public Predicate equal(Expression<?> x, Object y)
instead of a presumably more type safe version as follows?
public <T> Predicate equal(Expression<T> x, T y)
Would other query frameworks like Querydsl provide a better solution to this issue?
I believe the typesafe aspects of the JPA 2 Criteria API were added at a quite late point of the specification process. That's why it doesn't feel consistent.
Querydsl is more concise than the JPA 2 Criteria API and also more typesafe. Querydsl uses fluent builders instead of a factory class for predicate creation, so the equivalent method can be found here http://www.querydsl.com/static/querydsl/2.8.0/apidocs/com/mysema/query/types/expr/SimpleExpression.html#eq%28T%29
I am the maintainer of Querydsl, so this answer is biased.
specific on JPA you can use also Object Query or Torpedo Query(but this is specialized on HQL) that not needs compile time model generation. anyway QueryDsl is one of the first to implement typesafe query
I have a bunch of F# functions that implement different algorithms for the same input, kind of like the Strategy pattern. To pick the right strategy, I want to pattern match on the input argument and return the function as a value :
let equalStrategy points : seq<double> =
...
let multiplyStrategy factor (points: seq<double>) =
...
let getStrategy relationship =
match relationship with
| "=" -> equalStrategy
| "*5" -> multiplyStrategy 5.0
| _ -> raise (new System.NotImplementedException(" relationship not handled"))
Now I want to write some unit tests to make sure that I return the right strategy, so I tried something like this in nUnit :
[<TestCase("=")>]
[<Test>]
member self.getEqualstrategy( relationship:string ) =
let strategy = getStrategy relationship
Assert.AreEqual( strategy, equalStrategy )
Now I think the code is correct and will do what I want, but the assertion fails because functions don't seem to have an equality operation defined on them. so my questions are :
(a) is there a way to compare 2 functions to see if they are the same, i.e. let isFoo bar = foo == bar, that I can use in an nUnit assertion?
or
(b) is there another unit testing framework that will do this assertion for me in F#?
Testing whether an F# function returned by your getStrategy is the same function as one of the funcions you defined is also essentially impossible.
To give some details - the F# compiler generates a class that inherits from FSharpFunc when you return a function as a value. More importantly, it generates a new class each time you create a function value, so you cannot compare the types of the classes.
The structure of the generated classes is something like this:
class getStrategy#7 : FSharpFunc<IEnumerable<double>, IEnumerable<double>> {
public override IEnumerable<double> Invoke(IEnumerable<double> points) {
// Calls the function that you're returning from 'getStrategy'
return Test.equalStrategy(points);
}
}
// Later - in the body of 'getStrategy':
return new getStrategy#7(); // Returns a new instance of the single-purpose class
In principle, you could use Reflection to look inside the Invoke method and find which function is called from there, but that's not going to be a reliable solution.
In practice - I think you should probably use some other simpler test to check whether the getStrategy function returned the right algorithm. If you run the returned strategy on a couple of sample inputs, that should be enough to verify that the returned algorithm is the right one and you won't be relying on implementation details (such as whether the getStrategy function just returns a named function or whether it returns a new lambda function with the same behaviour.
Alternatively, you could wrap functions in Func<_, _> delegates and use the same approach that would work in C#. However, I think that checking whether getStrategy returns a particular reference is a too detailed test that just restricts your implementation.
Functions doesn't have equality comparer:
You will have error: The type '('a -> 'a)' does not support the 'equality' constraint because it is a function type
There is a good post here
It would be very difficult for the F# compiler to prove formally that two functions always have the same output (given the same input). If that was possible, you could use F# to prove mathematical theorems quite trivially.
As the next best thing, for pure functions, you can verify that two functions have the same output for a large enough sample of different inputs. Tools like fscheck can help you automate this type of test. I have not used it, but I've used scalacheck that is based on the same idea (both are ports from Haskell's QuickCheck)