Exemplo n.º 1
0
def refine_input(status_line: str) -> Dict[str, Any]:
    re_battery_line = re.compile(
        r'^Battery (?P<id>[0-9]+): '
        r'((?P<state>[a-zA-Z]+), (?P<percentage>[0-9]{1,3})%'
        r'(, (?P<time>[0-9:]+)[a-zA-Z ]*$|'
        r', (?P<unavailable>rate information unavailable)$|'
        r', [a-zA-Z \-.]+$|'
        r'$)|'  # end of group state information line 1
        r'design capacity (?P<design_capacity>[0-9]+) mAh, '
        r'last full capacity (?P<full_capacity>[0-9]+) mAh = [0-9]{1,3}%$'
        ')')  # end of group capacity information line 2

    group = re.match(re_battery_line, status_line).groupdict()

    def none_or_convert_to_int(x):
        return None if not x else int(x)

    functions = {
        'id': int,
        'state': lambda x: None
        if not x else State.get_state_according_to_value(x),
        'percentage': none_or_convert_to_int,
        'time': lambda x: None if not x else parse_time(x),
        'unavailable': lambda x: x == "rate information unavailable",
        'design_capacity': none_or_convert_to_int,
        'full_capacity': none_or_convert_to_int
    }

    batteries = {}

    for key, value in group.items():
        batteries[key] = functions[key](value)

    return batteries
def test_refine_input_discharge():
    status_line = 'Battery 1: Discharging, 88%, 00:50:14 remaining'
    expected = {
        'id': 1,
        'state': State.DISCHARGING,
        'percentage': 88,
        'time': parse_time('00:50:14'),
        'unavailable': False,
        'design_capacity': None,
        'full_capacity': None
    }
    assert refine_input(status_line) == expected, "re matching is broken"
def test_refine_input_charge():
    status_line = 'Battery 2: Charging, 88%, 00:09:24 until charged'
    expected = {
        'id': 2,
        'state': State.CHARGING,
        'percentage': 88,
        'time': parse_time('00:09:24'),
        'unavailable': False,
        'design_capacity': None,
        'full_capacity': None
    }
    assert refine_input(status_line) == expected, "re matching is broken"
def test_prepare_output_charging_small_any_order():
    sut = [
        {"state": State.UNKNOWN, 'percentage': 0, 'time': None,
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
        {"state": State.CHARGING, 'percentage': 70, 'time': parse_time("01:33:02"),
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
    ]
    expected = [wrap_span_fa(FA_PLUG, "yellow"),
                wrap_span_fa(FA_BATTERY_LIST[1], col=color(35)),
                wrap_span("(01:33)")
                ]
    input_list = []
    prepare_output(sut, [], input_list)
    assert input_list == expected, "output is not according to specifications"
def test_prepare_output_discharging_small_other_order():
    sut = [
        {"state": State.FULL, 'percentage': 100, 'time': None,
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
        {"state": State.UNKNOWN, 'percentage': 0, 'time': None,
         'unavailable': False, 'design_capacity': None, 'full_capacity': None},
        {"state": State.DISCHARGING, 'percentage': 70, 'time': parse_time("01:33:02"),
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
    ]
    expected = [wrap_span_fa(FA_LAPTOP),
                wrap_span_fa(FA_BATTERY_LIST[4], col=color(85)),
                wrap_span("(03:45)"),
                ]
    input_list = []
    prepare_output(sut, [], input_list)
    assert input_list == expected, "output is not according to specifications"
def test_prepare_output_discharging():
    # first battery discharging with 70% and second battery unknown with 0%
    sut = [
        {"state": State.DISCHARGING, 'percentage': 70, 'time': parse_time("01:33:02"),
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
        {"state": State.UNKNOWN, 'percentage': 0, 'time': None,
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
    ]
    expected = [wrap_span_battery_header(1) + wrap_span_fa(FA_LAPTOP) + ' ' + wrap_span_fa(FA_BATTERY_LIST[3]) + ' ',
                wrap_span_battery_header(2) + wrap_span_fa(FA_QUESTION) + ' ' + wrap_span_fa(FA_BATTERY_LIST[0]) + ' ',
                wrap_span("35%", color(35)),
                wrap_span("(01:33)"),
                ]
    input_list = []
    prepare_output(sut, input_list, [])
    assert input_list == expected, "output is not according to specifications"
def test_for_remove_of_battery_bug_second_variant():
    # first battery discharging with 70% and second battery unknown with 0%
    sut = [
        {"state": State.UNKNOWN, 'percentage': 6, 'time': None,
         'unavailable': False, 'design_capacity': 1960, 'full_capacity': 1898},
        {"state": State.DISCHARGING, 'percentage': 0, 'time': None,
         'unavailable': True, 'design_capacity': None, 'full_capacity': None},
        {"state": State.CHARGING, 'percentage': 89, 'time': parse_time('00:54:00'),
         'unavailable': False, 'design_capacity': 2010, 'full_capacity': 1658},
    ]
    expected = [wrap_span_battery_header(1) + wrap_span_fa(FA_QUESTION) + ' ' + wrap_span_fa(FA_BATTERY_LIST[0]) + ' ',
                wrap_span_battery_header(2) + wrap_span_fa(FA_PLUG, col='yellow') + ' ' +
                wrap_span_fa(FA_BATTERY_LIST[4]) + ' ',
                wrap_span("47%", color(47)),
                wrap_span("(00:58)")
                ]
    input_list = []
    prepare_output(sut, input_list, [])
    assert input_list == expected, "output is not according to specifications"
def test_time_parsing(time_input: str, expected: time):
    assert parse_time(time_input) == expected, "time parsing has gone wrong"
Exemplo n.º 9
0
     'design_capacity': None,
     'full_capacity': None
 }, {
     'id': 2,
     'state': State.CHARGING,
     'percentage': 100,
     'time': None,
     'unavailable': False,
     'design_capacity': 1860,
     'full_capacity': 1512
 }]),
 ('charging_from_low_state_with_bug.txt', [{
     'id': 0,
     'state': State.CHARGING,
     'percentage': 76,
     'time': parse_time('00:18:24'),
     'unavailable': False,
     'design_capacity': 1873,
     'full_capacity': 1708
 }, {
     'id': 1,
     'state': State.UNKNOWN,
     'percentage': 0,
     'time': None,
     'unavailable': True,
     'design_capacity': None,
     'full_capacity': None
 }, {
     'id': 2,
     'state': State.UNKNOWN,
     'percentage': 4,