ffi-clang

FORK: Ruby FFI bindings for my custom patched clang 8.0.
git clone https://git.neptards.moe/neptards/ffi-clang.git
Log | Files | Refs | README

code_completion_spec.rb (5464B)


      1 # Copyright, 2014, by Masahiro Sano.
      2 # 
      3 # Permission is hereby granted, free of charge, to any person obtaining a copy
      4 # of this software and associated documentation files (the "Software"), to deal
      5 # in the Software without restriction, including without limitation the rights
      6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      7 # copies of the Software, and to permit persons to whom the Software is
      8 # furnished to do so, subject to the following conditions:
      9 # 
     10 # The above copyright notice and this permission notice shall be included in
     11 # all copies or substantial portions of the Software.
     12 #
     13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     19 # THE SOFTWARE.
     20 
     21 describe CodeCompletion do
     22 	let(:filename) { fixture_path("completion.cxx") }
     23 	let(:translation_unit) { Index.new.parse_translation_unit(filename) }
     24 	let(:line) { 7 }
     25 	let(:column) { 6 }
     26 	let(:results) { translation_unit.code_complete(filename, line, column) }
     27 
     28 	describe "self.default_code_completion_options" do
     29 		let(:options) { FFI::Clang::CodeCompletion.default_code_completion_options }
     30 		it "returns a default set of code-completion options" do
     31 			expect(options).to be_kind_of(Hash)
     32 			options.keys.each { |key|
     33 				expect(FFI::Clang::Lib::CodeCompleteFlags.symbols).to include(key)
     34 			}
     35 		end
     36 	end
     37 
     38 	describe CodeCompletion::Results do
     39 		it "can be obtained from a translation unit" do
     40 			expect(results).to be_kind_of(CodeCompletion::Results)
     41 			
     42 			# At least 40 results, depends on standard library implementation:
     43 			expect(results.size).to be >= 40
     44 			
     45 			expect(results.results).to be_kind_of(Array)
     46 			expect(results.results.first).to be_kind_of(CodeCompletion::Result)
     47 		end
     48 
     49 		it "calls dispose_code_complete_results on GC" do
     50 			expect(Lib).to receive(:dispose_code_complete_results).at_least(:once)
     51 			expect{results.free}.not_to raise_error
     52 		end
     53 
     54 		it "#each" do
     55 			spy = double(stub: nil)
     56 			expect(spy).to receive(:stub).exactly(results.size).times
     57 			results.each { spy.stub }
     58 		end
     59 
     60 		it "#num_diagnostics" do
     61 			expect(results.num_diagnostics).to eq(2)
     62 		end
     63 
     64 		it "#diagnostic" do
     65 			expect(results.diagnostic(0)).to be_kind_of(Diagnostic)
     66 		end
     67 
     68 		it "#diagnostics" do
     69 			expect(results.diagnostics).to be_kind_of(Array)
     70 			expect(results.diagnostics.first).to be_kind_of(Diagnostic)
     71 			expect(results.diagnostics.size).to eq(results.num_diagnostics)
     72 		end
     73 
     74 		it "#contexts" do
     75 			expect(results.contexts).to be_kind_of(Hash)
     76 			results.contexts.keys.each { |key|
     77 				expect(FFI::Clang::Lib::CompletionContext.symbols).to include(key)
     78 			}
     79 		end
     80 
     81 		it "#container_usr" do
     82 			expect(results.container_usr).to be_kind_of(String)
     83 			expect(results.container_usr).to match(/std.+vector/)
     84 		end
     85 
     86 		it "#container_kind" do
     87 			expect(results.container_kind).to be_kind_of(Symbol)
     88 			expect(results.container_kind).to eq(:cursor_class_decl)
     89 		end
     90 
     91 		it "#incomplete?" do
     92 			expect(results.incomplete?).to be false
     93 		end
     94 
     95 		it "#objc_selector" do
     96 			#TODO
     97 		end
     98 
     99 		it "#sort!" do
    100 			results.sort!
    101 			
    102 			possibilities = results.first.string.chunks.select{|x| x[:kind] == :typed_text}.collect{|chunk| chunk[:text]}
    103 			
    104 			# may be sorted with typed_text kind, first result will start with 'a'.. not necessarily
    105 			expect(possibilities).to be == possibilities.sort
    106 		end
    107 	end
    108 
    109 	describe CodeCompletion::Result do
    110 		let(:result) { results.results.first }
    111 		it "#string" do
    112 			expect(result.string).to be_kind_of(CodeCompletion::String)
    113 		end
    114 
    115 		it "#kind" do
    116 			expect(result.kind).to be_kind_of(Symbol)
    117 		end
    118 	end
    119 
    120 	describe CodeCompletion::String do
    121 		let(:str) { results.sort!; results.find{|x| x.string.chunk_text(1) == 'assign'}.string }
    122 
    123 		it "#num_chunks" do
    124 			expect(str.num_chunks).to be >= 5
    125 		end
    126 
    127 		it "#chunk_kind" do
    128 			expect(str.chunk_kind(0)).to eq(:result_type)
    129 			expect(str.chunk_kind(1)).to eq(:typed_text)
    130 		end
    131 
    132 		it "#chunk_text" do
    133 			expect(str.chunk_text(0)).to be =~ /void/
    134 			expect(str.chunk_text(1)).to eq('assign')
    135 		end
    136 
    137 		it "#chunk_completion" do
    138 			expect(str.chunk_completion(0)).to be_kind_of(CodeCompletion::String)
    139 		end
    140 
    141 		it "#chunks" do
    142 			expect(str.chunks).to be_kind_of(Array)
    143 			expect(str.chunks.first).to be_kind_of(Hash)
    144 			expect(str.chunks.size).to eq(str.num_chunks)
    145 		end
    146 
    147 		it "#priority" do
    148 			expect(str.priority).to be_kind_of(Integer)
    149 		end
    150 
    151 		it "#availability" do
    152 			expect(str.availability).to be_kind_of(Symbol)
    153 			expect(str.availability).to eq(:available)
    154 		end
    155 
    156 		it "#num_annotations" do
    157 			expect(str.num_annotations).to be_kind_of(Integer)
    158 			expect(str.num_annotations).to eq(0)
    159 		end
    160 
    161 		it "#annotation" do
    162 			expect(str.annotation(100)).to be_nil
    163 			# TODO: need tests for String which has annotation
    164 		end
    165 
    166 		it "#annotations" do
    167 			expect(str.annotations).to be_kind_of(Array)
    168 			# TODO: need tests for String which has annotation
    169 		end
    170 
    171 		it "#parent" do
    172 			expect(str.parent).to be_kind_of(String)
    173 			expect(str.parent).to be =~ /std.+vector/
    174 		end
    175 
    176 		it "#comment" do
    177 			expect(str.comment).to be_nil
    178 			# TODO: need tests for String which has real comment
    179 		end
    180 	end
    181 end