| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from pyocd.core.options_manager import OptionsManager |
| 20 |
from pyocd.core.options import (OPTIONS_ALIASES, OPTIONS_INFO) |
| 21 |
|
| 22 |
@pytest.fixture(scope='function') |
| 23 |
def mgr(): |
| 24 |
return OptionsManager() |
| 25 |
|
| 26 |
@pytest.fixture(scope='function') |
| 27 |
def layer1(): |
| 28 |
return { |
| 29 |
'foo': 1, |
| 30 |
'bar': 2, |
| 31 |
'baz': 3, |
| 32 |
'auto_unlock': False, |
| 33 |
} |
| 34 |
|
| 35 |
@pytest.fixture(scope='function') |
| 36 |
def layer2(): |
| 37 |
return { |
| 38 |
'baz': 33, |
| 39 |
'dogcow': 777, |
| 40 |
} |
| 41 |
|
| 42 |
class TestOptionsManager(object): |
| 43 |
def test_defaults(self, mgr): |
| 44 |
assert mgr.get('auto_unlock') == OPTIONS_INFO['auto_unlock'].default |
| 45 |
assert mgr['auto_unlock'] == OPTIONS_INFO['auto_unlock'].default |
| 46 |
assert 'auto_unlock' not in mgr |
| 47 |
assert mgr.get_default('auto_unlock') == OPTIONS_INFO['auto_unlock'].default |
| 48 |
|
| 49 |
def test_a(self, mgr, layer1): |
| 50 |
mgr.add_front(layer1) |
| 51 |
assert 'auto_unlock' in mgr |
| 52 |
assert mgr.get('auto_unlock') == False |
| 53 |
|
| 54 |
def test_b(self, mgr, layer1): |
| 55 |
mgr.add_front(layer1) |
| 56 |
mgr.add_front({'auto_unlock': True}) |
| 57 |
assert 'auto_unlock' in mgr |
| 58 |
assert mgr.get('auto_unlock') == True |
| 59 |
|
| 60 |
def test_c(self, mgr, layer1): |
| 61 |
mgr.add_front(layer1) |
| 62 |
mgr.add_back({'auto_unlock': True}) |
| 63 |
assert 'auto_unlock' in mgr |
| 64 |
assert mgr.get('auto_unlock') == False |
| 65 |
|
| 66 |
def test_none_value(self, mgr): |
| 67 |
mgr.add_back({'auto_unlock': None}) |
| 68 |
assert 'auto_unlock' not in mgr |
| 69 |
assert mgr.get('auto_unlock') == True |
| 70 |
|
| 71 |
def test_convert_double_underscore(self, mgr): |
| 72 |
mgr.add_back({'debug__traceback': False}) |
| 73 |
assert 'debug.traceback' in mgr |
| 74 |
assert mgr.get('debug.traceback') == False |
| 75 |
|
| 76 |
def test_option_alias(self, mgr): |
| 77 |
mgr.add_back({'telnet_mode': 'file'}) |
| 78 |
assert 'stdio_mode' in mgr |
| 79 |
assert 'telnet_mode' not in mgr |
| 80 |
assert mgr.get('stdio_mode') == 'file' |
| 81 |
assert mgr.get('telnet_mode') is None |
| 82 |
assert mgr.get_default('telnet_mode') is None |
| 83 |
|
| 84 |
def test_option_alias_layer_priority(self, mgr): |
| 85 |
mgr.add_back({'stdio_mode': 'server'}) |
| 86 |
mgr.add_front({'telnet_mode': 'file'}) |
| 87 |
assert mgr.get('stdio_mode') == 'file' |
| 88 |
|
| 89 |
def test_option_alias_same_layer_order(self, mgr): |
| 90 |
mgr.add_back({'telnet_mode': 'file', 'stdio_mode': 'console'}) |
| 91 |
assert mgr.get('stdio_mode') == 'console' |
| 92 |
|
| 93 |
mgr = OptionsManager() |
| 94 |
mgr.add_back({'stdio_mode': 'console', 'telnet_mode': 'file'}) |
| 95 |
assert mgr.get('stdio_mode') == 'file' |
| 96 |
|
| 97 |
def test_legacy_aliases_not_registered_as_options(self): |
| 98 |
assert not any(name in OPTIONS_INFO for name in OPTIONS_ALIASES) |
| 99 |
|
| 100 |
def test_set(self, mgr, layer1): |
| 101 |
mgr.add_front(layer1) |
| 102 |
mgr.set('buzz', 1234) |
| 103 |
assert mgr['buzz'] == 1234 |
| 104 |
mgr.add_front({'buzz': 4321}) |
| 105 |
assert mgr.get('buzz') == 4321 |
| 106 |
|
| 107 |
def test_update(self, mgr, layer1, layer2): |
| 108 |
mgr.add_front(layer1) |
| 109 |
mgr.add_front(layer2) |
| 110 |
mgr.update({'foo': 888, 'debug__traceback': False}) |
| 111 |
assert mgr['foo'] == 888 |
| 112 |
assert mgr.get('debug.traceback') == False |
| 113 |
|
| 114 |
def test_notify_set(self, mgr, layer1): |
| 115 |
mgr.add_front(layer1) |
| 116 |
flag = [False] |
| 117 |
def cb(note): |
| 118 |
flag[0] = True |
| 119 |
assert note.event == 'foo' |
| 120 |
assert note.source == mgr |
| 121 |
assert note.data.new_value == 100 and note.data.old_value == 1 |
| 122 |
mgr.subscribe(cb, 'foo') |
| 123 |
mgr.set('foo', 100) |
| 124 |
assert flag[0] == True |
| 125 |
|
| 126 |
def test_notify_layer(self, mgr, layer1, layer2): |
| 127 |
mgr.add_front(layer1) |
| 128 |
flag = [False] |
| 129 |
def cb(note): |
| 130 |
flag[0] = True |
| 131 |
assert note.event == 'baz' |
| 132 |
assert note.source == mgr |
| 133 |
assert note.data.new_value == 33 and note.data.old_value == 3 |
| 134 |
mgr.subscribe(cb, 'baz') |
| 135 |
mgr.add_front(layer2) |
| 136 |
assert flag[0] == True |
| 137 |
|
| 138 |
def test_notify_back_layer(self, mgr, layer1, layer2): |
| 139 |
mgr.add_front(layer1) |
| 140 |
flag = [False] |
| 141 |
def cb(note): |
| 142 |
flag[0] = True |
| 143 |
mgr.subscribe(cb, 'baz') |
| 144 |
mgr.add_back(layer2) |
| 145 |
assert flag[0] == False |
| 146 |
|
| 147 |
|