Chih-Hung Hsieh | bde955c | 2019-04-01 20:05:54 -0700 | [diff] [blame] | 1 | # Prolog Unit Test Examples |
| 2 | |
| 3 | ## Run all examples |
| 4 | |
| 5 | Build a local gerrit.war and then run the script: |
| 6 | |
| 7 | ./run.sh |
| 8 | |
| 9 | Note that a local Gerrit server is not needed because |
| 10 | these unit test examples redefine wrappers of the `gerrit:change\*` |
| 11 | rules to provide mocked change data. |
| 12 | |
| 13 | ## Add a new unit test |
| 14 | |
| 15 | Please follow the pattern in `t1.pl`, `t2.pl`, or `t3.pl`. |
| 16 | |
| 17 | * Put code to be tested in a file, e.g. `rules.pl`. |
| 18 | For easy unit testing, split long clauses into short ones |
| 19 | and test every positive and negative path. |
| 20 | |
| 21 | * Create a new unit test file, e.g. `t1.pl`, |
| 22 | which should _load_ the test source file and `utils.pl`. |
| 23 | |
| 24 | % First load all source files and the utils.pl. |
| 25 | :- load([aosp_rules,utils]). |
| 26 | |
| 27 | :- begin_tests(t1). % give this test any name |
| 28 | |
| 29 | % Use test0/1 or test1/1 to verify failed/passed goals. |
| 30 | |
| 31 | :- end_tests(_,0). % check total pass/fail counts |
| 32 | |
| 33 | * Optionally replace calls to gerrit functions that depend on repository. |
| 34 | For example, define the following wrappers and in source code, use |
| 35 | `change_branch/1` instead of `gerrti:change_branch/1`. |
| 36 | |
| 37 | change_branch(X) :- gerrit:change_branch(X). |
| 38 | commit_label(L,U) :- gerrit:commit_label(L,U). |
| 39 | |
| 40 | * In unit test file, redefine the gerrit function wrappers and test. |
| 41 | For example, in `t3.pl`, we have: |
| 42 | |
| 43 | :- redefine(uploader,1,uploader(user(42))). % mocked uploader |
| 44 | :- test1(uploader(user(42))). |
| 45 | :- test0(is_exempt_uploader). |
| 46 | |
| 47 | % is_exempt_uploader/0 is expected to fail because it is |
| 48 | % is_exempt_uploader :- uploader(user(Id)), memberchk(Id, [104, 106]). |
| 49 | |
| 50 | % Note that gerrit:remove_label does not depend on Gerrit repository, |
| 51 | % so its caller remove_label/1 is tested without any redefinition. |
| 52 | |
| 53 | :- test1(remove_label('MyReview',[],[])). |
| 54 | :- test1(remove_label('MyReview',submit(),submit())). |