Repository: servo/servo
File: python/servo/devtools_tests.py (Python)

Code before:
                for resource in data["array"]:
                    if resource[0] != "console-message":
                        continue
                    evaluation_result.set_result(resource[1][0])
                    return


            # Listen for console messages
            devtools.client.add_event_listener(
                devtools.watcher.actor_id, Events.Watcher.RESOURCES_AVAILABLE_ARRAY, on_resource_available
            )

            devtools.client.add_event_listener(
                devtools.targets[0]["actor"], Events.Watcher.RESOURCES_AVAILABLE_ARRAY, on_resource_available
            )

            # Run the console.log statements
            console.evaluate_js_async("log_objects();")
            result = evaluation_result.result(1)["arguments"][0]

            # Run assertions on the result
            self.assertEquals(result["ownPropertyLength"], 3)

            preview = result["preview"]
            self.assertEquals(preview["kind"], "Object")
            self.assertEquals(preview["ownPropertiesLength"], 3)

            def assert_property_descriptor_equals(actual_descriptor, expected_descriptor):
                for key, value in expected_descriptor.items():
                    self.assertEquals(
                        actual_descriptor[key],
                        value,
                        f"Incorrect value for {key}, expected {value} got {actual_descriptor[key]}"
                    )

            assert_property_descriptor_equals(
                preview["ownProperties"]["foo"],
                {"configurable": True, "enumerable": True, "value": 1, "writable": True}
            )
            assert_property_descriptor_equals(
                preview["ownProperties"]["bar"],
                {"configurable": True, "enumerable": False, "value": "servo", "writable": True}
            )
            assert_property_descriptor_equals(
                preview["ownProperties"]["baz"],
                # TODO: The boolean value here should not be a string! That's a bug in our
                # devtools implementation.
                {"configurable": False, "enumerable": True, "value": "true", "writable": True}
            )



Review comment (refactor):
Its likely possible to write this as a reusable assert-method for other tests but lets keep it simple for now. This is the first test for `console.log`.

Code after:
                for resource in data["array"]:
                    if resource[0] != "console-message":
                        continue
                    evaluation_result.set_result(resource[1][0])
                    return

            # Listen for console messages
            devtools.client.add_event_listener(
                devtools.watcher.actor_id, Events.Watcher.RESOURCES_AVAILABLE_ARRAY, on_resource_available
            )

            devtools.client.add_event_listener(
                devtools.targets[0]["actor"], Events.Watcher.RESOURCES_AVAILABLE_ARRAY, on_resource_available
            )

            # Run the console.log statements
            console.evaluate_js_async("log_object();")
            result = evaluation_result.result(1)["arguments"][0]

            # Run assertions on the result
            self.assertEquals(result["ownPropertyLength"], 3)

            preview = result["preview"]
            self.assertEquals(preview["kind"], "Object")
            self.assertEquals(preview["ownPropertiesLength"], 3)

            def assert_property_descriptor_equals(actual_descriptor, expected_descriptor):
                for key, value in expected_descriptor.items():
                    self.assertEquals(
                        actual_descriptor[key],
                        value,
                        f"Incorrect value for {key}, expected {value} got {actual_descriptor[key]}",
                    )

            assert_property_descriptor_equals(
                preview["ownProperties"]["foo"],
                {"configurable": True, "enumerable": True, "value": 1, "writable": True},
            )
            assert_property_descriptor_equals(
                preview["ownProperties"]["bar"],
                {"configurable": True, "enumerable": False, "value": "servo", "writable": True},
            )
            assert_property_descriptor_equals(
                preview["ownProperties"]["baz"],
                # TODO: The boolean value here should not be a string! That's a bug in our
                # devtools implementation.
                {"configurable": False, "enumerable": True, "value": "true", "writable": True},
            )

    # Sets `base_url` and `web_server` and `web_server_thread`.
    @classmethod