|
| 1 | +## Tests |
| 2 | + |
| 3 | +The java2python package includes a [test suite][] for exercising the compiler and |
| 4 | +its various translation features. This doc explains how the tests work, how to |
| 5 | +run these suite, and how to add new tests to it. |
| 6 | + |
| 7 | +### How the Test Suite Works |
| 8 | + |
| 9 | +The test suite is a [makefile][] that finds `.java` files in the same directory, |
| 10 | +converts each to Python, runs both programs, and then compares their output. If |
| 11 | +the output matches, the test is considered successful. If not, it's considered |
| 12 | +a failure. |
| 13 | + |
| 14 | +### How to Run the Test Suite |
| 15 | + |
| 16 | +The simplest way to run the suite is to run all of it: |
| 17 | + |
| 18 | +```bash |
| 19 | +$ cd some_path_to/java2python/test |
| 20 | +$ make |
| 21 | +``` |
| 22 | + |
| 23 | +This will print lines like this: |
| 24 | + |
| 25 | +``` |
| 26 | +... |
| 27 | +[PASS] Class00 |
| 28 | +[PASS] Class01 |
| 29 | +[PASS] Class02 |
| 30 | +... |
| 31 | +``` |
| 32 | + |
| 33 | +You can also run an individual test like this: |
| 34 | + |
| 35 | +```bash |
| 36 | +$ make Class02 |
| 37 | +[PASS] Class02 |
| 38 | +``` |
| 39 | + |
| 40 | +Notice that there isn't a suffix to the file name; you don't run `make |
| 41 | +Class02.java`, just `make Class02`. If you supply an extension, nothing will |
| 42 | +happen and the test won't run. |
| 43 | + |
| 44 | +The `test` directory contains two helper scripts used by the makefile that you |
| 45 | +can also use during development. The first is `[runjava][]`, which runs the |
| 46 | +Java compiler and the Java VM with the indicated file. Use it like this: |
| 47 | + |
| 48 | +```bash |
| 49 | +$ ./runjava Class01.java |
| 50 | +Hello, world. |
| 51 | +``` |
| 52 | + |
| 53 | +The second script is `[runj2py][]`, which is a handy shortcut for running the |
| 54 | +`j2py` script with preset command line arguments for the test configuration. |
| 55 | +You run it like this: |
| 56 | + |
| 57 | +```bash |
| 58 | +$ ./runj2py Class01.java |
| 59 | +#!/usr/bin/env python |
| 60 | +""" generated source for module Class01 """ |
| 61 | +class Class01(object): |
| 62 | +... |
| 63 | +``` |
| 64 | + |
| 65 | +### Adding New Tests |
| 66 | + |
| 67 | +When a new compiler feature is added, or when the translation semantics change, |
| 68 | +it's a good idea to add one or more tests to the test suite. Follow this |
| 69 | +general outline: |
| 70 | + |
| 71 | +1. Create a Java source file that exhibits the language feature in question. |
| 72 | + |
| 73 | +2. Name the Java source file `FeatureNN` where `NN` is the next number in |
| 74 | +sequence for `Feature`, e.g., `Class14.java`. |
| 75 | + |
| 76 | +3. In your Java source, write one or more values to stdout with |
| 77 | +`System.out.println`. |
| 78 | + |
| 79 | +4. Check the comparison via `make FeatureNN`. If the test passes, it might |
| 80 | +indicate the new feature is working correctly. |
| 81 | + |
| 82 | +[test suite]: https://github.com/natural/java2python/tree/master/test/ |
| 83 | +[makefile]: https://github.com/natural/java2python/blob/master/test/Makefile |
| 84 | +[runjava]: https://github.com/natural/java2python/blob/master/test/runjava |
| 85 | +[runj2py]: https://github.com/natural/java2python/blob/master/test/runj2py |
0 commit comments