Skip to content

Commit f8df0ce

Browse files
authored
update CONTRIBUTING.md, add doc for the generator (exercism#153)
1 parent a29e182 commit f8df0ce

File tree

3 files changed

+181
-29
lines changed

3 files changed

+181
-29
lines changed

CONTRIBUTING.md

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
11
# How to contribute to the Exercism Lean track
22

3-
#### **Do you want to add an exercise?**
3+
## Contributing
44

5-
- **Ensure that someone else isn't already adding it** by searching the [forum](https://forum.exercism.org/c/programming/lean) and the repository's [issues](https://github.com/exercism/lean/issues) and [pull requests](https://github.com/exercism/lean/pulls).
5+
We 💙 our community, but **this repository does not accept unsolicited pull requests at this time**.
66

7-
- If nobody is yet adding the exercise, [open a conversation](https://forum.exercism.org/c/programming/lean) and indicate you'd like to add the exercise.
7+
Please read this [community blog post][guidelines] for details.
88

9-
- Follow the [Add a Practice Exercise docs](https://exercism.org/docs/building/tracks/practice-exercises/add).
9+
### How to contribute
1010

11-
#### **Do you want to report a bug?**
11+
1. Open a topic on the [Lean forum][lean-forum]
12+
2. Discuss the proposal with the maintainers
13+
3. After receiving the go-ahead, submit a pull request
1214

13-
- **Ensure the bug was not already reported** by searching the [forum](https://forum.exercism.org/c/programming/lean).
15+
Pull requests must follow [Exercism's style guide][style].
1416

15-
- If you're unable to find an open conversation addressing the problem, [open a new one](https://forum.exercism.org/new-topic?category=lean). Be sure to include a **title and clear description**, as much relevant information as possible, and (when possible) a **code sample**.
17+
Before submitting, please read:
1618

17-
#### **Do you want to fix a bug?**
19+
- [Contributors Pull Request Guide][contributors-pr-guide]
20+
- [Pull Request Guide][pr-guide]
1821

19-
- **Ensure that the bug is [reported](#do-you-want-to-report-a-bug)**.
20-
Only start fixing the bug when there is agreement on whether (and how) it should be fixed.
22+
When opening a PR:
2123

22-
- Fix the bug and [submit a Pull Request](https://exercism.org/docs/building/github/contributors-pull-request-guide) to this repository.
24+
- Clearly describe the problem and the solution
25+
- Link to the corresponding forum discussion
26+
- Add a link to the PR in that same discussion
2327

24-
- Ensure the PR description clearly describes the problem and solution.
25-
Include a link to the bug's corresponding forum conversation.
28+
If the PR touches an existing exercise, please also consider [this warning][unnecessary-test-runs].
2629

27-
- Before submitting, please read the [Contributors Pull Request Guide](https://exercism.org/docs/building/github/contributors-pull-request-guide) and [Pull Request Guide](https://exercism.org/docs/community/being-a-good-community-member/pull-requests).
30+
### Adding an exercise
2831

29-
#### **Do you intend to add a new feature or change an existing one?**
32+
Practice exercises should follow the [Add a Practice Exercise docs][add-exercise].
3033

31-
- **Ensure that the feature or change is discussed on the [forum](https://forum.exercism.org/c/programming/lean).**
32-
Only start adding the feature or change when there is agreement on whether (and how) it should be added or changed.
34+
All exercises must include a test generator located in:
3335

34-
- Add the feature or change and [submit a Pull Request](https://exercism.org/docs/building/github/contributors-pull-request-guide) to this repository.
36+
```text
37+
generator/Generator/Generator
38+
```
3539

36-
- Ensure the PR description clearly describes the problem and solution.
37-
Include a link to the bug's corresponding forum conversation.
40+
The generator must:
3841

39-
- Before submitting, please read the [Contributors Pull Request Guide](https://exercism.org/docs/building/github/contributors-pull-request-guide) and [Pull Request Guide](https://exercism.org/docs/community/being-a-good-community-member/pull-requests).
42+
- be imported by `generator/Generator/Generator.lean`
43+
- define the required generator functions
44+
- register them in the `dispatch` table
45+
46+
The Lean track provides a generator script to help with this process.
47+
48+
See the [generator documentation][generator-doc].
49+
50+
[guidelines]: https://exercism.org/blog/contribution-guidelines-nov-2023
51+
[lean-forum]: https://forum.exercism.org/c/programming/lean/761
52+
[style]: https://exercism.org/docs/building/markdown/style-guide
53+
[contributors-pr-guide]: https://exercism.org/docs/building/github/contributors-pull-request-guide
54+
[pr-guide]: https://exercism.org/docs/community/being-a-good-community-member/pull-requests
55+
[unnecessary-test-runs]: https://exercism.org/docs/building/tracks#h-avoiding-triggering-unnecessary-test-runs
56+
[add-exercise]: https://exercism.org/docs/building/tracks/practice-exercises/add
57+
[generator-doc]: generators/GenerateTestFile.md

generators/GenerateTestFile.lean

Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,74 @@ import Generator
55
open Lean
66
open Std
77

8+
/-!
9+
# GenerateTestFile
10+
11+
This script implements utilities for generating test files for exercises in the track.
12+
13+
## Running the generator
14+
15+
Run the generator from this folder using the following command:
16+
17+
```lean
18+
lake exe generator [Options] <exercise-slug> [Extra-Parameters]
19+
```
20+
21+
The following options are available:
22+
23+
* `-s` or `--stub` - Generates a stub test generator in `./generators/Generator/Generator/`
24+
* `-a` or `--add` - Adds a practice exercise to `./exercises/practice` and generates a test file if a test generator exists.
25+
The author and the difficulty of the new exercise may be passed as extra parameters.
26+
* `-g` or `--generate` - Generates a test file in `./exercises/practice/<exercise-slug>/`
27+
* `-r` or `--regenerate` - Regenerates all test files with a test generator, syncing all docs and test data.
28+
This option does not take any parameters.
29+
30+
`<exercise-slug>` is a required parameter for all options except `--regenerate`.
31+
The slug must be in kebab-case, for example: `two-fer`.
32+
33+
## Test generators
34+
35+
Generating test files requires a test generator in `./generators/Generator/Generator/`.
36+
37+
This generator must be imported in `./generators/Generator/Generator.lean` and must define the following three functions:
38+
39+
1. `introGenerator : String → String`
40+
2. `testCaseGenerator : String → Std.TreeMap.Raw String Lean.Json → String`
41+
3. `endBodyGenerator : String → String`
42+
43+
These functions must also be added to the `dispatch` table in `./generators/Generator/Generator.lean`, using the exercise name in `PascalCase` as the key.
44+
For example:
45+
46+
```lean
47+
def dispatch : Std.HashMap String (introGenerator × testCaseGenerator × endBodyGenerator) :=
48+
Std.HashMap.ofList [
49+
...
50+
("TwoFer", (TwoFerGenerator.genIntro, TwoFerGenerator.genTestCase, TwoFerGenerator.genEnd))
51+
...
52+
]
53+
```
54+
55+
Running the generator with `-s` or `--stub` automatically creates a stub test generator in the correct folder.
56+
It also adds an import for it in `./generators/Generator/Generator.lean` and an entry for its functions in the `dispatch` table.
57+
58+
## Importing files
59+
60+
The test generator may `import Helper` in order to use helper functions defined in `./generators/Generator/Helper.lean`.
61+
62+
Note that all generator files are built by Lean each time this script runs.
63+
Please keep dependencies to the minimum necessary.
64+
65+
In particular, *do not* import the entire `Lean` package.
66+
67+
## Adding test cases
68+
69+
Practice exercises get their test cases from `https://github.com/exercism/problem-specifications/`.
70+
71+
Additional test cases may be defined by the author in JSON format in an `extra.json` file in `exercises/practice/<exercise-slug>/.meta` folder.
72+
73+
The `testCaseGenerator` function is called for each case from `problem-specifications` and for any extra cases.
74+
-/
75+
876
structure OrderedMap where
977
order : Array String
1078
map : TreeMap.Raw String Json
@@ -189,21 +257,22 @@ def getIncludes (toml : String) : TreeMap String String :=
189257

190258
def showUsage : IO Unit :=
191259
let usageMsg := s!"Usage is:
192-
lake exe generator [Options] <exercise-slug> [Extra-Options]
260+
lake exe generator [Options] <exercise-slug> [Extra-Parameters]
193261
194262
Options:
195263
-s / --stub :
196-
Generates a stub Generator for the exercise in ./generators/Generator/Generator/
197-
198-
-g / --generate :
199-
Generates a test file for the exercise in ./exercises/practice/<exercise-slug>/
264+
Generates a stub test generator for the exercise in `./generators/Generator/Generator/`
200265
201266
-a / --add :
202-
Adds a practice exercise and then generates a test file, if there is a Generator for it.
203-
Other than the exercise slug, the author and the difficulty of the new exercise may be passed as extra options.
267+
Adds a practice exercise to `./exercises/practice` and then generates a test file if a test generator exists.
268+
The author and the difficulty of the new exercise may be passed as extra parameters.
269+
270+
-g / --generate :
271+
Generates a test file for the exercise in `./exercises/practice/<exercise-slug>/`
204272
205273
-r / --regenerate :
206-
Regenerates all test files with a Generator, syncing all docs and test data with canonical-data."
274+
Regenerates all test files with a test generator, syncing all docs and test data.
275+
This option does not take any parameters."
207276
IO.println usageMsg
208277

209278
def generateTestFile (exercise : String) : IO Unit := do

generators/GenerateTestFile.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# GenerateTestFile
2+
3+
This script implements utilities for generating test files for exercises in the track.
4+
5+
## Running the generator
6+
7+
Run the generator from this folder using the following command:
8+
9+
```lean
10+
lake exe generator [Options] <exercise-slug> [Extra-Parameters]
11+
```
12+
13+
The following options are available:
14+
15+
* `-s` or `--stub` - Generates a stub test generator in `./generators/Generator/Generator/`
16+
* `-a` or `--add` - Adds a practice exercise to `./exercises/practice` and generates a test file if a test generator exists.
17+
The author and the difficulty of the new exercise may be passed as extra parameters.
18+
* `-g` or `--generate` - Generates a test file in `./exercises/practice/<exercise-slug>/`
19+
* `-r` or `--regenerate` - Regenerates all test files with a test generator, syncing all docs and test data.
20+
This option does not take any parameters.
21+
22+
`<exercise-slug>` is a required parameter for all options except `--regenerate`.
23+
The slug must be in kebab-case, for example: `two-fer`.
24+
25+
## Test generators
26+
27+
Generating test files requires a test generator in `./generators/Generator/Generator/`.
28+
29+
This generator must be imported in `./generators/Generator/Generator.lean` and must define the following three functions:
30+
31+
1. `introGenerator : String → String`
32+
2. `testCaseGenerator : String → Std.TreeMap.Raw String Lean.Json → String`
33+
3. `endBodyGenerator : String → String`
34+
35+
These functions must also be added to the `dispatch` table in `./generators/Generator/Generator.lean`, using the exercise name in `PascalCase` as the key.
36+
For example:
37+
38+
```lean
39+
def dispatch : Std.HashMap String (introGenerator × testCaseGenerator × endBodyGenerator) :=
40+
Std.HashMap.ofList [
41+
...
42+
("TwoFer", (TwoFerGenerator.genIntro, TwoFerGenerator.genTestCase, TwoFerGenerator.genEnd))
43+
...
44+
]
45+
```
46+
47+
Running the generator with `-s` or `--stub` automatically creates a stub test generator in the correct folder.
48+
It also adds an import for it in `./generators/Generator/Generator.lean` and an entry for its functions in the `dispatch` table.
49+
50+
## Importing files
51+
52+
The test generator may `import Helper` in order to use helper functions defined in `./generators/Generator/Helper.lean`.
53+
54+
Note that all generator files are built by Lean each time this script runs.
55+
Please keep dependencies to the minimum necessary.
56+
57+
In particular, *do not* import the entire `Lean` package.
58+
59+
## Adding test cases
60+
61+
Practice exercises get their test cases from `https://github.com/exercism/problem-specifications/`.
62+
63+
Additional test cases may be defined by the author in JSON format in an `extra.json` file in `exercises/practice/<exercise-slug>/.meta` folder.
64+
65+
The `testCaseGenerator` function is called for each case from `problem-specifications` and for any extra cases.

0 commit comments

Comments
 (0)