Skip to content
This repository was archived by the owner on Apr 4, 2024. It is now read-only.

Commit 32651b6

Browse files
committedMar 11, 2024
Push WriteTracker and RecordCall_test
1 parent 6c28563 commit 32651b6

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from typing import List, Optional
2+
from selfie_lib.CommentTracker import SnapshotFileLayout
3+
import inspect
4+
5+
6+
class CallLocation:
7+
def __init__(self, file_name: Optional[str], line: int):
8+
self._file_name = file_name
9+
self._line = line
10+
11+
@property
12+
def file_name(self) -> Optional[str]:
13+
return self._file_name
14+
15+
@property
16+
def line(self) -> int:
17+
return self._line
18+
19+
def with_line(self, line: int) -> "CallLocation":
20+
return CallLocation(self._file_name, line)
21+
22+
def ide_link(self, layout: "SnapshotFileLayout") -> str:
23+
return f"File: {self._file_name}, Line: {self._line}"
24+
25+
def same_path_as(self, other: "CallLocation") -> bool:
26+
if not isinstance(other, CallLocation):
27+
return False
28+
return self._file_name == other.file_name
29+
30+
def source_filename_without_extension(self) -> str:
31+
if self._file_name is not None:
32+
return self._file_name.rsplit(".", 1)[0]
33+
return ""
34+
35+
def __lt__(self, other) -> bool:
36+
if not isinstance(other, CallLocation):
37+
return NotImplemented
38+
return (self._file_name, self._line) < (other.file_name, other.line)
39+
40+
def __eq__(self, other) -> bool:
41+
if not isinstance(other, CallLocation):
42+
return NotImplemented
43+
return (self._file_name, self._line) == (other.file_name, other.line)
44+
45+
46+
class CallStack:
47+
def __init__(self, location: CallLocation, rest_of_stack: List[CallLocation]):
48+
self.location = location
49+
self.rest_of_stack = rest_of_stack
50+
51+
def ide_link(self, layout: "SnapshotFileLayout") -> str:
52+
links = [self.location.ide_link(layout)] + [
53+
loc.ide_link(layout) for loc in self.rest_of_stack
54+
]
55+
return "\n".join(links)
56+
57+
58+
def recordCall(callerFileOnly: bool = False) -> CallStack:
59+
stack_frames = inspect.stack()[1:]
60+
61+
if callerFileOnly:
62+
caller_file = stack_frames[0].filename
63+
stack_frames = [
64+
frame for frame in stack_frames if frame.filename == caller_file
65+
]
66+
67+
call_locations = [
68+
CallLocation(frame.filename, frame.lineno) for frame in stack_frames
69+
]
70+
71+
location = call_locations[0]
72+
rest_of_stack = call_locations[1:]
73+
74+
return CallStack(location, rest_of_stack)
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from unittest.mock import Mock
2+
from selfie_lib.WriteTracker import CallLocation, CallStack, recordCall
3+
4+
5+
def test_call_location_ide_link():
6+
layout = Mock()
7+
location = CallLocation(file_name="example.py", line=10)
8+
expected_link = "File: example.py, Line: 10"
9+
10+
assert location.ide_link(layout) == expected_link
11+
12+
13+
def test_call_stack_ide_link():
14+
layout = Mock()
15+
location1 = CallLocation(file_name="example1.py", line=10)
16+
location2 = CallLocation(file_name="example2.py", line=20)
17+
call_stack = CallStack(location=location1, rest_of_stack=[location2])
18+
19+
expected_links = "File: example1.py, Line: 10\nFile: example2.py, Line: 20"
20+
assert call_stack.ide_link(layout) == expected_links
21+
22+
23+
def test_record_call_with_caller_file_only_false():
24+
call_stack = recordCall(False)
25+
assert (
26+
len(call_stack.rest_of_stack) > 0
27+
), "Expected the rest of stack to contain more than one CallLocation"
28+
29+
30+
def test_record_call_with_caller_file_only_true():
31+
call_stack = recordCall(True)
32+
assert (
33+
len(call_stack.rest_of_stack) >= 0
34+
), "Expected the rest of stack to potentially contain only the caller's file location"

0 commit comments

Comments
 (0)
This repository has been archived.