blob: 290e1612ad8218a93f6f48e88ad791336c30f03e [file] [log] [blame]
Paul Kehrer874445a2016-12-13 11:09:35 -06001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
Alex Gaynor0c9aed92017-07-08 21:50:01 -04007import pytest
8
Paul Kehrer874445a2016-12-13 11:09:35 -06009from cryptography import utils
10
11
12def test_int_from_bytes_bytearray():
13 assert utils.int_from_bytes(bytearray(b"\x02\x10"), "big") == 528
Alex Gaynor0c9aed92017-07-08 21:50:01 -040014
15
16class TestCachedProperty(object):
17 def test_simple(self):
18 accesses = []
19
20 class T(object):
21 @utils.cached_property
22 def t(self):
23 accesses.append(None)
24 return 14
25
26 assert T.t
27 t = T()
28 assert t.t == 14
29 assert len(accesses) == 1
30 assert t.t == 14
31 assert len(accesses) == 1
32
33 t = T()
34 assert t.t == 14
35 assert len(accesses) == 2
36 assert t.t == 14
37 assert len(accesses) == 2
38
39 def test_set(self):
40 accesses = []
41
42 class T(object):
43 @utils.cached_property
44 def t(self):
45 accesses.append(None)
46 return 14
47
48 t = T()
49 with pytest.raises(AttributeError):
50 t.t = None
51 assert len(accesses) == 0
52 assert t.t == 14
53 assert len(accesses) == 1
54 with pytest.raises(AttributeError):
55 t.t = None
56 assert len(accesses) == 1
57 assert t.t == 14
58 assert len(accesses) == 1