aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test_library.py
blob: a8daffc55f484914ab4929c80473f1fa07a87547 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""
Test script to verify SelectUI works as a library.
This doesn't require user interaction - just checks that the API works.
"""

import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))

from selectui import SelectUI


def test_initialization():
    """Test that SelectUI can be initialized with items."""
    items = [
        {"title": "Item 1", "subtitle": "Description 1"},
        {"title": "Item 2", "subtitle": "Description 2"},
    ]

    app = SelectUI(
        items=items,
        oneshot=True,
        title_key="title",
        subtitle_key="subtitle"
    )

    assert app.items == items
    assert app.oneshot
    assert app.title_key == "title"
    assert app.subtitle_key == "subtitle"
    assert app.selected_item is None
    print("✓ Initialization test passed")


def test_custom_keys():
    """Test custom title/subtitle keys."""
    items = [
        {"name": "Alice", "role": "Engineer"},
        {"name": "Bob", "role": "Designer"},
    ]

    app = SelectUI(
        items=items,
        title_key="name",
        subtitle_key="role"
    )

    assert app.title_key == "name"
    assert app.subtitle_key == "role"
    print("✓ Custom keys test passed")


def test_info_key():
    """Test info_key parameter."""
    items = [
        {"title": "Item 1", "year": "2024"},
    ]

    app = SelectUI(
        items=items,
        info_key="year"
    )

    assert app.info_key == "year"
    print("✓ Info key test passed")


def test_events_mode():
    """Test events mode initialization."""
    app = SelectUI(
        items=[],
        events_mode=True
    )

    assert app.events_mode
    print("✓ Events mode test passed")


def test_command_template():
    """Test command template initialization."""
    app = SelectUI(
        items=[],
        command_template="echo {}"
    )

    assert app.command_template == "echo {}"
    assert app.input_mode
    print("✓ Command template test passed")


def test_single_line_mode():
    """Test single-line display (no subtitle)."""
    items = [
        {"filename": "test.py"},
    ]

    app = SelectUI(
        items=items,
        title_key="filename",
        subtitle_key=None
    )

    assert app.subtitle_key is None
    print("✓ Single-line mode test passed")


if __name__ == "__main__":
    print("Running SelectUI library tests...\n")

    try:
        test_initialization()
        test_custom_keys()
        test_info_key()
        test_events_mode()
        test_command_template()
        test_single_line_mode()

        print("\n✅ All tests passed!")
        print("\nSelectUI is ready to use as a library.")
        print("See examples/README.md for usage examples.")

    except AssertionError as e:
        print(f"\n❌ Test failed: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"\n❌ Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)