@@ -247,3 +247,175 @@ def test_convert_code_format_replacements():
247247 transformed = core_agent_module .convert_code_format (original_text )
248248
249249 assert transformed == expected_text , "convert_code_format did not perform expected replacements"
250+
251+ # ----------------------------------------------------------------------------
252+ # Tests for parse_code_blobs function
253+ # ----------------------------------------------------------------------------
254+
255+ def test_parse_code_blobs_python_match ():
256+ """Test parse_code_blobs with ```python\n content\n ``` pattern."""
257+ text = """Here is some code:
258+ ```python
259+ print("Hello World")
260+ x = 42
261+ ```
262+ And some more text."""
263+
264+ result = core_agent_module .parse_code_blobs (text )
265+ expected = "print(\" Hello World\" )\n x = 42"
266+ assert result == expected
267+
268+
269+ def test_parse_code_blobs_py_match ():
270+ """Test parse_code_blobs with ```py\n content\n ``` pattern."""
271+ text = """Here is some code:
272+ ```py
273+ def hello():
274+ return "Hello"
275+ ```
276+ And some more text."""
277+
278+ result = core_agent_module .parse_code_blobs (text )
279+ expected = "def hello():\n return \" Hello\" "
280+ assert result == expected
281+
282+
283+ def test_parse_code_blobs_multiple_matches ():
284+ """Test parse_code_blobs with multiple code blocks."""
285+ text = """First code block:
286+ ```python
287+ print("First")
288+ ```
289+
290+ Second code block:
291+ ```py
292+ print("Second")
293+ ```"""
294+
295+ result = core_agent_module .parse_code_blobs (text )
296+ expected = "print(\" First\" )\n \n print(\" Second\" )"
297+ assert result == expected
298+
299+
300+ def test_parse_code_blobs_with_whitespace ():
301+ """Test parse_code_blobs with whitespace around language identifier."""
302+ text = """Code with whitespace:
303+ ```python
304+ print("Hello")
305+ ```
306+ More code:
307+ ```py
308+ print("World")
309+ ```"""
310+
311+ result = core_agent_module .parse_code_blobs (text )
312+ expected = "print(\" Hello\" )\n \n print(\" World\" )"
313+ assert result == expected
314+
315+
316+ def test_parse_code_blobs_no_match ():
317+ """Test parse_code_blobs with ```\n content\n ``` (no language specified)."""
318+ text = """Here is some code:
319+ ```
320+ print("Hello World")
321+ ```
322+ But no language specified."""
323+
324+ with pytest .raises (ValueError ) as exc_info :
325+ core_agent_module .parse_code_blobs (text )
326+
327+ assert "regex pattern" in str (exc_info .value )
328+
329+
330+ def test_parse_code_blobs_javascript_no_match ():
331+ """Test parse_code_blobs with ```javascript\n content\n ``` (other language)."""
332+ text = """Here is some JavaScript code:
333+ ```javascript
334+ console.log("Hello World");
335+ ```
336+ But this should not match."""
337+
338+ with pytest .raises (ValueError ) as exc_info :
339+ core_agent_module .parse_code_blobs (text )
340+
341+ assert "regex pattern" in str (exc_info .value )
342+
343+
344+ def test_parse_code_blobs_java_no_match ():
345+ """Test parse_code_blobs with ```java\n content\n ``` (other language)."""
346+ text = """Here is some Java code:
347+ ```java
348+ System.out.println("Hello World");
349+ ```
350+ But this should not match."""
351+
352+ with pytest .raises (ValueError ) as exc_info :
353+ core_agent_module .parse_code_blobs (text )
354+
355+ assert "regex pattern" in str (exc_info .value )
356+
357+
358+ def test_parse_code_blobs_direct_python_code ():
359+ """Test parse_code_blobs with direct Python code (no code blocks)."""
360+ text = """print("Hello World")
361+ x = 42
362+ def hello():
363+ return "Hello\" """
364+
365+ result = core_agent_module .parse_code_blobs (text )
366+ assert result == text
367+
368+
369+ def test_parse_code_blobs_invalid_python_syntax ():
370+ """Test parse_code_blobs with invalid Python syntax (should raise ValueError)."""
371+ text = """print("Hello World"
372+ x = 42
373+ def hello(:
374+ return "Hello\" """
375+
376+ with pytest .raises (ValueError ) as exc_info :
377+ core_agent_module .parse_code_blobs (text )
378+
379+ assert "regex pattern" in str (exc_info .value )
380+
381+
382+ def test_parse_code_blobs_generic_error ():
383+ """Test parse_code_blobs with generic case that should raise ValueError."""
384+ text = """This is just some random text.
385+ Just plain text that should fail."""
386+
387+ with pytest .raises (ValueError ) as exc_info :
388+ core_agent_module .parse_code_blobs (text )
389+
390+ error_msg = str (exc_info .value )
391+ assert "regex pattern" in error_msg
392+ assert "Make sure to include code with the correct pattern" in error_msg
393+
394+
395+ def test_parse_code_blobs_single_line_content ():
396+ """Test parse_code_blobs with single line content."""
397+ text = """Single line:
398+ ```python
399+ print("Hello")
400+ ```"""
401+
402+ result = core_agent_module .parse_code_blobs (text )
403+ expected = "print(\" Hello\" )"
404+ assert result == expected
405+
406+
407+ def test_parse_code_blobs_mixed_content ():
408+ """Test parse_code_blobs with mixed content including non-code text."""
409+ text = """Thoughts: I need to calculate the sum
410+ Code:
411+ ```python
412+ def sum_numbers(a, b):
413+ return a + b
414+
415+ result = sum_numbers(5, 3)
416+ ```
417+ The result is 8."""
418+
419+ result = core_agent_module .parse_code_blobs (text )
420+ expected = "def sum_numbers(a, b):\n return a + b\n \n result = sum_numbers(5, 3)"
421+ assert result == expected
0 commit comments