test_visualize.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from unittest.mock import mock_open, patch
  2. import pytest
  3. from openhands.linter.base import LintResult
  4. @pytest.fixture
  5. def mock_file_content():
  6. return '\n'.join([f'Line {i}' for i in range(1, 21)])
  7. def test_visualize_standard_case(mock_file_content):
  8. lint_result = LintResult(
  9. file='test_file.py', line=10, column=5, message='Test error message'
  10. )
  11. with patch('builtins.open', mock_open(read_data=mock_file_content)):
  12. result = lint_result.visualize(half_window=3)
  13. expected_output = (
  14. " 7|Line 7\n"
  15. " 8|Line 8\n"
  16. " 9|Line 9\n"
  17. "\033[91m10|Line 10\033[0m\n"
  18. f" {' ' * lint_result.column}^ ERROR HERE: Test error message\n"
  19. "11|Line 11\n"
  20. "12|Line 12\n"
  21. "13|Line 13"
  22. )
  23. assert result == expected_output
  24. def test_visualize_small_window(mock_file_content):
  25. lint_result = LintResult(
  26. file='test_file.py', line=10, column=5, message='Test error message'
  27. )
  28. with patch('builtins.open', mock_open(read_data=mock_file_content)):
  29. result = lint_result.visualize(half_window=1)
  30. expected_output = (
  31. " 9|Line 9\n"
  32. "\033[91m10|Line 10\033[0m\n"
  33. f" {' ' * lint_result.column}^ ERROR HERE: Test error message\n"
  34. "11|Line 11"
  35. )
  36. assert result == expected_output
  37. def test_visualize_error_at_start(mock_file_content):
  38. lint_result = LintResult(
  39. file='test_file.py', line=1, column=3, message='Start error'
  40. )
  41. with patch('builtins.open', mock_open(read_data=mock_file_content)):
  42. result = lint_result.visualize(half_window=2)
  43. expected_output = (
  44. "\033[91m 1|Line 1\033[0m\n"
  45. f" {' ' * lint_result.column}^ ERROR HERE: Start error\n"
  46. " 2|Line 2\n"
  47. " 3|Line 3"
  48. )
  49. assert result == expected_output
  50. def test_visualize_error_at_end(mock_file_content):
  51. lint_result = LintResult(
  52. file='test_file.py', line=20, column=1, message='End error'
  53. )
  54. with patch('builtins.open', mock_open(read_data=mock_file_content)):
  55. result = lint_result.visualize(half_window=2)
  56. expected_output = (
  57. "18|Line 18\n"
  58. "19|Line 19\n"
  59. "\033[91m20|Line 20\033[0m\n"
  60. f" {' ' * lint_result.column}^ ERROR HERE: End error"
  61. )
  62. assert result == expected_output