generalized set_using_dict

This commit is contained in:
Erik Frojdh
2020-09-24 11:01:51 +02:00
parent 97fea10ee2
commit 101f029eef
3 changed files with 171 additions and 54 deletions

View File

@ -167,10 +167,12 @@ def test_make_ip_from_list():
arg = ["192.168.1.1", "192.168.1.2", "127.0.0.1"]
assert make_ip(arg) == [IpAddr(a) for a in arg]
def test_make_ip_from_tuple():
arg = ("127.0.0.1")
assert make_ip(arg) == (IpAddr(arg))
def test_make_mac_from_dict():
arg = {6: "84:a9:aa:24:32:88", 12: "84:a9:3e:24:32:aa"}
res = make_mac(arg)
@ -198,10 +200,92 @@ def test_make_mac_from_tuple():
assert make_mac(arg) == (MacAddr("84:a9:aa:24:32:88"),
MacAddr("84:a9:3e:24:32:aa"))
def test_make_path_from_str():
assert make_path("/") == Path("/")
assert make_path("/home") == Path("/home")
def test_make_path_from_list():
arg = ["/", "/home", "/another/path"]
assert make_path(arg) == [Path(p) for p in arg]
assert make_path(arg) == [Path(p) for p in arg]
def test_make_string_path_from_str():
arg = "/path/to/something"
assert make_string_path(arg) == arg
def test_make_string_path_from_Path():
s = "/path/to/something"
arg = Path(s)
assert make_string_path(arg) == s
def test_make_string_path_from_list_of_Path_and_string():
args = ["/path/to", Path("/something/something"), "else/"]
assert make_string_path(args) == [
"/path/to", "/something/something", "else/"
]
def test_make_string_path_from_Path_list():
s = "/path/to/something"
arg = [Path(s)]
assert make_string_path(arg) == [s]
def test_make_string_path_from_dict():
args = {0: "/path/to/something", 1: Path("/something/else")}
assert make_string_path(args) == {
0: "/path/to/something",
1: "/something/else"
}
class DummyClass:
def __init__(self):
self.args = []
def call(self, *args):
"""Normal type call in slsdet where list of detectors is passed"""
self.args.append(args)
def call_int_id(self, *args):
"""call where det_is is an int"""
*args, i = args
if isinstance(i, list):
raise TypeError
self.args.append((*args, i))
def test_set_using_dict_single_int():
c = DummyClass()
set_using_dict(c.call, 5)
assert c.args == [(5,)]
def test_set_using_dict_two_ints():
c = DummyClass()
set_using_dict(c.call, 1, 2)
assert c.args == [(1,2)]
def test_set_using_dict_passing_dict():
c = DummyClass()
set_using_dict(c.call, {0: 5, 8:3, 9:7})
assert len(c.args) == 3
assert c.args == [(5, [0]), (3, [8]), (7, [9])]
def test_set_using_dict_calling_int_id():
c = DummyClass()
set_using_dict(c.call_int_id, {0: "hej", 8:3, 9:7})
assert len(c.args) == 3
assert c.args == [("hej", 0), (3, 8), (7, 9)]
def test_set_using_dict_pass_multiple_args():
c = DummyClass()
set_using_dict(c.call, "a", "b", "c")
assert len(c.args) == 1
assert c.args == [("a", "b", "c")]
def test_set_using_dict_passing_dict_with_multiple_args():
c = DummyClass()
set_using_dict(c.call, {0: ("a", "b"), 1: ("c", "d")})
assert c.args == [("a", "b", [0]), ("c", "d", [1])]