1# SPDX-License-Identifier: GPL-2.0
2# Copyright (C) 2020 Sean Anderson
3
4import pytest
5
6@pytest.mark.buildconfigspec('cmd_dm')
7def test_dm_compat(u_boot_console):
8    """Test that each driver in `dm tree` is also listed in `dm compat`."""
9    response = u_boot_console.run_command('dm tree')
10    driver_index = response.find('Driver')
11    assert driver_index != -1
12    drivers = (line[driver_index:].split()[0]
13               for line in response[:-1].split('\n')[2:])
14
15    response = u_boot_console.run_command('dm compat')
16    for driver in drivers:
17        assert driver in response
18
19    # check sorting - output looks something like this:
20    #  testacpi      0  [   ]   testacpi_drv          |-- acpi-test
21    #  testacpi      1  [   ]   testacpi_drv          |   `-- child
22    #  pci_emul_p    1  [   ]   pci_emul_parent_drv   |-- pci-emul2
23    #  pci_emul      5  [   ]   sandbox_swap_case_em  |   `-- emul2@1f,0
24
25    # The number of '|   ' and '--' matches indicate the indent level. We start
26    # checking sorting only after UCLASS_AXI_EMUL after which the names should
27    # be sorted.
28
29    response = u_boot_console.run_command('dm tree -s')
30    lines = response.split('\n')[2:]
31    stack = []   # holds where we were up to at the previous indent level
32    prev = ''    # uclass name of previous line
33    start = False
34    for line in lines:
35        indent = line.count('|   ') + ('--' in line)
36        cur = line.split()[0]
37        if not start:
38            if cur != 'axi_emul':
39                continue
40            start = True
41
42        # Handle going up or down an indent level
43        if indent > len(stack):
44            stack.append(prev)
45            prev = ''
46        elif indent < len(stack):
47            prev = stack.pop()
48
49        # Check that the current uclass name is not alphabetically before the
50        # previous one
51        if 'emul' not in cur and cur < prev:
52            print('indent', cur >= prev, indent, prev, cur, stack)
53            assert cur >= prev
54            prev = cur
55
56
57@pytest.mark.buildconfigspec('cmd_dm')
58def test_dm_drivers(u_boot_console):
59    """Test that each driver in `dm compat` is also listed in `dm drivers`."""
60    response = u_boot_console.run_command('dm compat')
61    drivers = (line[:20].rstrip() for line in response[:-1].split('\n')[2:])
62    response = u_boot_console.run_command('dm drivers')
63    for driver in drivers:
64        assert driver in response
65
66@pytest.mark.buildconfigspec('cmd_dm')
67def test_dm_static(u_boot_console):
68    """Test that each driver in `dm static` is also listed in `dm drivers`."""
69    response = u_boot_console.run_command('dm static')
70    drivers = (line[:25].rstrip() for line in response[:-1].split('\n')[2:])
71    response = u_boot_console.run_command('dm drivers')
72    for driver in drivers:
73        assert driver in response
74
75@pytest.mark.buildconfigspec("cmd_dm")
76def test_dm_uclass(u_boot_console):
77    response = u_boot_console.run_command("dm uclass")
78
79@pytest.mark.buildconfigspec("cmd_dm")
80def test_dm_devres(u_boot_console):
81    response = u_boot_console.run_command("dm devres")
82