|
| 1 | +# Generator |
| 2 | + |
| 3 | +Last Updated: 2024/11/9 |
| 4 | + |
| 5 | +The generator is a powerful tool that can be used to generate tests for exercises based on the canonical data. |
| 6 | +The generator is written in Ruby and is located in the `bin` directory. |
| 7 | + |
| 8 | +## How to use the generator |
| 9 | + |
| 10 | +### Things to do before running the generator |
| 11 | + |
| 12 | +Run `bundle install` to install the required libraries. |
| 13 | +Before running the generator you have to make sure a couple of files are in place. |
| 14 | + |
| 15 | +1. `tests.toml` file |
| 16 | + |
| 17 | +It is located under the `.meta` folder for each exercise. |
| 18 | +The toml file is used to configure which exercises are generated and which are not. |
| 19 | +Since the generator grabs all the data from the canonical data, so does this enable new tests that won't automatically be merged in. |
| 20 | +Instead so does new tests have to be added to the toml file before they show up in the test file. |
| 21 | + |
| 22 | +If there is a test that isn't needed or something that doesn't fit Ruby you can remove it from the toml file. |
| 23 | +By writing after the test name `include = false` and it will be skipped when generating the test file. |
| 24 | + |
| 25 | +2. `config.json` file, located in the root of the track |
| 26 | + |
| 27 | +The generator makes sure that the exercise is in the config.json so you need to add it there before running the generator. |
| 28 | + |
| 29 | +**NOTE:** |
| 30 | +You are **NOT** allowed to write `include = false` more than once after each UUID. |
| 31 | +Since that can lead to errors in the generator. |
| 32 | + |
| 33 | +Bad way: |
| 34 | + |
| 35 | +```toml |
| 36 | +[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4] |
| 37 | +description = "basic" |
| 38 | +include = false |
| 39 | +include = false |
| 40 | +``` |
| 41 | + |
| 42 | +Good way: |
| 43 | + |
| 44 | +```toml |
| 45 | +[1e22cceb-c5e4-4562-9afe-aef07ad1eaf4] |
| 46 | +description = "basic" |
| 47 | +include = false |
| 48 | +``` |
| 49 | + |
| 50 | +### Template |
| 51 | + |
| 52 | +The generator uses a template file to generate the test file. |
| 53 | +The template is located under the `.meta` for each exercise. |
| 54 | + |
| 55 | +This template has to be manually written for each exercise. |
| 56 | +The goal is to make it so that you only have to write the template once and then it will be able to be used to generate new tests. |
| 57 | + |
| 58 | +The template file is written in [Embedded Ruby(ERB)][erb]. |
| 59 | +ERB enables you to write Ruby code inside of the template file. |
| 60 | +It also means that the templates can be highly customizable since you can write any Ruby code you want. |
| 61 | + |
| 62 | +When writing the template file, it is recommended to look at already existing template files to get a better understanding of how it works. |
| 63 | +The template is getting a slightly modified version of the canonical data, so you can check out the [canonical data][canonical data] to see the data structure. |
| 64 | +The modification is that the cases which are not included in the toml file will be removed from the data structure. |
| 65 | + |
| 66 | +When writing the template so is it a special tool that can help with giving `# skip` and `skip` tags for tests. |
| 67 | +You simply have to call the `skip?` method. |
| 68 | +It will return either `# skip` or `skip` depending on if it is the first test case or not. |
| 69 | + |
| 70 | +Here is an example: |
| 71 | + |
| 72 | +``` |
| 73 | +<%= skip? %> |
| 74 | +<%= skip? %> |
| 75 | +<%= skip? %> |
| 76 | +``` |
| 77 | + |
| 78 | +result: |
| 79 | + |
| 80 | +``` |
| 81 | +# skip |
| 82 | +skip |
| 83 | +skip |
| 84 | +``` |
| 85 | + |
| 86 | +### The Test Generator |
| 87 | + |
| 88 | +If all the earlier steps are done you run the generator. |
| 89 | +To run the generator you need to have a working Ruby installation with the gems installed, via `bundle install`. |
| 90 | +The generator is located in the `bin` directory and is called `generator`. |
| 91 | + |
| 92 | +To run the generator so do you have to be in the root directory and run the following command: |
| 93 | + |
| 94 | +```shell |
| 95 | +bundle exec ./bin/generate -e <exercise-name> |
| 96 | +``` |
| 97 | + |
| 98 | +Where `<exercise-name>` is the same name as the exercise has in its directory. |
| 99 | + |
| 100 | +For more commands and options, you can see this by running the command: |
| 101 | + |
| 102 | +```shell |
| 103 | +bundle exec ./bin/generate --help |
| 104 | +``` |
| 105 | + |
| 106 | +### Errors and warnings |
| 107 | + |
| 108 | +The generator will give you errors and warnings if something is wrong. |
| 109 | +That includes if the exercise is not in the `config.json` file, if the exercise is not in the toml file, or if the template file is missing. |
| 110 | +It will also report an error if it can not read the `canonical-data.json` file. |
| 111 | +The generator will check that the generated file is formatted correctly, reporting an error if there is a problem. |
| 112 | +The file will still be generated even if the formatter reports errors, So that you can check the file and see what is wrong and fix it in the template. |
| 113 | + |
| 114 | +[erb]: https://docs.ruby-lang.org/en/master/ERB.html |
| 115 | +[canonical data]: https://github.com/exercism/problem-specifications |
0 commit comments