22import pytest
33
44from awsshell .utils import InMemoryFSLayer
5+ from prompt_toolkit .buffer import Document
6+ from prompt_toolkit .contrib .validators .base import Validator , ValidationError
57from awsshell .interaction import InteractionLoader , InteractionException
68from awsshell .interaction import SimpleSelect , SimplePrompt , FilePrompt
9+ from awsshell .interaction import FuzzyCompleter , FuzzySelect
710
811
912@pytest .fixture
@@ -98,14 +101,15 @@ def test_simple_select_with_path():
98101 assert xformed == options [1 ]
99102
100103
101- def test_simple_select_bad_data (simple_selector ):
102- # Test that simple select throws exceptions when given bad data
104+ @pytest .mark .parametrize ('selector' , [simple_selector (), FuzzySelect ({}, u'' )])
105+ def test_simple_select_bad_data (selector ):
106+ # Test that a selector select throws exceptions when given bad data
103107 with pytest .raises (InteractionException ) as ie :
104- simple_selector .execute ({})
105- assert 'SimpleSelect expects a non-empty list' in str (ie .value )
108+ selector .execute ({})
109+ assert 'expects a non-empty list' in str (ie .value )
106110 with pytest .raises (InteractionException ) as ie :
107- simple_selector .execute ([])
108- assert 'SimpleSelect expects a non-empty list' in str (ie .value )
111+ selector .execute ([])
112+ assert 'expects a non-empty list' in str (ie .value )
109113
110114
111115def test_simple_prompt ():
@@ -126,3 +130,53 @@ def test_simple_prompt_bad_data(simple_prompt):
126130 with pytest .raises (InteractionException ) as ie :
127131 simple_prompt .execute ([])
128132 assert 'SimplePrompt expects a dict as data' in str (ie .value )
133+
134+
135+ def test_fuzzy_completer ():
136+ def to_list (candidates ):
137+ return [c .text for c in candidates ]
138+
139+ corpus = ['A word' , 'Awo' , 'A b c' ]
140+ document = Document (text = u'' )
141+ completer = FuzzyCompleter (corpus )
142+ candidates = completer .get_completions (document , None )
143+ assert to_list (candidates ) == corpus
144+ document = Document (text = u'Awo' )
145+ candidates = completer .get_completions (document , None )
146+ assert to_list (candidates ) == ['Awo' , 'A word' ]
147+
148+
149+ def test_fuzzy_select ():
150+ # Verify that SimpleSelect calls prompt and it returns a selection
151+ prompt = mock .Mock ()
152+ selector = FuzzySelect ({}, 'one or two?' , prompt )
153+ options = ['one' , 'two' ]
154+ prompt .return_value = options [1 ]
155+ xformed = selector .execute (options )
156+ assert prompt .call_count == 1
157+ assert xformed == options [1 ]
158+ args , kwargs = prompt .call_args
159+ validator = kwargs ['validator' ]
160+ assert isinstance (kwargs ['validator' ], Validator )
161+ with pytest .raises (ValidationError ):
162+ document = Document (text = u'three' )
163+ validator .validate (document )
164+
165+
166+ def test_fuzzy_select_with_path ():
167+ # Verify that FuzzySelect calls prompt and it returns the corresponding
168+ # item derived from the path.
169+ prompt = mock .Mock ()
170+ model = {'Path' : '[].a' }
171+ fuzzy_selector = FuzzySelect (model , 'Promptingu' , prompt )
172+ options = [{'a' : '1' , 'b' : 'one' }, {'a' : '2' , 'b' : 'two' }]
173+ prompt .return_value = '2'
174+ xformed = fuzzy_selector .execute (options )
175+ assert prompt .call_count == 1
176+ assert xformed == options [1 ]
177+ args , kwargs = prompt .call_args
178+ validator = kwargs ['validator' ]
179+ assert isinstance (kwargs ['validator' ], Validator )
180+ with pytest .raises (ValidationError ):
181+ document = Document (text = u'3' )
182+ validator .validate (document )
0 commit comments