mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-15 06:29:05 +00:00
[LIT] Add JSONMetricValue type to wrap types supported by the json encoder.
Summary: The following types can be encoded and decoded by the json library: `dict`, `list`, `tuple`, `str`, `unicode`, `int`, `long`, `float`, `bool`, `NoneType`. `JSONMetricValue` can be constructed with any of these types, and used as part of Test.Result. This patch also adds a toMetricValue function that converts a value into a MetricValue. Reviewers: ddunbar, EricWF Reviewed By: EricWF Subscribers: cfe-commits, llvm-commits Differential Revision: http://reviews.llvm.org/D6576 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224628 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
from xml.sax.saxutils import escape
|
from xml.sax.saxutils import escape
|
||||||
|
from json import JSONEncoder
|
||||||
|
|
||||||
# Test result codes.
|
# Test result codes.
|
||||||
|
|
||||||
@@ -73,6 +74,41 @@ class RealMetricValue(MetricValue):
|
|||||||
def todata(self):
|
def todata(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
class JSONMetricValue(MetricValue):
|
||||||
|
"""
|
||||||
|
JSONMetricValue is used for types that are representable in the output
|
||||||
|
but that are otherwise uninterpreted.
|
||||||
|
"""
|
||||||
|
def __init__(self, value):
|
||||||
|
# Ensure the value is a serializable by trying to encode it.
|
||||||
|
# WARNING: The value may change before it is encoded again, and may
|
||||||
|
# not be encodable after the change.
|
||||||
|
try:
|
||||||
|
e = JSONEncoder()
|
||||||
|
e.encode(value)
|
||||||
|
except TypeError:
|
||||||
|
raise
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def format(self):
|
||||||
|
return str(self.value)
|
||||||
|
|
||||||
|
def todata(self):
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
def toMetricValue(value):
|
||||||
|
if isinstance(value, MetricValue):
|
||||||
|
return value
|
||||||
|
elif isinstance(value, int) or isinstance(value, long):
|
||||||
|
return IntMetricValue(value)
|
||||||
|
elif isinstance(value, float):
|
||||||
|
return RealMetricValue(value)
|
||||||
|
else:
|
||||||
|
# Try to create a JSONMetricValue and let the constructor throw
|
||||||
|
# if value is not a valid type.
|
||||||
|
return JSONMetricValue(value)
|
||||||
|
|
||||||
|
|
||||||
# Test results.
|
# Test results.
|
||||||
|
|
||||||
class Result(object):
|
class Result(object):
|
||||||
|
Reference in New Issue
Block a user