|
| 1 | +# Developer Guide |
| 2 | + |
| 3 | +This guide provides technical information for developers working on the MathCAT codebase. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +To develop MathCAT, you need to have Rust installed. If you haven't already: |
| 8 | + |
| 9 | +1. [Download and install Rust](https://www.rust-lang.org/tools/install) |
| 10 | +2. Clone the MathCAT repository |
| 11 | +3. Open the project directory in your IDE |
| 12 | + |
| 13 | +## Working with Cargo |
| 14 | + |
| 15 | +Cargo is Rust's build system and package manager. Here are the essential commands you'll use: |
| 16 | + |
| 17 | +### Building the Project |
| 18 | + |
| 19 | +```bash |
| 20 | +# Build the project in debug mode |
| 21 | +cargo build |
| 22 | + |
| 23 | +# Build the project in release mode (optimized) |
| 24 | +cargo build --release |
| 25 | +``` |
| 26 | + |
| 27 | +### Running the Project |
| 28 | + |
| 29 | +```bash |
| 30 | +# Run the main executable |
| 31 | +cargo run |
| 32 | + |
| 33 | +# Run with specific arguments |
| 34 | +cargo run -- <args> |
| 35 | +``` |
| 36 | + |
| 37 | +### Managing Dependencies |
| 38 | + |
| 39 | +Dependencies are defined in `Cargo.toml`. Cargo automatically downloads and manages them. |
| 40 | + |
| 41 | +```bash |
| 42 | +# Update dependencies to their latest compatible versions |
| 43 | +cargo update |
| 44 | +``` |
| 45 | + |
| 46 | +## Testing |
| 47 | + |
| 48 | +Testing is crucial for maintaining code quality and ensuring that changes don't break existing functionality. |
| 49 | + |
| 50 | +### Running Tests |
| 51 | + |
| 52 | +```bash |
| 53 | +# Run all tests |
| 54 | +cargo test |
| 55 | + |
| 56 | +# Run a specific test |
| 57 | +cargo test test_name |
| 58 | +``` |
| 59 | + |
| 60 | +### Writing Tests |
| 61 | + |
| 62 | +Tests in MathCAT verify that MathML expressions produce the expected speech output. Example: |
| 63 | + |
| 64 | +```rust |
| 65 | +#[test] |
| 66 | +fn test_simple_fraction() { |
| 67 | + let expr = "<math> |
| 68 | + <mfrac> |
| 69 | + <mn>1</mn> |
| 70 | + <mn>2</mn> |
| 71 | + </mfrac> |
| 72 | + </math>"; |
| 73 | + test("en", "SimpleSpeak", expr, "1 half"); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Test Coverage |
| 78 | + |
| 79 | +Test coverage helps identify which parts of the code are exercised by tests and which parts need more testing. |
| 80 | + |
| 81 | +<details> |
| 82 | +<summary>Using grcov on macOS</summary> |
| 83 | + |
| 84 | +This approach uses `llvm-cov` and `grcov` to generate test coverage reports. Other operating systems should also work with [grcov](https://github.com/mozilla/grcov), but may require some adjustments regarding LLVM paths and configuration. |
| 85 | + |
| 86 | +**One-time setup:** |
| 87 | + |
| 88 | +```bash |
| 89 | +# Install required components |
| 90 | +rustup component add llvm-tools-preview |
| 91 | +cargo install grcov |
| 92 | +``` |
| 93 | + |
| 94 | +**Generate coverage report:** |
| 95 | + |
| 96 | +```bash |
| 97 | +# Set environment variable for profiling data |
| 98 | +export LLVM_PROFILE_FILE="target/coverage/%p-%m.profraw" |
| 99 | + |
| 100 | +# Run tests with coverage instrumentation |
| 101 | +RUSTFLAGS="-Cinstrument-coverage" cargo test |
| 102 | + |
| 103 | +# Example: Run a single test |
| 104 | +# RUSTFLAGS="-Cinstrument-coverage" cargo test Languages::zh::tw::units::without_prefix_powers_of_2 |
| 105 | + |
| 106 | +# Generate HTML report |
| 107 | +grcov . \ |
| 108 | + --source-dir . \ |
| 109 | + --binary-path ./target/debug/deps \ |
| 110 | + -t html \ |
| 111 | + --branch \ |
| 112 | + --ignore-not-existing \ |
| 113 | + --ignore "target/*" \ |
| 114 | + -o target/coverage/html |
| 115 | + |
| 116 | +# Open the report in your browser |
| 117 | +open target/coverage/html/index.html |
| 118 | +``` |
| 119 | + |
| 120 | +</details> |
| 121 | + |
| 122 | +**Alternative: IDE Integration** |
| 123 | + |
| 124 | +Many Rust IDEs provide built-in test coverage support, like RustRover or VSCode. |
0 commit comments