| 1 |
# Copyright (C) 2008 John Paulett (john -at- paulett.org) |
| 2 |
# Copyright (C) 2009-2018 David Aguilar (davvid -at- gmail.com) |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# This software is licensed as described in the file COPYING, which |
| 6 |
# you should have received as part of this distribution. |
| 7 |
|
| 8 |
import io |
| 9 |
import time |
| 10 |
|
| 11 |
from jsonpickle import util |
| 12 |
|
| 13 |
|
| 14 |
class Thing: |
| 15 |
def __init__(self, name): |
| 16 |
self.name = name |
| 17 |
self.child = None |
| 18 |
|
| 19 |
|
| 20 |
class DictSubclass(dict): |
| 21 |
pass |
| 22 |
|
| 23 |
|
| 24 |
class ListSubclass(list): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class MethodTestClass: |
| 29 |
variable = None |
| 30 |
|
| 31 |
@staticmethod |
| 32 |
def static_method(): |
| 33 |
pass |
| 34 |
|
| 35 |
@classmethod |
| 36 |
def class_method(cls): |
| 37 |
pass |
| 38 |
|
| 39 |
def bound_method(self): |
| 40 |
pass |
| 41 |
|
| 42 |
|
| 43 |
class MethodTestSubclass(MethodTestClass): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
class MethodTestOldStyle: |
| 48 |
def bound_method(self): |
| 49 |
pass |
| 50 |
|
| 51 |
|
| 52 |
def test_is_primitive_int(): |
| 53 |
assert util.is_primitive(0) |
| 54 |
assert util.is_primitive(3) |
| 55 |
assert util.is_primitive(-3) |
| 56 |
|
| 57 |
|
| 58 |
def test_is_primitive_float(): |
| 59 |
assert util.is_primitive(0) |
| 60 |
assert util.is_primitive(3.5) |
| 61 |
assert util.is_primitive(-3.5) |
| 62 |
assert util.is_primitive(float(3)) |
| 63 |
|
| 64 |
|
| 65 |
def test_is_primitive_long(): |
| 66 |
assert util.is_primitive(2**64) |
| 67 |
|
| 68 |
|
| 69 |
def test_is_primitive_bool(): |
| 70 |
assert util.is_primitive(True) |
| 71 |
assert util.is_primitive(False) |
| 72 |
|
| 73 |
|
| 74 |
def test_is_primitive_None(): |
| 75 |
assert util.is_primitive(None) |
| 76 |
|
| 77 |
|
| 78 |
def test_is_primitive_bytes(): |
| 79 |
assert not util.is_primitive(b'hello') |
| 80 |
assert not util.is_primitive(b'foo') |
| 81 |
assert util.is_primitive('foo') |
| 82 |
|
| 83 |
|
| 84 |
def test_is_primitive_unicode(): |
| 85 |
assert util.is_primitive('') |
| 86 |
assert util.is_primitive('hello') |
| 87 |
|
| 88 |
|
| 89 |
def test_is_primitive_list(): |
| 90 |
assert not util.is_primitive([]) |
| 91 |
assert not util.is_primitive([4, 4]) |
| 92 |
|
| 93 |
|
| 94 |
def test_is_primitive_dict(): |
| 95 |
assert not util.is_primitive({'key': 'value'}) |
| 96 |
assert not util.is_primitive({}) |
| 97 |
|
| 98 |
|
| 99 |
def test_is_primitive_tuple(): |
| 100 |
assert not util.is_primitive((1, 3)) |
| 101 |
assert not util.is_primitive((1,)) |
| 102 |
|
| 103 |
|
| 104 |
def test_is_primitive_set(): |
| 105 |
assert not util.is_primitive({1, 3}) |
| 106 |
|
| 107 |
|
| 108 |
def test_is_primitive_object(): |
| 109 |
assert not util.is_primitive(Thing('test')) |
| 110 |
|
| 111 |
|
| 112 |
def test_is_list_list(): |
| 113 |
assert util.is_list([1, 2]) |
| 114 |
|
| 115 |
|
| 116 |
def test_is_list_set(): |
| 117 |
assert util.is_set({1, 2}) |
| 118 |
|
| 119 |
|
| 120 |
def test_is_list_tuple(): |
| 121 |
assert util.is_tuple((1, 2)) |
| 122 |
|
| 123 |
|
| 124 |
def test_is_list_dict(): |
| 125 |
assert not util.is_list({'key': 'value'}) |
| 126 |
assert not util.is_set({'key': 'value'}) |
| 127 |
assert not util.is_tuple({'key': 'value'}) |
| 128 |
|
| 129 |
|
| 130 |
def test_is_list_other(): |
| 131 |
assert not util.is_list(1) |
| 132 |
assert not util.is_set(1) |
| 133 |
assert not util.is_tuple(1) |
| 134 |
|
| 135 |
|
| 136 |
def test_is_sequence_various(): |
| 137 |
assert util.is_sequence([]) |
| 138 |
assert util.is_sequence(tuple()) |
| 139 |
assert util.is_sequence(set()) |
| 140 |
|
| 141 |
|
| 142 |
def test_is_dictionary_dict(): |
| 143 |
assert util.is_dictionary({}) |
| 144 |
|
| 145 |
|
| 146 |
def test_is_dicitonary_sequences(): |
| 147 |
assert not util.is_dictionary([]) |
| 148 |
assert not util.is_dictionary(set()) |
| 149 |
|
| 150 |
|
| 151 |
def test_is_dictionary_tuple(): |
| 152 |
assert not util.is_dictionary(tuple()) |
| 153 |
|
| 154 |
|
| 155 |
def test_is_dictionary_primitive(): |
| 156 |
assert not util.is_dictionary(int()) |
| 157 |
assert not util.is_dictionary(None) |
| 158 |
assert not util.is_dictionary('') |
| 159 |
|
| 160 |
|
| 161 |
def test_is_dictionary_subclass_dict(): |
| 162 |
assert not util.is_dictionary_subclass({}) |
| 163 |
|
| 164 |
|
| 165 |
def test_is_dictionary_subclass_subclass(): |
| 166 |
assert util.is_dictionary_subclass(DictSubclass()) |
| 167 |
|
| 168 |
|
| 169 |
def test_is_sequence_subclass_subclass(): |
| 170 |
assert util.is_sequence_subclass(ListSubclass()) |
| 171 |
|
| 172 |
|
| 173 |
def test_is_sequence_subclass_list(): |
| 174 |
assert not util.is_sequence_subclass([]) |
| 175 |
|
| 176 |
|
| 177 |
def test_is_noncomplex_time_struct(): |
| 178 |
t = time.struct_time('123456789') |
| 179 |
assert util.is_noncomplex(t) |
| 180 |
|
| 181 |
|
| 182 |
def test_is_noncomplex_other(): |
| 183 |
assert not util.is_noncomplex('a') |
| 184 |
|
| 185 |
|
| 186 |
def test_is_function_builtins(): |
| 187 |
assert util.is_function(globals) |
| 188 |
|
| 189 |
|
| 190 |
def test_is_function_lambda(): |
| 191 |
assert util.is_function(lambda: False) |
| 192 |
|
| 193 |
|
| 194 |
def test_is_function_instance_method(): |
| 195 |
class Foo: |
| 196 |
def method(self): |
| 197 |
pass |
| 198 |
|
| 199 |
@staticmethod |
| 200 |
def staticmethod(): |
| 201 |
pass |
| 202 |
|
| 203 |
@classmethod |
| 204 |
def classmethod(cls): |
| 205 |
pass |
| 206 |
|
| 207 |
f = Foo() |
| 208 |
assert util.is_function(f.method) |
| 209 |
assert util.is_function(f.staticmethod) |
| 210 |
assert util.is_function(f.classmethod) |
| 211 |
|
| 212 |
|
| 213 |
def test_itemgetter(): |
| 214 |
expect = '0' |
| 215 |
actual = util.itemgetter((0, 'zero')) |
| 216 |
assert expect == actual |
| 217 |
|
| 218 |
|
| 219 |
def test_has_method(): |
| 220 |
instance = MethodTestClass() |
| 221 |
x = 1 |
| 222 |
has_method = util.has_method |
| 223 |
# no attribute |
| 224 |
assert not has_method(instance, 'foo') |
| 225 |
# builtin method type |
| 226 |
assert not has_method(int, '__getnewargs__') |
| 227 |
assert has_method(x, '__getnewargs__') |
| 228 |
# staticmethod |
| 229 |
assert has_method(instance, 'static_method') |
| 230 |
assert has_method(MethodTestClass, 'static_method') |
| 231 |
# not a method |
| 232 |
assert not has_method(instance, 'variable') |
| 233 |
assert not has_method(MethodTestClass, 'variable') |
| 234 |
# classmethod |
| 235 |
assert has_method(instance, 'class_method') |
| 236 |
assert has_method(MethodTestClass, 'class_method') |
| 237 |
# bound method |
| 238 |
assert has_method(instance, 'bound_method') |
| 239 |
assert not has_method(MethodTestClass, 'bound_method') |
| 240 |
# subclass bound method |
| 241 |
sub_instance = MethodTestSubclass() |
| 242 |
assert has_method(sub_instance, 'bound_method') |
| 243 |
assert not has_method(MethodTestSubclass, 'bound_method') |
| 244 |
# old style object |
| 245 |
old_instance = MethodTestOldStyle() |
| 246 |
assert has_method(old_instance, 'bound_method') |
| 247 |
assert not has_method(MethodTestOldStyle, 'bound_method') |
| 248 |
|
| 249 |
|
| 250 |
def test_importable_name(): |
| 251 |
func_being_tested_obj = util.importable_name |
| 252 |
io_method_obj = io.BytesIO(b'bytes').readline |
| 253 |
assert ( |
| 254 |
util.importable_name(func_being_tested_obj) == 'jsonpickle.util.importable_name' |
| 255 |
) |
| 256 |
assert util.importable_name(io_method_obj) == '_io.BytesIO.readline' |
| 257 |
assert util.importable_name(dict) == 'builtins.dict' |