WIP: Increase Clip test coverage #592
Conversation
|
|
||
| return float(time.GetLength()) / fps; | ||
| if (!reader) { | ||
| // Throw error if reader not initialized |
ferdnyc
Nov 23, 2020
Author
Collaborator
Suggested change
// Throw error if reader not initialized
Most of these offensively-redundant comments, I took out if I was touching the code around them, but I left this one in for some reason. Does someone think we can't read the text directly below it!?
| // Throw error if reader not initialized |
Most of these offensively-redundant comments, I took out if I was touching the code around them, but I left this one in for some reason. Does someone think we can't read the text directly below it!?
| // Cache frame | ||
| cache.Add(frame); | ||
|
|
||
| // Return processed 'frame' | ||
| return frame; |
ferdnyc
Nov 23, 2020
Author
Collaborator
Suggested change
// Cache frame
cache.Add(frame);
// Return processed 'frame'
return frame;
cache.Add(frame);
return frame;
No idea why I left these, either.
| // Cache frame | |
| cache.Add(frame); | |
| // Return processed 'frame' | |
| return frame; | |
| cache.Add(frame); | |
| return frame; |
No idea why I left these, either.
...The wrong thing, since it now strikes me that the ...Wait, nope, never mind. I was right the first time, since the comment before the return says "use the duration (as detected by the reader)", so clearly there has to be a reader. Hah! But the new version of the function does break several of the existing tests, which are clearly assuming that they can get |
Codecov Report
@@ Coverage Diff @@
## develop #592 +/- ##
===========================================
+ Coverage 52.25% 53.43% +1.18%
===========================================
Files 129 129
Lines 10772 10831 +59
===========================================
+ Hits 5629 5788 +159
+ Misses 5143 5043 -100
Continue to review full report at Codecov.
|
|
Quit being a pushy jerk, Codecov, I'm working on it! (Sheesh! |
|
Merge conflicts have been detected on this PR, please resolve. |
In case anyone was doubting the value of unit tests, I've found three bugs in the Clip code so far.
1: Nullability error
The first, I found while writing tests to cover more failure cases (exceptions thrown, etc.). I added a set of tests, and every time they tried to run, the test program crashed before completion. Turned out, it was impossible to null a clip's Reader using the setter:
The third line would crash with an unhandled exception, because the code for
Reader(openshot::ReaderBase* new_reader)tried to make calls to the reader immediately after setting the member variable to the new pointer value, without first checking whetherreaderwas valid. Fixed.2: Logic error
The second one, I found just by reading the code on Codecov's website, because it's in the only one of four conditional paths that we don't currently have a test for:
Do you see it? The last condition should be
As a result, it was impossible to use auto-enabled video on a video file with no audio, or the video would auto-disable itself. Fixed.
3: Confusing order of operations
The third one isn't really a bug per se, but it cleans up a very confusing logical flow in what should be a simple function. The code for
Clip::Endused to read:Setting aside the unnecessary
elseconditions that can be streamlined with early returns, the innermost if/else also has athrowin theelse, which means it'll never get to the code after the else when theifcondition is false. Which in turn means the condition doesn't need to be there at all, thethrowserves as an early return.All in all, after spinning everything the right way around,
Clip::Endis now reduced to:(This third one actually has nothing to do with unit tests, it's just something I spotted while I was in the code. One nice thing about writing tests is that it makes you read the code, instead of write it, which is a much better way of evaluating it objectively.)
I also used early returns to unspool a bunch of other functions that looked like this:
I'm starting to develop this weird theory that 16:9 aspect screens are causing programmers to be more cavalier about deeply nested functions, because their screens have more width than height at this point. It certainly seem like the art of the early return, and the preference for many separate, succinct functions over functions containing long chains of conditional logic, have been falling by the wayside in recent years.