| 1 | """Tests for RST preprocessor functionality.""" |
| 2 | |
| 3 | from textwrap import dedent |
| 4 | |
| 5 | from cyclopts.help.rst_preprocessor import ( |
| 6 | _gather_indented_block, |
| 7 | _skip_indented_block, |
| 8 | process_sphinx_directives, |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | def test_process_sphinx_directives_empty_content(): |
| 13 | """Test that empty/None content is handled gracefully.""" |
| 14 | assert process_sphinx_directives(None) == "" |
| 15 | assert process_sphinx_directives("") == "" |
| 16 | assert process_sphinx_directives(" ") == "" |
| 17 | |
| 18 | |
| 19 | def test_process_sphinx_directives_malformed_directive(): |
| 20 | """Test that malformed directives are handled gracefully.""" |
| 21 | text = dedent("""\ |
| 22 | Some text |
| 23 | |
| 24 | .. not-a-proper-directive |
| 25 | Content here |
| 26 | |
| 27 | More text |
| 28 | """) |
| 29 | result = process_sphinx_directives(text) |
| 30 | assert "Some text" in result |
| 31 | assert "More text" in result |
| 32 | |
| 33 | |
| 34 | def test_process_sphinx_directives_unknown_directive(): |
| 35 | """Test that unknown directives are skipped.""" |
| 36 | text = dedent("""\ |
| 37 | Some text |
| 38 | |
| 39 | .. unknown_directive:: arg |
| 40 | This content should be skipped |
| 41 | |
| 42 | More text |
| 43 | """) |
| 44 | result = process_sphinx_directives(text) |
| 45 | assert "Some text" in result |
| 46 | assert "More text" in result |
| 47 | assert "unknown_directive" not in result |
| 48 | assert "This content should be skipped" not in result |
| 49 | |
| 50 | |
| 51 | def test_skip_indented_block(): |
| 52 | """Test _skip_indented_block helper function.""" |
| 53 | lines = [ |
| 54 | "base line", |
| 55 | " indented line 1", |
| 56 | " indented line 2", |
| 57 | " more indented", |
| 58 | "", |
| 59 | " indented line 3", |
| 60 | "next base line", |
| 61 | ] |
| 62 | result_index = _skip_indented_block(lines, 1, 0) |
| 63 | assert result_index == 6 |
| 64 | assert lines[result_index] == "next base line" |
| 65 | |
| 66 | |
| 67 | def test_skip_indented_block_end_of_file(): |
| 68 | """Test _skip_indented_block when reaching end of file.""" |
| 69 | lines = [ |
| 70 | "base line", |
| 71 | " indented line 1", |
| 72 | " indented line 2", |
| 73 | ] |
| 74 | result_index = _skip_indented_block(lines, 1, 0) |
| 75 | assert result_index == 3 |
| 76 | |
| 77 | |
| 78 | def test_gather_indented_block(): |
| 79 | """Test _gather_indented_block helper function preserves paragraph structure.""" |
| 80 | lines = [ |
| 81 | "base line", |
| 82 | " indented line 1", |
| 83 | " indented line 2", |
| 84 | " more indented", |
| 85 | "", |
| 86 | " indented line 3", |
| 87 | "next base line", |
| 88 | ] |
| 89 | content, result_index = _gather_indented_block(lines, 1, 0) |
| 90 | assert content == [ |
| 91 | "indented line 1 indented line 2", |
| 92 | " more indented", |
| 93 | "indented line 3", |
| 94 | ] |
| 95 | assert result_index == 6 |
| 96 | |
| 97 | |
| 98 | def test_gather_indented_block_empty(): |
| 99 | """Test _gather_indented_block with no indented content.""" |
| 100 | lines = [ |
| 101 | "base line", |
| 102 | "next base line", |
| 103 | ] |
| 104 | content, result_index = _gather_indented_block(lines, 1, 0) |
| 105 | assert content == [] |
| 106 | assert result_index == 1 |
| 107 | |
| 108 | |
| 109 | def test_gather_indented_block_end_of_file(): |
| 110 | """Test _gather_indented_block when reaching end of file.""" |
| 111 | lines = [ |
| 112 | "base line", |
| 113 | " indented line 1", |
| 114 | " indented line 2", |
| 115 | ] |
| 116 | content, result_index = _gather_indented_block(lines, 1, 0) |
| 117 | assert content == ["indented line 1 indented line 2"] |
| 118 | assert result_index == 3 |
| 119 | |
| 120 | |
| 121 | def test_process_sphinx_directives_with_options(): |
| 122 | """Test that directive options are ignored (not processed).""" |
| 123 | text = dedent("""\ |
| 124 | Some text |
| 125 | |
| 126 | .. versionadded:: 1.0 |
| 127 | :platform: Unix |
| 128 | :synopsis: Added Unix support |
| 129 | |
| 130 | More text |
| 131 | """) |
| 132 | result = process_sphinx_directives(text) |
| 133 | assert "Some text" in result |
| 134 | assert "More text" in result |
| 135 | assert "[Added in v1.0]" in result |
| 136 | |
| 137 | |
| 138 | def test_process_sphinx_directives_with_list_content(normalize_trailing_whitespace): |
| 139 | """Test directives containing list structures.""" |
| 140 | text = dedent("""\ |
| 141 | Some text |
| 142 | |
| 143 | .. note:: |
| 144 | This note contains a list: |
| 145 | |
| 146 | - Item 1 |
| 147 | - Item 2 |
| 148 | - Item 3 |
| 149 | |
| 150 | More text |
| 151 | """) |
| 152 | result = process_sphinx_directives(text) |
| 153 | expected = dedent("""\ |
| 154 | Some text |
| 155 | |
| 156 | Note: This note contains a list: |
| 157 | |
| 158 | - Item 1 |
| 159 | - Item 2 |
| 160 | - Item 3 |
| 161 | More text""") |
| 162 | assert normalize_trailing_whitespace(result) == normalize_trailing_whitespace(expected) |
| 163 | |
| 164 | |
| 165 | def test_process_sphinx_directives_non_ascii_content(normalize_trailing_whitespace): |
| 166 | """Test directives with non-ASCII characters.""" |
| 167 | text = dedent("""\ |
| 168 | Some text |
| 169 | |
| 170 | .. versionadded:: 2.0β |
| 171 | |
| 172 | .. note:: 这是中文内容 / Contenu français / Содержание |
| 173 | |
| 174 | .. deprecated:: 3.0α |
| 175 | Use alternative 日本語 |
| 176 | |
| 177 | More text |
| 178 | """) |
| 179 | result = process_sphinx_directives(text) |
| 180 | expected = dedent("""\ |
| 181 | Some text |
| 182 | |
| 183 | |
| 184 | Note: 这是中文内容 / Contenu français / Содержание |
| 185 | More text [Added in v2.0β] [⚠ Deprecated in v3.0α] Use alternative 日本語""") |
| 186 | assert normalize_trailing_whitespace(result) == normalize_trailing_whitespace(expected) |
| 187 | |
| 188 | |
| 189 | def test_process_sphinx_directives_nested_directives(normalize_trailing_whitespace): |
| 190 | """Test directives that contain other directives (should handle gracefully).""" |
| 191 | text = dedent("""\ |
| 192 | Some text |
| 193 | |
| 194 | .. note:: |
| 195 | This is a note. |
| 196 | |
| 197 | .. warning:: |
| 198 | Nested warning inside note. |
| 199 | |
| 200 | More text |
| 201 | """) |
| 202 | result = process_sphinx_directives(text) |
| 203 | expected = dedent("""\ |
| 204 | Some text |
| 205 | |
| 206 | Note: This is a note. |
| 207 | |
| 208 | .. warning:: |
| 209 | |
| 210 | Nested warning inside note. |
| 211 | More text""") |
| 212 | assert normalize_trailing_whitespace(result) == normalize_trailing_whitespace(expected) |
| 213 | |
| 214 | |
| 215 | def test_gather_indented_block_with_multiple_paragraphs(): |
| 216 | """Test _gather_indented_block with multiple paragraphs separated by blank lines.""" |
| 217 | lines = [ |
| 218 | "base line", |
| 219 | " First paragraph line 1", |
| 220 | " First paragraph line 2", |
| 221 | "", |
| 222 | " Second paragraph line 1", |
| 223 | " Second paragraph line 2", |
| 224 | "", |
| 225 | " Third paragraph", |
| 226 | "next base line", |
| 227 | ] |
| 228 | content, result_index = _gather_indented_block(lines, 1, 0) |
| 229 | assert content == [ |
| 230 | "First paragraph line 1 First paragraph line 2", |
| 231 | "Second paragraph line 1 Second paragraph line 2", |
| 232 | "Third paragraph", |
| 233 | ] |
| 234 | assert result_index == 8 |
| 235 | |
| 236 | |
| 237 | def test_process_sphinx_directives_code_block_in_directive(normalize_trailing_whitespace): |
| 238 | """Test directives containing code block-like content.""" |
| 239 | text = dedent("""\ |
| 240 | Some text |
| 241 | |
| 242 | .. note:: |
| 243 | Use this function like: |
| 244 | |
| 245 | result = my_function(arg) |
| 246 | print(result) |
| 247 | |
| 248 | Remember to check the output. |
| 249 | |
| 250 | More text |
| 251 | """) |
| 252 | result = process_sphinx_directives(text) |
| 253 | expected = dedent("""\ |
| 254 | Some text |
| 255 | |
| 256 | Note: Use this function like: |
| 257 | |
| 258 | result = my_function(arg) |
| 259 | print(result) |
| 260 | |
| 261 | Remember to check the output. |
| 262 | More text""") |
| 263 | assert normalize_trailing_whitespace(result) == normalize_trailing_whitespace(expected) |
| 264 | |
| 265 | |
| 266 | def test_process_sphinx_directives_version_with_punctuation(normalize_trailing_whitespace): |
| 267 | """Test version numbers with various punctuation.""" |
| 268 | text = dedent("""\ |
| 269 | .. versionadded:: 1.2.3 |
| 270 | |
| 271 | .. versionchanged:: 2.0.0-rc1 |
| 272 | |
| 273 | .. deprecated:: 3.0.0a1 |
| 274 | """) |
| 275 | result = process_sphinx_directives(text) |
| 276 | expected = "[Added in v1.2.3] [Changed in v2.0.0-rc1] [⚠ Deprecated in v3.0.0a1]" |
| 277 | assert normalize_trailing_whitespace(result) == normalize_trailing_whitespace(expected) |