Generators refactoring#601
Generators refactoring#601ErikSchierboom merged 97 commits intoexercism:masterfrom ErikSchierboom:generators-refactoring
Conversation
… to customize output
|
Finally, after much work, I'm done with my huge generators refactoring. It has become even bigger that the F# equivalent, and it contains the following improvements:
|
|
@ErikSchierboom I'm feeling brave... I'll take a look :) |
|
@robkeim Great! I'm really interested in what you think. |
robkeim
left a comment
There was a problem hiding this comment.
Awesome work @ErikSchierboom! Super cool stuff 👍
Added a few comments and reviewed through CollatatzConjecture so far.
| Additional methods added using this override will be added to the bottom of the test suite. | ||
| In some case, you might want to override the parameters that are used as input parameters. | ||
|
|
||
| An example of this is the [two fer]() generator, which does not use any input parameters when the `"name"` input parameter is set to `null`: |
|
|
||
| If a test method tests an instance method, you can also specify which parameters to use as constructor parameters (the others will be input parameters, unless specified otherwise). | ||
|
|
||
| An example of this is the [matrix]() generator, which specifies that the `"string"` parameter should be passed as a constructor parameter: |
| Some test methods want to verify that an exception is being thrown. | ||
|
|
||
| Here the **Assert** is being overridden. The assert needs to call additional functions, but only if the property is `consistency`. Otherwise, render the assert as usual. | ||
| An example of this is the [RNA transcription]() generator, which defines that some test methods should throws an `ArgumentException`: |
|
|
||
| This method allows you to customize the output of the test class. Only in rare cases would you want to override this method. The most common use case to override this method, is to add additional (helper) methods to the test suite. | ||
|
|
||
| An example of this is the [Tournament](https://github.com/exercism/csharp/blob/master/generators/Exercises/Tournament.cs) generator, which adds a helper method to the test suite: |
There was a problem hiding this comment.
Minor: tournament should be lowercase to be consistent with the other test names
| { | ||
| var inputBase = 2; | ||
| var digits = new int[0]; | ||
| var digits = Array.Empty<int>(); |
| { | ||
| var sut = new BowlingGame(); | ||
| var previousRolls = new [] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7 }; | ||
| var previousRolls = new[] |
There was a problem hiding this comment.
Why are these all on separate lines but the previous tests aren't? IMO it's probably better to have it on one line
There was a problem hiding this comment.
I guess there are a few other occurrences in the rest of this test file.
There was a problem hiding this comment.
Well, this is me trying to be clever. Many of the input arrays have a very limited number of elements (e.g. 1 or 2), in which case rendering them over multiple lines would be overkill IMHO. The code that does this check can be found here. It essentially checks if it is not an array of arrays and if the rendered single line output would be less than or equal to 72 characters. If so, it is output as a single line.
This is of course completely up for debate. I'd be happy to hear your thoughts on this.
There was a problem hiding this comment.
What I could do, is detect how many lines would end up being created. If there are more than a given number, say 8, then I could put multiple elements on a single line. Would that be an option?
There was a problem hiding this comment.
@robkeim I've given it some more thought, and I think I was attempting to be a bit too clever. I've removed the automatic multi-line rendering of single-dimensional arrays. Exercises can choose to opt-in themselves, should they want to do multi-line array initialization.
| new ValueTuple<int, int>(col+1, row), | ||
| new ValueTuple<int, int>(col, row+1) | ||
| (col, row - 1), | ||
| (col-1, row), |
There was a problem hiding this comment.
Super minor: missing spaces around the - here and the + below
| var expected = new string[] | ||
| { | ||
| "" | ||
| var minefield = new[] |
There was a problem hiding this comment.
Minor: the fact that each row is a separate line here vs in the expected it's on one line makes this definitely look like it wasn't handwritten.
| public void Find_primes_up_to_1000() | ||
| { | ||
| var expected = new[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 }; | ||
| var expected = new[] |
There was a problem hiding this comment.
This could probably be on one line or a few lines instead of one line per number
There was a problem hiding this comment.
See comment above.
|
haha, oh man.. where to start. Yeah, let me check this out locally and try to make sense of it all. Should hopefully have some time later in the week. |
|
Although I do think the commits are really useful to see the gradual evolution of the refactoring, I think that squashing this will be the way to go when merging this PR. |
| #### Customize test data | ||
|
|
||
| As an example, if you wanted to change the default behavior so that when the `Input` value of a test is a negative number, an exception should be thrown, the code would look like this. | ||
| There are quite some generators that want to change the input or expected value. |
There was a problem hiding this comment.
This sentence doesn't seem quite right
| As an example, if you wanted to change the default behavior so that when the `Input` value of a test is a negative number, an exception should be thrown, the code would look like this. | ||
| There are quite some generators that want to change the input or expected value. | ||
|
|
||
| An example of this is the [bracket-push](https://github.com/exercism/csharp/blob/master/generators/Exercises/Generators/BracketPush.cs) generator, which input value with the key `"value"` requires some additional escaping: |
There was a problem hiding this comment.
This sentence doesn't seem quite right
| Some test methods want to verify that an exception is being thrown. | ||
|
|
||
| Here the **Assert** is being overridden. The assert needs to call additional functions, but only if the property is `consistency`. Otherwise, render the assert as usual. | ||
| An example of this is the [rna-transcription](https://github.com/exercism/csharp/blob/master/generators/Exercises/Generators/RnaTranscription.cs) generator, which defines that some test methods should throws an `ArgumentException`: |
| #### Custom input/constructor parameters | ||
|
|
||
| Additional methods added using this override will be added to the bottom of the test suite. | ||
| In some case, you might want to override the parameters that are used as input parameters. |
| } | ||
| ``` | ||
|
|
||
| Note that as mentioned before, the namespace of any thrown exception types are automaticall added to the list of namespaces. |
|
That review destroyed by browser. |
|
@jpreese Thanks for the review. I've updated the docs. |
robkeim
left a comment
There was a problem hiding this comment.
Awesome job @ErikSchierboom!!! Super high quality code as always 👍
This is to test the newly enabled WIP plugin (see exercism/discussions#217 (comment)). And it will also give insight into how my refactoring attempt is progressing.