2026-04-18 15:46:22 -04:00
|
|
|
"""
|
|
|
|
|
Tests for scripts/normalize-code-blocks.py — Code block indentation normalization.
|
|
|
|
|
"""
|
2026-04-16 04:58:24 +00:00
|
|
|
|
|
|
|
|
import json
|
2026-04-18 15:46:22 -04:00
|
|
|
import unittest
|
2026-04-16 04:58:24 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2026-04-18 15:46:22 -04:00
|
|
|
import sys
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
|
|
|
|
|
from normalize_code_blocks import process_line
|
2026-04-16 04:58:24 +00:00
|
|
|
|
|
|
|
|
|
2026-04-18 15:46:22 -04:00
|
|
|
class TestProcessLine(unittest.TestCase):
|
|
|
|
|
def test_normalizes_indented_code_block(self):
|
|
|
|
|
entry = {
|
|
|
|
|
"prompt": "Write code",
|
|
|
|
|
"response": "```python\n def hello():\n print('world')\n```"
|
2026-04-16 04:58:24 +00:00
|
|
|
}
|
2026-04-18 15:46:22 -04:00
|
|
|
line = json.dumps(entry)
|
|
|
|
|
result, count = process_line(line)
|
|
|
|
|
parsed = json.loads(result.strip())
|
|
|
|
|
# Code block indentation should be normalized
|
|
|
|
|
self.assertIn("def hello():", parsed["response"])
|
|
|
|
|
|
|
|
|
|
def test_preserves_non_code_content(self):
|
|
|
|
|
entry = {"prompt": "Hello", "response": "How are you?"}
|
|
|
|
|
line = json.dumps(entry)
|
|
|
|
|
result, count = process_line(line)
|
|
|
|
|
parsed = json.loads(result.strip())
|
|
|
|
|
self.assertEqual(parsed["response"], "How are you?")
|
|
|
|
|
|
|
|
|
|
def test_handles_multiple_code_blocks(self):
|
|
|
|
|
entry = {
|
|
|
|
|
"prompt": "Two blocks",
|
|
|
|
|
"response": "First:\n```python\n x = 1\n```\nSecond:\n```python\n y = 2\n```"
|
2026-04-16 04:58:24 +00:00
|
|
|
}
|
2026-04-18 15:46:22 -04:00
|
|
|
line = json.dumps(entry)
|
|
|
|
|
result, count = process_line(line)
|
|
|
|
|
parsed = json.loads(result.strip())
|
|
|
|
|
self.assertIn("x = 1", parsed["response"])
|
|
|
|
|
self.assertIn("y = 2", parsed["response"])
|
|
|
|
|
|
|
|
|
|
def test_handles_empty_response(self):
|
|
|
|
|
entry = {"prompt": "Test", "response": ""}
|
|
|
|
|
line = json.dumps(entry)
|
|
|
|
|
result, count = process_line(line)
|
|
|
|
|
parsed = json.loads(result.strip())
|
|
|
|
|
self.assertEqual(parsed["response"], "")
|
|
|
|
|
|
|
|
|
|
def test_preserves_prompt(self):
|
|
|
|
|
entry = {"prompt": "Write a function", "response": "```python\n def f(): pass\n```"}
|
|
|
|
|
line = json.dumps(entry)
|
|
|
|
|
result, count = process_line(line)
|
|
|
|
|
parsed = json.loads(result.strip())
|
|
|
|
|
self.assertEqual(parsed["prompt"], "Write a function")
|
2026-04-16 04:58:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|