Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting str type properties + add tests (#1779) #2046

Merged
merged 9 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1779.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
macOS widget methods that return strings are now guaranteed to return strings, rather than native Objective C string objects.
4 changes: 2 additions & 2 deletions cocoa/src/toga_cocoa/widgets/multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def create(self):
self.add_constraints()

def get_placeholder(self):
return self.native_text.placeholderString
return str(self.native_text.placeholderString)

def set_placeholder(self, value):
self.native_text.placeholderString = value
Expand All @@ -73,7 +73,7 @@ def set_readonly(self, value):
self.native_text.editable = not value

def get_value(self):
return self.native_text.string
return str(self.native_text.string)

def set_value(self, value):
self.native_text.string = value
Expand Down
2 changes: 1 addition & 1 deletion cocoa/src/toga_cocoa/widgets/textinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def set_readonly(self, value):
self.native.selectable = True

def get_placeholder(self):
return self.native.cell.placeholderString
return str(self.native.cell.placeholderString)

def set_placeholder(self, value):
self.native.cell.placeholderString = value
Expand Down
4 changes: 2 additions & 2 deletions iOS/src/toga_iOS/widgets/multilinetextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def constrain_placeholder_label(self):
)

def get_placeholder(self):
return self.placeholder_label.text
return str(self.placeholder_label.text)

def set_placeholder(self, value):
self.placeholder_label.text = value
Expand All @@ -129,7 +129,7 @@ def set_readonly(self, value):
self.native.editable = not value

def get_value(self):
return self.native.text
return str(self.native.text)

def set_value(self, value):
self.native.text = value
Expand Down
3 changes: 3 additions & 0 deletions testbed/tests/widgets/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ async def test_text(widget, probe):
widget.text = text
await probe.redraw(f"Widget text should be {str(text)!r}")

assert isinstance(widget.text, str)
assert widget.text == str(text)
assert probe.text == str(text)

Expand All @@ -118,6 +119,7 @@ async def test_text_value(widget, probe):
widget.value = text
await probe.redraw(f"Widget value should be {str(text)!r}")

assert isinstance(widget.value, str)
assert widget.value == str(text)
assert probe.value == str(text)

Expand All @@ -129,6 +131,7 @@ async def test_placeholder(widget, probe):
widget.value = "Hello"
widget.placeholder = "placeholder"
await probe.redraw("Widget placeholder should not be visible")
assert isinstance(widget.placeholder, str)
assert widget.value == "Hello"
assert widget.placeholder == "placeholder"
assert probe.value == "Hello"
Expand Down
1 change: 1 addition & 0 deletions testbed/tests/widgets/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async def test_text(widget, probe):
await probe.redraw(f"Button text should be {text}")

# Text after a newline will be stripped.
assert isinstance(widget.text, str)
expected = str(text).split("\n")[0]
assert widget.text == expected
assert probe.text == expected
Expand Down
1 change: 1 addition & 0 deletions testbed/tests/widgets/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async def test_text(widget, probe):

# Text after a newline will be stripped.
expected = str(text).split("\n")[0]
assert isinstance(widget.text, str)
assert widget.text == expected
assert probe.text == expected
assert probe.height == initial_height
Expand Down
Loading