[Piglit] [PATCH 07/13] results.py: Replace JSONEncoder subclass with default method
Ilia Mirkin
imirkin at alum.mit.edu
Sat Jun 21 06:49:56 PDT 2014
On Sat, Jun 21, 2014 at 8:06 AM, Dylan Baker <baker.dylan.c at gmail.com> wrote:
> Both methods are equally viable solution when using the builtin json
> module, but using defaults is superior when using simplejson (a C based
> implementation of the json module providing an identical interface but
> vastly better performance) which can be used with piglit if it's
> installed.
>
> Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
> ---
> framework/results.py | 21 +++++++++++++--------
> framework/tests/dmesg_tests.py | 5 ++---
> 2 files changed, 15 insertions(+), 11 deletions(-)
>
> diff --git a/framework/results.py b/framework/results.py
> index c9831f7..7762d39 100644
> --- a/framework/results.py
> +++ b/framework/results.py
> @@ -40,13 +40,17 @@ __all__ = [
> ]
>
>
> -class PiglitJSONEncoder(json.JSONEncoder):
> - def default(self, o):
> - if isinstance(o, status.Status):
> - return str(o)
> - elif isinstance(o, set):
> - return list(o)
> - return json.JSONEncoder.default(self, o)
> +def _piglit_encoder(obj):
> + """ Encoder for piglit that can transform additional classes into json
> +
> + Adds support for status.Status objects and for set() instances
> +
> + """
> + if isinstance(obj, status.Status):
> + return str(obj)
> + elif isinstance(obj, set):
> + return list(obj)
> + return obj
>
>
> class JSONWriter:
> @@ -98,7 +102,8 @@ class JSONWriter:
> self.file = file
> self.__indent_level = 0
> self.__inhibit_next_indent = False
> - self.__encoder = PiglitJSONEncoder(indent=self.INDENT)
> + self.__encoder = json.JSONEncoder(indent=self.INDENT,
> + default=_piglit_encoder)
>
> # self.__is_collection_empty
> #
> diff --git a/framework/tests/dmesg_tests.py b/framework/tests/dmesg_tests.py
> index ff70e2d..785edb8 100644
> --- a/framework/tests/dmesg_tests.py
> +++ b/framework/tests/dmesg_tests.py
> @@ -27,7 +27,7 @@ import re
> import nose.tools as nt
> from nose.plugins.skip import SkipTest
> from framework.dmesg import DummyDmesg, LinuxDmesg, get_dmesg
> -from framework.results import TestResult, PiglitJSONEncoder
> +from framework.results import TestResult, _piglit_encoder
> from framework.exectest import PiglitTest
> from framework.gleantest import GleanTest
> from framework.shader_test import ShaderTest
> @@ -258,9 +258,8 @@ def test_update_result_add_dmesg():
>
> def test_json_serialize_updated_result():
> """ Test that a TestResult that has been updated is json serializable """
> - encoder = PiglitJSONEncoder()
> result = test_update_result_add_dmesg()
> - encoded = encoder.encode(result)
> + _piglit_encoder(result)
This no longer tests that it's json-serializable. It just tests that
the function doesn't crash. You should instantiate the json encoder
here and serialize (or something else to the same effect).
Alternatively you could just assert that it returns something that's
isinstance('str').
>
>
> def test_testclasses_dmesg():
> --
> 2.0.0
>
> _______________________________________________
> Piglit mailing list
> Piglit at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/piglit
More information about the Piglit
mailing list