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

compilation_database_spec.rb (5999B)


      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 CompilationDatabase do
     22 	let(:dirpath) { fixture_path('') }
     23 	let(:cdb) { CompilationDatabase.new(dirpath) }
     24 	let(:file) { '/home/xxxxx/src/llvm-trunk/lib/Support/APFloat.cpp' }
     25 
     26 	it "can be created" do
     27 		expect(cdb).to be_kind_of(CompilationDatabase)
     28 	end
     29 
     30 	it "raises DatabaseLoadError if cannot be created" do
     31 		expect{CompilationDatabase.new('/not/exist/directory')}.to raise_error FFI::Clang::CompilationDatabase::DatabaseLoadError
     32 	end
     33 
     34 	it "calls compilation_database_dispose on GC" do
     35 		cdb.autorelease = false
     36 		# expect(Lib).to receive(:compilation_database_dispose).with(cdb).once
     37 		expect{cdb.free}.not_to raise_error
     38 	end
     39 
     40 	describe '#compile_commands' do
     41 		let(:not_found_file) { '/home/xxxxx/not_found_file_path' }
     42 
     43 		it "returns compile commands used for a file" do
     44 			expect(cdb.compile_commands(file)).to be_kind_of(CompilationDatabase::CompileCommands)
     45 			expect(cdb.compile_commands(file).size).to eq(1)
     46 		end
     47 
     48 		it "returns compile commands if the specified file is not found" do
     49 			expect(cdb.compile_commands(not_found_file)).to be_kind_of(CompilationDatabase::CompileCommands)
     50 			if FFI::Clang.clang_version_string[/\d+/].to_i >= 10
     51 				expect(cdb.compile_commands(not_found_file).size).to eq(1)
     52 			else
     53 				expect(cdb.compile_commands(not_found_file).size).to eq(0)
     54 			end
     55 		end
     56 	end
     57 
     58 	describe '#all_compile_commands' do
     59 		it "returns all compile commands in the database" do
     60 			expect(cdb.all_compile_commands).to be_kind_of(CompilationDatabase::CompileCommands)
     61 			expect(cdb.all_compile_commands.size).to eq(3)
     62 		end
     63 	end
     64 
     65 	describe CompilationDatabase::CompileCommands do
     66 		let(:commands) { cdb.compile_commands(file) }
     67 
     68 		it "calls compile_commands_dispose on GC" do
     69 			commands.autorelease = false
     70 			# expect(Lib).to receive(:compile_commands_dispose).with(commands).once
     71 			expect{commands.free}.not_to raise_error
     72 		end
     73 
     74 		describe '#size' do
     75 			it "returns the number of CompileCommand" do
     76 				expect(commands.size).to be_kind_of(Integer)
     77 				expect(commands.size).to eq(1)
     78 			end
     79 
     80 			it "returns the number of CompileCommand" do
     81 				expect(cdb.all_compile_commands.size).to eq(3)
     82 			end
     83 		end
     84 
     85 		describe '#command' do
     86 			it "returns the I'th CompileCommand" do
     87 				expect(commands.command(0)).to be_kind_of(CompilationDatabase::CompileCommand)
     88 			end
     89 		end
     90 
     91 		describe "#commands" do
     92 			it "returns all CompileCommand as Array" do
     93 				expect(commands.commands).to be_kind_of(Array)
     94 				expect(commands.commands.first).to be_kind_of(CompilationDatabase::CompileCommand)
     95 				expect(commands.commands.size).to eq(commands.size)
     96 			end
     97 		end
     98 
     99 		describe "#each" do
    100 			let(:spy) { double(stub: nil) }
    101 			it "calls block once for each CompileCommand" do
    102 				expect(spy).to receive(:stub).exactly(commands.size).times
    103 				commands.each { spy.stub }
    104 			end
    105 		end
    106 	end
    107 
    108 	describe CompilationDatabase::CompileCommand do
    109 		let(:cmd) { cdb.compile_commands(file).first }
    110 
    111 		describe '#directory' do
    112 			it "returns the working directory" do
    113 				expect(cmd.directory).to be_kind_of(String)
    114 				expect(cmd.directory).to eq('/home/xxxxx/src/build-trunk/lib/Support')
    115 			end
    116 		end
    117 
    118 		describe '#num_args' do
    119 			it "returns the number of CompileCommand" do
    120 				expect(cmd.num_args).to be_kind_of(Integer)
    121 				expect(cmd.num_args).to be >= 31 # clang may insert additional args
    122 			end
    123 		end
    124 
    125 		describe '#arg' do
    126 			it "returns the I'th argument value" do
    127 				expect(cmd.arg(0)).to be_kind_of(String)
    128 				expect(cmd.arg(0)).to eq('/opt/llvm/3.4/bin/clang++')
    129 			end
    130 		end
    131 
    132 		describe '#args' do
    133 			it "returns all argument values as Array" do
    134 				expect(cmd.args).to be_kind_of(Array)
    135 				expect(cmd.args.first).to be_kind_of(String)
    136 				expect(cmd.args.size).to eq(cmd.num_args)
    137 			end
    138 		end
    139 
    140 		describe '#num_mapped_sources' do
    141 			# TODO: a case which has mapped sources
    142 
    143 			it "returns the number of source mappings" do
    144 				# expect(cmd.num_mapped_sources).to be_kind_of(Integer)
    145 				# expect(cmd.num_mapped_sources).to eq(0)
    146 			end
    147 		end
    148 
    149 		describe '#mapped_source_path' do
    150 			it "returns the I'th mapped source path" do
    151 				# TODO: a case which returns real source path
    152 				# expect(cmd.mapped_source_path(0)).to be_kind_of(String)
    153 			end
    154 
    155 			it "returns nil if the index exceeds element size" do
    156 				# expect(cmd.mapped_source_path(1000)).to be_nil
    157 			end
    158 		end
    159 
    160 		describe '#mapped_source_content' do
    161 			it "returns the I'th mapped source content" do
    162 				# TODO: a case which returns real source path
    163 				# expect(cmd.mapped_source_content(0)).to be_kind_of(String)
    164 			end
    165 
    166 			it "returns nil if the index exceeds element size" do
    167 				# expect(cmd.mapped_source_content(1000)).to be_nil
    168 			end
    169 		end
    170 
    171 		describe '#mapped_sources' do
    172 			# TODO: a case which has mapped sources
    173 
    174 			it "returns all mapped sources as Array" do
    175 				# expect(cmd.mapped_sources).to be_kind_of(Array)
    176 				# expect(cmd.mapped_sources.size).to eq(cmd.num_mapped_sources)
    177 			end
    178 		end
    179 	end
    180 end