Skip to content

Commit

Permalink
Handle highlight position absence
Browse files Browse the repository at this point in the history
  • Loading branch information
hermansje committed Aug 11, 2019
1 parent cf9a5a0 commit 39cff17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
19 changes: 12 additions & 7 deletions protowhat/Feedback.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict


class Feedback:
def __init__(self, message, state=None):
self.message = message
Expand All @@ -10,16 +13,17 @@ def __init__(self, message, state=None):
if hasattr(state, "path"):
self.path = state.path

def _highlight_data(self):
def _highlight_data(self) -> Dict[str, int]:
highlight = {}
if self.path:
highlight["path"] = self.path

if hasattr(self.highlight, "get_position"):
highlight.update(self.highlight.get_position())
return highlight
elif hasattr(self.highlight, "first_token") and hasattr(
self.highlight, "last_token"
position = self.highlight.get_position()
if position:
highlight.update(position)
elif getattr(self.highlight, "first_token", None) and getattr(
self.highlight, "last_token", None
):
# used by pythonwhat
# a plugin+register system would be better
Expand All @@ -32,9 +36,10 @@ def _highlight_data(self):
"column_end": self.highlight.last_token.end[1],
}
)
return highlight

def get_highlight_data(self):
return highlight

def get_highlight_data(self) -> Dict[str, int]:
result = None
if self.highlight is not None and not self.highlighting_disabled:
result = self._highlight_data()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,24 @@ class FeedbackState:
}

assert payload == expected_payload


def test_highlighting_path_no_position():
r = Reporter()

class FeedbackState:
highlighting_disabled = False
highlight = Highlight(None)
path = Path("test.py")

f = Feedback("msg", FeedbackState())

payload = r.build_failed_payload(f)

expected_payload = {
"correct": False,
"message": "msg",
"path": "test.py",
}

assert payload == expected_payload

0 comments on commit 39cff17

Please sign in to comment.