1+ # MIT License
2+ #
3+ # Copyright (c) 2025 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
4+ #
5+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6+ # of this software and associated documentation files (the "Software"), to deal
7+ # in the Software without restriction, including without limitation the rights
8+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ # copies of the Software, and to permit persons to whom the Software is
10+ # furnished to do so, subject to the following conditions:
11+ #
12+ # The above copyright notice and this permission notice shall be included in all
13+ # copies or substantial portions of the Software.
14+ #
15+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ # SOFTWARE.
22+
23+ import sys , os
24+ import pytest
25+ from typing import Union , Optional
26+
27+ sys .path .append (os .path .join (sys .path [0 ],'../../libs/pyTermTk' ))
28+
29+ import TermTk as ttk
30+
31+ def test_slots_stringType ():
32+ signal1 = ttk .pyTTkSignal (str )
33+ signal2 = ttk .pyTTkSignal (ttk .TTkString )
34+ signal3 = ttk .pyTTkSignal (ttk .TTkStringType )
35+
36+ @ttk .pyTTkSlot (str )
37+ def _test_slot1 (txt :str ):
38+ print ("slot 1" , txt )
39+
40+ @ttk .pyTTkSlot (ttk .TTkString )
41+ def _test_slot2 (txt :ttk .TTkString ):
42+ print ("slot 2" , txt )
43+
44+ @ttk .pyTTkSlot (ttk .TTkStringType )
45+ def _test_slot3 (txt :ttk .TTkStringType ):
46+ print ("slot 3" , txt )
47+
48+ signal1 .connect (_test_slot1 )
49+ signal2 .connect (_test_slot2 )
50+ signal3 .connect (_test_slot3 )
51+
52+ signal1 .connect (_test_slot3 )
53+ signal2 .connect (_test_slot3 )
54+
55+ with pytest .raises (TypeError ):
56+ signal1 .connect (_test_slot2 )
57+ with pytest .raises (TypeError ):
58+ signal2 .connect (_test_slot1 )
59+ with pytest .raises (TypeError ):
60+ signal3 .connect (_test_slot1 )
61+ with pytest .raises (TypeError ):
62+ signal3 .connect (_test_slot2 )
63+
64+ print ('OKKK' )
65+
66+ signal1 .emit ('Eugenio1' )
67+ signal2 .emit ('Eugenio2' )
68+ signal3 .emit ('Eugenio3' )
69+
70+ def test_slots_unionType ():
71+ t1 = ttk .TTkWidget
72+ t2 = ttk .TTkContainer
73+ t3 = ttk .TTkFrame
74+ # TTkContainer extends TTkWidget
75+ # TTkWindow extends TTkFrame
76+ # TTkFileButtonPicker extends TTkButton
77+ t4 = Union [ttk .TTkFrame , ttk .TTkButton ]
78+ t5 = Union [ttk .TTkWindow , ttk .TTkFileButtonPicker ]
79+
80+ signal1 = ttk .pyTTkSignal (t1 )
81+ signal2 = ttk .pyTTkSignal (t2 )
82+ signal3 = ttk .pyTTkSignal (t3 )
83+ signal4 = ttk .pyTTkSignal (t4 )
84+ signal5 = ttk .pyTTkSignal (t5 )
85+
86+ @ttk .pyTTkSlot (t1 )
87+ def _test_slot1 (_ ): pass
88+ @ttk .pyTTkSlot (t2 )
89+ def _test_slot2 (_ ): pass
90+ @ttk .pyTTkSlot (t3 )
91+ def _test_slot3 (_ ): pass
92+ @ttk .pyTTkSlot (t4 )
93+ def _test_slot4 (_ ): pass
94+ @ttk .pyTTkSlot (t5 )
95+ def _test_slot5 (_ ): pass
96+
97+ signal1 .connect (_test_slot1 )
98+ signal1 .connect (_test_slot2 )
99+ signal1 .connect (_test_slot3 )
100+ signal1 .connect (_test_slot4 )
101+ signal1 .connect (_test_slot5 )
102+
103+ signal2 .connect (_test_slot2 )
104+ signal2 .connect (_test_slot3 )
105+ signal2 .connect (_test_slot4 )
106+ signal2 .connect (_test_slot5 )
107+
108+ signal3 .connect (_test_slot3 )
109+ signal3 .connect (_test_slot4 )
110+ signal3 .connect (_test_slot5 )
111+
112+ signal4 .connect (_test_slot4 )
113+ signal4 .connect (_test_slot5 )
114+
115+ signal5 .connect (_test_slot5 )
116+
117+ with pytest .raises (TypeError ):
118+ signal5 .connect (_test_slot4 )
119+ with pytest .raises (TypeError ):
120+ signal5 .connect (_test_slot1 )
121+
122+ with pytest .raises (TypeError ):
123+ @ttk .pyTTkSlot (float )
124+ def _slot (_ ): pass
125+ ttk .pyTTkSignal (int ).connect (_slot )
126+ with pytest .raises (TypeError ):
127+ @ttk .pyTTkSlot (int )
128+ def _slot (_ ): pass
129+ ttk .pyTTkSignal (Union [int ,float ]).connect (_slot )
130+ with pytest .raises (TypeError ):
131+ @ttk .pyTTkSlot (Union [int ,str ])
132+ def _slot (_ ): pass
133+ ttk .pyTTkSignal (Union [int ,float ]).connect (_slot )
0 commit comments