@@ -293,31 +293,41 @@ def with_failing_method(klass, method_name, message)
293293 klass . define_method ( method_name , original_method )
294294 end
295295
296- def test_completion_candidates_are_sorted
297- xzyb , xzya , xzyc = 'b' , 'a' , 'c'
298- lvar_binding = binding
299-
300- {
301- '1.' => empty_binding , # [:call] with empty method name
302- "''.s" => empty_binding , # [:call] with method name prefix
303- 'Math::' => empty_binding , # [:call_or_const]
304- '$std' => empty_binding , # [:gvar]
305- 'xzy' => lvar_binding # [:lvar_or_method]
306- } . each do |code , bind |
307- candidates = ReplTypeCompletor . analyze ( code , binding : bind ) . completion_candidates
308- refute_empty candidates , "Expected completion candidates of #{ code . inspect } not to be empty"
309- underscore , regular = candidates . partition { _1 . start_with? ( '_' ) }
310- assert_equal regular . sort + underscore . sort , candidates , "Expected completion candidates of #{ code . inspect } to be sorted with underscore methods last"
311- end
296+ def test_call_candidates_are_sorted_with_underscore_methods_last
297+ type = Struct . new ( :methods ) . new ( %i[ _zeta beta _alpha alpha ] )
298+ result = ReplTypeCompletor ::Result . new ( [ :call , '' , type , false ] , binding , __FILE__ )
299+
300+ assert_equal %w[ alpha beta _alpha _zeta ] , result . completion_candidates
301+ end
302+
303+ def test_call_or_const_candidates_are_sorted_within_groups
304+ type = Struct . new ( :methods , :constants ) . new ( %i[ z_method a_method ] , %i[ ZConst AConst ] )
305+ result = ReplTypeCompletor ::Result . new ( [ :call_or_const , '' , type , false ] , binding , __FILE__ )
306+
307+ assert_equal %w[ a_method z_method AConst ZConst ] , result . completion_candidates
308+ end
309+
310+ def test_const_candidates_are_sorted_before_reserved_words
311+ scope = Struct . new ( :constants ) . new ( %w[ ZConst AConst ] )
312+ result = ReplTypeCompletor ::Result . new ( [ :const , '' , nil , scope ] , binding , __FILE__ )
313+
314+ assert_equal %w[ AConst ZConst ] + ReplTypeCompletor ::Result ::RESERVED_WORDS , result . completion_candidates
315+ end
316+
317+ def test_ivar_candidates_are_sorted_within_groups
318+ scope = Struct . new ( :instance_variables , :class_variables ) . new ( %w[ @z @a ] , %w[ @@z @@a ] )
319+ result = ReplTypeCompletor ::Result . new ( [ :ivar , '@' , scope ] , binding , __FILE__ )
320+
321+ assert_equal %w[ a z @a @z ] , result . completion_candidates
322+ end
312323
313- # Internal methods (leading underscore, e.g. __id__ and __send__) are listed last
314- candidates = ReplTypeCompletor . analyze ( '1.' , binding : empty_binding ) . completion_candidates
315- assert candidates . any? { _1 . start_with? ( '_' ) }
316- refute candidates . first . start_with? ( '_' )
317- assert candidates . last . start_with? ( '_' )
324+ def test_lvar_or_method_candidates_are_sorted_within_groups
325+ type = Struct . new ( :all_methods ) . new ( %i[ z_method a_method ] )
326+ scope = Struct . new ( :self_type , :local_variables ) . new ( type , %w[ z_local a_local ] )
327+ result = ReplTypeCompletor ::Result . new ( [ :lvar_or_method , '' , scope ] , binding , __FILE__ )
318328
319- # Local variables xzyb, xzya, xzyc are defined in non-alphabetical order
320- assert_equal [ xzya , xzyb , xzyc ] , ReplTypeCompletor . analyze ( 'xzy' , binding : lvar_binding ) . completion_candidates
329+ expected = %w[ a_local z_local a_method z_method ] + ReplTypeCompletor :: Result :: RESERVED_WORDS
330+ assert_equal expected , result . completion_candidates
321331 end
322332
323333 def test_analyze_error
0 commit comments