How can I avoid over-testing with example - unit-testing

I've been trying to start testing my code more, and I thought I would mimic the style of some of the tests that were autogenerated for by me rails scaffolding (I'm using rspec and rails, but my question is really just general-tdd). However, those methods were very basic, along the lines of do something. then if foo, do something else. I feel like as soon as you get more complex by adding one more 2 more things to condition in your if, things start spiraling and you get nested examples all over the place. Here's an example of what i've been doing that feels like overkill.
First the method
def maybe_blah(foo)
if foo != nil && foos.find_by_id(foo.id) != nil
blah unless bar?(foo)
end
end
this method is pretty simple, here's how I was planning on testing it
describe maybe_blah
shared_examples "what happens when bad input is passed" do
it "doesn't call bar?"...
it "doesn't call blah"...
end
context "when foo is not nil"
context "when foo is in foos"
context "bar? returns true"
it "doesn't call blah" do
some code involving mocking bar? and blah
end
end
context "bar? returns false"
it "does call blah" do
some code involving mocking bar? and blah
end
end
end
context "when foo is not in foos"
include_examples "what happens when bad input is passed"
end
end
context "when foo is nil"
include_examples "what happens when bad input is passed"
end
end
That's noticeably shorter than what the test would be if there was all of the setup and whatever else in there (testing maybe_blah for real like that took me 55 lines), so you can see how it seems to get out of hand. Is there a good way of testing a method that does feel like such overkill.
I don't see a way around having 3-deep nesting when you have 3 conditions you're testing (at least without repeating yourself even more), but it seems like you'd need to do that to make sure you're handling all different cases. Also, it seems dumb to test the fail result for every different bad input, but how else would you know you're actually failing on those bad inputs?
So is this just overkill?

Actually you condition is the same as:
def maybe_blah(foo)
if foo != nil && foos.find_by_id(foo.id) != nil && !bar?(foo)
blah
end
end
Thus you can extract it into separate method using Decompose Conditional and Consolidate Conditional Expression techniques:
def maybe_blah(foo)
blah if can_do_it?(foo)
end
def can_do_it?(foo)
foo != nil && foos.find_by_id(foo.id) != nil && !bar?(foo)
end
After that you can test this method in two contexts
describe '#maybe_blah' do
context 'when can do' do
# stub can_do_it? and returns true
# stould receive :blah
end
context 'when cant do' do
# stub can_do_it? and returns false
# should not receive :blah
end
end
And test condition separately.
And you can omit != nil
def can_do_it?(foo)
foo && foos.find_by_id(foo.id) && !bar?(foo)
end

Related

What is the idiomatic (and fast) way of treating the empty list/Seq as failure in a short-circuiting operation?

I have a situation where I am using functions to model rule applications, with each function returning the actions it would take when applied, or, if the rule cannot be applied, the empty list. I have a number of rules that I would like to try in sequence and short-circuit. In other languages I am used to, I would treat the empty sequence as false/None and chain them with orElse, like this:
def ruleOne(): Seq[Action] = ???
def ruleTwo(): Seq[Action] = ???
def ruleThree(): Seq[Action] = ???
def applyRules(): Seq[Action] = ruleOne().orElse(ruleTwo).orElse(ruleThree)
However, as I understand the situation, this will not work and will, in fact, do something other than what I expect.
I could use return which feels bad to me, or, even worse, nested if statements. if let would have been great here, but AFAICT Scala does not have that.
What is the idiomatic approach here?
You have different approaches here.
One of them is combining all the actions inside a Seq (so creating a Seq[Seq[Action]]) and then using find (it will return the first element that matches a given condition). So, for instance:
Seq(ruleOne, ruleTwo, ruleThree).find(_.nonEmpty).getOrElse(Seq.empty[Action])
I do not know clearly your domain application, but the last getOrElse allows to convert the Option produced by the find method in a Seq. This method though eval all the sequences (no short circuit).
Another approach consists in enriching Seq with a method that simulated your idea of orElse using pimp my library/extensions method:
implicit class RichSeq[T](left: Seq[T]) {
def or(right: => Seq[T]): Seq[T] = if(left.isEmpty) { right } else { left }
}
The by name parameter enable short circuit evaluation. Indeed, the right sequence is computed only if the left sequence is empty.
Scala 3 has a better syntax to this kind of abstraction:
extension[T](left: Seq[T]){
def or(rigth: => Seq[T]): Seq[T] = if(left.nonEmpty) { left } else { rigth }
}
In this way, you can call:
ruleOne or ruleTwo or ruleThree
Scastie for scala 2
Scastie for scala 3

Check if all elements of list are prime in Raku

my #g = (1,2,3,4);
say reduce {is-prime}, #g; # ==> gives error
say reduce {is-prime *}, #g; #==> gives error
say reduce {is-prime}, (1,2,3,4); # ==> gives error
say so is-prime #g.all; # ==> gives error
How to check if all elements of list are prime in Raku?
The answers above are all helpful, but they fail to explain why your solution does not work. Basically reduce is not going to apply a function (in your case, is-prime) to every member of a list. You want map for that. The error says
Calling is-prime() will never work with signature of the proto ($, *%)
Because reduce expects an infix, thus binary, function, or a function with two arguments; what it does is to apply them to the first pair of elements, then to the result and the third element, and so on. Last statement does not work for a similar reason: you are calling is-prime with a list argument, not a single argument.
You're basically asking: are there any elements in this list which are not prime? I would write that as:
say "not all prime" if #g.first: !*.is-prime;
Please note though, that apparently 1 is not considered prime according to the is-prime function:
say 1.is-prime; # False
so the first would trigger on the 1 in your example, not on the 4.
There are of course may ways to do this. A very explicit way is using a for loop:
for #g -> $g {
if $g.is-prime {
say $g;
}
}
Or with a grep (you could leave the $_ implicit):
#g.grep({ $_.is-prime }).say
Both above are assuming you really want to filter the primes out. Of course you can also really check each number and get a boolean:
#g.map({ .is-prime }).say
There is a big problem with this:
say reduce {is-prime}, #g;
You created a lambda:
{ }
The only thing it does is calls a function:
is-prime
You didn't give the function any arguments though.
Is it just supposed to guess what the arguments should be?
If you meant to pass in is-prime as a reference, you should have used &is-prime rather than {is-prime}.
Of course that still wouldn't have worked.
The other problem is that reduce operates by recursively combining values.
It can't do that if it operates on one argument at a time.
The bare block lambda {}, takes zero or one argument, not two or more.
reduce is often combined with map.
It happens so often that there is a Wikipedia page about MapReduce.
say ( map &is-prime, #g ==> reduce { $^a and $^b } );
# False
say ( map &is-prime, 2,3,5 ==> reduce { $^a and $^b } );
# True
I wrote it that way so that map would be in the line before reduce, but perhaps it would be more clear this way:
say reduce {$^a and $^b}, map &is-prime, 2,3,5;
# True
reduce with an infix operator is so common that there is a shorter way to write it.
say [and] map &is-prime, 2,3,5;
# True
Of course it would be better to just find the first value that isn't prime, and say the inverse.
Since if there is even a single value that isn't prime that would mean they can't all be primes.
You have to be careful though, as you may think something like this would always work:
not #g.first: !*.is-prime;
It does happen to work for the values you gave it, but may not always.
first returns Nil if it can't find the value.
not (2,3,5).first: !*.is-prime;
# not Nil === True
not (2,3,4).first: !*.is-prime;
# not 4 === False
not (2,3,0,4).first: !*.is-prime;
# not 0 === True
That last one returned 0 which when combined with not returns True.
You could fix this with defined.
not defined (2,3,0,4).first: !*.is-prime;
# False
This only works if first wouldn't return an undefined element that happens to be in the list.
(Int,Any).first: Real
# Int
defined (Int,Any).first: Real
# False
You could fix that by asking for the index instead of the value.
You of course still need defined.
(Int,Any).first: :k, Real
# 0
defined (Int,Any).first: :k, Real
# True
The other way to fix it is to just use grep.
not (2,3,0,4).grep: !*.is-prime;
# not (0,4) === False
Since grep always returns a List, you don't have to worry about checking for 0 or undefined elements.
(A List is True if it contains any elements, no matter what the values.)
grep is smart enough to know that if you coerce to Bool that it can stop upon finding the first value.
So it short-circuits the same as if you had used first.
This results in some fairly funky code, with those two negating operators. So it should be put into a function.
sub all-prime ( +#_ ) {
# return False if we find any non-prime
not #_.grep: !*.is-prime
# grep short-circuits in Bool context, so this will stop early
}
This could still fail if you give it something weird
all-prime 2,3,5, Date.today;
# ERROR: No such method 'is-prime' for invocant of type 'Date'
If you care, add some error handling.
sub all-prime ( +#_ ) {
# return Nil if there was an error
CATCH { default { return Nil }}
# return False if we find any non-prime
not #_.grep: !*.is-prime
}
all-prime 2,3,5, Date.today;
# Nil
use the all junction:
say so all #g».is-prime; # False

Eiffel: classical typecasting switch structure with attached and inspect

What is the best practice to do something such as
local
l_pet: ANIMAL
do
l_pet := catch_it_from_the_sky
inspect l_pet
when attached {DOG} l_pet as l_dog
l_dog.eat (meat)
when attached {FISH} l_pet as l_fish
l_fish.eat (plants)
else
io.put_string ("Strange animal how do I feed him???")
end
do
the compiler is complaining with the attached after when...
Update: why such a need?
because it just happened me to mess up with repeated copy-paste which is what a language tries to help avoiding. In the above case, the l_pet is written one time, with a N times if/else I'd have to write it as much times as ifs...
An inspect statement allows for checking if an expression has a specific value, and can be applied to expressions of integral types (such as INTEGER_64, CHARACTER_32 or NATURAL_8):
inspect age
when 6 .. 16 then ...
when 18 then ...
when 80, 90 then ...
...
end
For discriminating over object types, conditional instructions are used:
if attached {DOG} pet as dog then
dog.eat (meat)
elseif attached {FISH} pet as fish then
fish.eat (plants)
else
io.put_string ("Strange animal how do I feed him???")
end
In a multi-branch instruction
inspect exp when ... then ... else ... end
The exp expression needs to be a character or an integer expression.
In your given example I don't see the need to do that Object-Test,
but if you need to do something like that you need to use the conditional instruction.
if ... then
...
elseif ... then
...
else
...
end

Nested IF, INDEX and MATCH in Google Sheets

I'm trying to return a value in Google sheets.
This is done using an Index Match as follows, which does work:
=iferror(index(Data!B:B, match(B5339,Data!G:G,0)),"Not Found")
I'd now like to expand this, so that if this first test fails, try looking up that same data in another sheet....
=iferror(if(index(Data!B:B, match(B5340,Data!G:G,0),if(index(HeadOfficeLeads!B:B, match(B5340,HeadOfficeLeads!A:A,0))))),"Not found")
This outputs the fail msg of "Not Found".
However, although the first test is indeed false, the second test is true (this second data set does in fact hold a match).
NB - the data containing this correct match on the 2nd sheet is created by a UNIQUE ( FILTER, FWIW....
For some reason, it doesnt look like the second IF statement is being run - and the whole thing doesnt work, giving the error "Wrong number of arguments".
I have a feeling the argument issue is that the first test doesnt have an "if false" clause - but believe the "IFERROR" parent should handle this?
If not, where would I put the "if false clause" for the IF's?
You don't need any if, because iferror already contains an if statement in its logic (as its name suggests). Here is an example of nested iferror statements, simplified for clarity:
=iferror(match("a", A1:A5, 0), iferror(match("a", B1:B5", 0), "not found"))
This will return the position of "a" in column A, if it's there; otherwise, it will return its position in column B if it's there, otherwise it returns "not found".
Works the same with index or anything else around match function.

Passing a value in a where: block in Spock test

Is it possible to pass a value in where block like this.
I have tried this. But it fails and gives MissingPropertyException.
And I want the name1 and name2 to be inside the method.
def "length of names #name"() {
def name1 = "Spock"
def name2 = "Java"
expect:
name.size() == length
where:
name || length
name1 || 5
name2 || 2
}
Try this:
def "test length of names"() {
expect:
name.size() == length
where:
[name,length]<<getTestData()
}
def getTestData(){
[["Ram" ,3 ] ,["Test" ,4] ]
}
Hope that helps!!!
Thanks
Test data belongs in the where block, not hard coded in the test (feature) method.
One rough way to see this is to think of the test body (excluding the where block) as a method with some number of parameters -- 2 in your case, name and length. And then realize that the where clause just provides data values to the test runner to use when invoking your test method.
Spock uses Groovy magic to transform
def "test length of names"() {
expect:
name.size() == length
where:
name | length
"Spock" | 5
"Java" | 4
}
into something roughly like
def test_length_of_names(name, length) { // note the arguments
assert name.size() == length
}
and then tells the test runner to call the test once for each row in the where clause
test_length_of_names("Spock", 5)
test_length_of_names("Java", 4)
This approach
provides a nice separation of concerns between test logic and test data
allows relatively generic tests to be reused for a wide range of inputs and edge cases
avoids duplication in test code
supports a test design style that deserves the name data-driven testing.
This explanation leaves out a few details, such as creating an instance of the spec for each test, updating the name of each test invocation and calling the various setup and cleanup methods.
See the Spock documentation for more details.