|
| 1 | +# Test Suite Documentation |
| 2 | + |
| 3 | +This directory contains comprehensive test coverage for the `docx_viewer` package. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +``` |
| 8 | +test/ |
| 9 | +├── docx_viewer_test.dart # Main test entry point |
| 10 | +├── fixtures/ |
| 11 | +│ └── test_docx_generator.dart # Helper to generate test DOCX files |
| 12 | +├── src/ |
| 13 | +│ ├── docx_view_test.dart # Widget tests for DocxView |
| 14 | +│ ├── extract_text_from_docx_test.dart # Tests for text extraction |
| 15 | +│ ├── file_io_stub_test.dart # Tests for stub implementation |
| 16 | +│ └── file_io_web_test.dart # Tests for web implementation |
| 17 | +└── utils/ |
| 18 | + └── support_type_test.dart # Tests for utility classes |
| 19 | +``` |
| 20 | + |
| 21 | +## Test Coverage |
| 22 | + |
| 23 | +### 1. Text Extraction Tests (`src/extract_text_from_docx_test.dart`) |
| 24 | +- Extract text from simple DOCX files |
| 25 | +- Extract text from DOCX with multiple paragraphs |
| 26 | +- Handle empty DOCX documents |
| 27 | +- Extract and number items from DOCX with numbering |
| 28 | +- Handle special characters and unicode |
| 29 | +- Handle invalid ZIP/DOCX data |
| 30 | +- Handle empty paragraphs and whitespace |
| 31 | +- Handle long text content |
| 32 | +- Test FirstOrNullExtension utility |
| 33 | + |
| 34 | +### 2. DocxView Widget Tests (`src/docx_view_test.dart`) |
| 35 | +- Display loading indicator during content load |
| 36 | +- Display content after loading with bytes parameter |
| 37 | +- Apply custom font size |
| 38 | +- Use default font size when not specified |
| 39 | +- Display multiple paragraphs with newlines |
| 40 | +- Handle empty documents |
| 41 | +- Call onError callback when no input provided |
| 42 | +- Display error messages without callback |
| 43 | +- Validate error when both filePath and bytes provided |
| 44 | +- Handle invalid bytes gracefully |
| 45 | +- Render content in scrollable view |
| 46 | +- Handle numbered lists |
| 47 | +- Apply correct padding |
| 48 | + |
| 49 | +### 3. Platform-Specific File I/O Tests |
| 50 | +- Stub implementation tests (`src/file_io_stub_test.dart`) |
| 51 | +- Web implementation tests (`src/file_io_web_test.dart`) |
| 52 | +- Verify proper error messages for unsupported operations |
| 53 | + |
| 54 | +### 4. Utility Tests (`utils/support_type_test.dart`) |
| 55 | +- Validate Supporttype constants |
| 56 | + |
| 57 | +## Running Tests |
| 58 | + |
| 59 | +### Run All Tests |
| 60 | +```bash |
| 61 | +flutter test |
| 62 | +``` |
| 63 | + |
| 64 | +### Run Tests with Coverage |
| 65 | +```bash |
| 66 | +flutter test --coverage |
| 67 | +``` |
| 68 | + |
| 69 | +### Run Specific Test File |
| 70 | +```bash |
| 71 | +flutter test test/src/docx_view_test.dart |
| 72 | +``` |
| 73 | + |
| 74 | +### View Coverage Report |
| 75 | +After running tests with coverage, you can generate an HTML report: |
| 76 | + |
| 77 | +```bash |
| 78 | +# Install lcov (Ubuntu/Debian) |
| 79 | +sudo apt-get install lcov |
| 80 | + |
| 81 | +# Generate HTML report |
| 82 | +genhtml coverage/lcov.info -o coverage/html |
| 83 | + |
| 84 | +# Open in browser |
| 85 | +open coverage/html/index.html |
| 86 | +``` |
| 87 | + |
| 88 | +## Test Fixtures |
| 89 | + |
| 90 | +The `fixtures/test_docx_generator.dart` file provides helper methods to generate test DOCX files: |
| 91 | + |
| 92 | +- `createSimpleDocx(String text)` - Creates a simple DOCX with given text |
| 93 | +- `createDocxWithNumbering(List<String> items)` - Creates DOCX with numbered list |
| 94 | +- `createEmptyDocx()` - Creates an empty DOCX file |
| 95 | +- `createDocxWithMultipleParagraphs(List<String> paragraphs)` - Creates DOCX with multiple paragraphs |
| 96 | + |
| 97 | +These helpers create proper DOCX files (ZIP archives) with the correct XML structure for testing. |
| 98 | + |
| 99 | +## Continuous Integration |
| 100 | + |
| 101 | +Tests are automatically run on every pull request through GitHub Actions (`.github/workflows/ci.yml`): |
| 102 | + |
| 103 | +1. **Analyze Job**: Runs static analysis and formatting checks |
| 104 | +2. **Test Job**: Runs all tests with coverage |
| 105 | + - Generates coverage report |
| 106 | + - Posts coverage summary as PR comment |
| 107 | + - Uploads coverage artifacts |
| 108 | + |
| 109 | +The CI workflow: |
| 110 | +- Runs on pull requests to `main` and `dev` branches |
| 111 | +- Runs on push to `main` and `dev` branches |
| 112 | +- Generates test coverage reports |
| 113 | +- Comments on PRs with coverage information |
| 114 | +- Provides coverage badges |
| 115 | + |
| 116 | +## Adding New Tests |
| 117 | + |
| 118 | +When adding new tests: |
| 119 | + |
| 120 | +1. Create test files in appropriate directories (`src/`, `utils/`, etc.) |
| 121 | +2. Follow the existing test structure and naming conventions |
| 122 | +3. Use descriptive test names that explain what is being tested |
| 123 | +4. Include arrange-act-assert comments in tests for clarity |
| 124 | +5. Import the test file in `docx_viewer_test.dart` to include in the main test suite |
| 125 | +6. Run tests locally before committing |
| 126 | + |
| 127 | +Example: |
| 128 | +```dart |
| 129 | +import 'package:flutter_test/flutter_test.dart'; |
| 130 | +
|
| 131 | +void main() { |
| 132 | + group('FeatureName', () { |
| 133 | + test('should do something specific', () { |
| 134 | + // Arrange |
| 135 | + final input = 'test'; |
| 136 | + |
| 137 | + // Act |
| 138 | + final result = functionUnderTest(input); |
| 139 | + |
| 140 | + // Assert |
| 141 | + expect(result, equals('expected')); |
| 142 | + }); |
| 143 | + }); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +## Test Best Practices |
| 148 | + |
| 149 | +1. **Isolation**: Each test should be independent and not rely on other tests |
| 150 | +2. **Clarity**: Use descriptive test names that explain the scenario |
| 151 | +3. **Coverage**: Aim for high coverage but focus on meaningful tests |
| 152 | +4. **Edge Cases**: Test boundary conditions, error cases, and edge cases |
| 153 | +5. **Maintainability**: Keep tests simple and maintainable |
| 154 | +6. **Performance**: Tests should run quickly to support rapid development |
| 155 | + |
| 156 | +## Coverage Goals |
| 157 | + |
| 158 | +The package aims for: |
| 159 | +- **Minimum**: 80% code coverage |
| 160 | +- **Target**: 90%+ code coverage |
| 161 | +- **Focus**: All critical paths and error handling must be tested |
| 162 | + |
| 163 | +Current coverage is tracked automatically in CI and reported on pull requests. |
0 commit comments