Skip to content
Closed
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
14 changes: 8 additions & 6 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,17 @@ def rect(self) -> Rect:
"""Return a Rect describing the size of the window."""
return LBWH(0, 0, self.width, self.height)

def run(self) -> None:
from typing import Optional

def run(self, view: Optional[View] = None) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the modern View | None notation here instead of Optional and remove the stray import.

"""
Run the event loop.
Run the event loop. Optionally start with a specified view.

After the window has been set up, and the event hooks are in place, this
is usually one of the last commands on the main program. This is a blocking
function starting pyglet's event loop meaning it will start to dispatch
events such as ``on_draw`` and ``on_update``.
Comment on lines -409 to -412
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to remove this part.

Args:
view (Optional[View]): The view to display when starting the run. Defaults to None.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit overkill to repeat the parameter typing here but not a big deal.

"""
if view is not None:
self.show_view(view)
arcade.run()

def close(self) -> None:
Expand Down
6 changes: 5 additions & 1 deletion arcade/window_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def close_window() -> None:
gc.collect()


def run():
def run(view = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing annotation here.

"""
Run the main loop.

Expand All @@ -108,6 +108,10 @@ def run():
"""
window = get_window()

# Show the specific view if provided
if view is not None:
window.show_view(view)

# Used in some unit test
if os.environ.get("ARCADE_TEST"):
window.on_update(1.0 / 60.0)
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/window/test_window_new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
import arcade


class TestView(arcade.View):
Copy link
Member

@einarf einarf Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unit tests needs to use the global test window instead of making a new one or this will be extremely complicated.

Write the tests like this. You will automatically get a test window fixture you can use. Creating new windows are not allowed in our unit tests. You should get a "clean" window for every function.

def test_something(window):
    window.run(..)

As far as I know the ARCADE_TEST environment variable is automatically set when running tests and you can see in the run() function that it simply calls on_update and on_draw once and exits and you get test coverage from checking if they were called.

def __init__(self):
super().__init__()
self.on_show_called = False

def on_show_view(self):
self.on_show_called = True


def test_window_with_view():
# Cria uma janela e uma view para o teste
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window = arcade.Window(width=800, height=600, title="Window Test with View")
view = TestView()

# Executa o método run com a view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window.run(view=view)

# Verifica se a view foi mostrada automaticamente
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

assert window.current_view == view
assert view.on_show_called is True

# Fecha a janela ao finalizar o teste
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window.close()


def test_run_without_view():
# Cria uma janela e chama run sem uma view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window = arcade.Window(width=800, height=600, title="Window Test without View")

# Executa o método run sem uma view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window.run()

# Verifica que não há uma view ativa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

assert window.current_view is None

# Fecha a janela ao finalizar o teste
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-English comment

window.close()