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

translation_unit_spec.rb (7547B)


      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 TranslationUnit do
     22 	before :all do
     23 		FileUtils.mkdir_p TMP_DIR
     24 	end
     25 
     26 	after :all do
     27 		FileUtils.rm_rf TMP_DIR
     28 	end
     29 
     30 	let(:translation_unit) { Index.new.parse_translation_unit fixture_path("a.c") }
     31 
     32 	it "returns a list of diagnostics" do
     33 		diags = translation_unit.diagnostics
     34 		expect(diags).to be_kind_of(Array)
     35 		expect(diags).to_not be_empty
     36 	end
     37 
     38 	it "returns a list of diagnostics from an unsaved file" do
     39 		file = UnsavedFile.new("a.c", File.read(fixture_path("a.c")))
     40 		translation_unit = Index.new.parse_translation_unit("a.c", nil,[file])
     41 		diags = translation_unit.diagnostics
     42 		expect(diags).to be_kind_of(Array)
     43 		expect(diags).to_not be_empty
     44 	end
     45 
     46 	it "calls dispose_translation_unit on GC" do
     47 		translation_unit.autorelease = false
     48 		# expect(Lib).to receive(:dispose_translation_unit).with(translation_unit).once
     49 		expect{translation_unit.free}.not_to raise_error
     50 	end
     51 
     52 	describe "#spelling" do
     53 		let (:spelling) { translation_unit.spelling }
     54 
     55 		it "returns own filename" do
     56 			expect(spelling).to be_kind_of(String)
     57 			expect(spelling).to eq(fixture_path("a.c"))
     58 		end
     59 	end
     60 
     61 	describe "#file" do
     62 		let (:specified_file) { translation_unit.file(fixture_path("a.c")) }
     63 		let (:unspecified_file) { translation_unit.file }
     64 
     65 		it "returns File instance" do
     66 			expect(specified_file).to be_kind_of(FFI::Clang::File)
     67 		end
     68 
     69 		it "returns main file when file name is not specified" do
     70 			expect(unspecified_file).to be_kind_of(FFI::Clang::File)
     71 			expect(unspecified_file.name).to include("a.c")
     72 		end
     73 	end
     74 
     75 	describe "#location" do
     76 		let(:file) { translation_unit.file(fixture_path("a.c")) }
     77 		let(:column) { 12 }
     78 		let(:location) { translation_unit.location(file, 1, column) }
     79 
     80 		it "returns source location at a specific point" do
     81 			expect(location).to be_kind_of(SourceLocation)
     82 			expect(location.file).to eq(fixture_path("a.c"))
     83 			expect(location.line).to eq(1)
     84 			expect(location.column).to eq(column)
     85 		end
     86 	end
     87 
     88 	describe "#location_offset" do
     89 		let(:file) { translation_unit.file(fixture_path("a.c")) }
     90 		let(:offset) { 10 }
     91 		let(:location) { translation_unit.location_offset(file, offset) }
     92 
     93 		it "returns source location at a specific offset point" do
     94 			expect(location).to be_kind_of(SourceLocation)
     95 			expect(location.file).to eq(fixture_path("a.c"))
     96 			expect(location.column).to eq(offset+1)
     97 		end
     98 	end
     99 
    100 	describe "#cursor" do
    101 		let(:cursor) { translation_unit.cursor }
    102 		let(:location) { translation_unit.location(translation_unit.file(fixture_path("a.c")), 1, 10) }
    103 		let(:cursor_with_loc) { translation_unit.cursor(location) }
    104 
    105 		it "returns translation unit cursor if no arguments are specified" do
    106 			expect(cursor).to be_kind_of(Cursor)
    107 			expect(cursor.kind).to eq(:cursor_translation_unit)
    108 		end
    109 
    110 		it "returns a correspond cursor if a source location is passed" do
    111 			expect(cursor_with_loc).to be_kind_of(Cursor)
    112 			expect(cursor_with_loc.kind).to eq(:cursor_parm_decl)
    113 		end
    114 	end
    115 
    116 	describe "#self.default_editing_translation_unit_options" do
    117 		let (:opts) { FFI::Clang::TranslationUnit.default_editing_translation_unit_options }
    118 		it "returns hash with symbols of TranslationUnitFlags" do
    119 			expect(opts).to be_kind_of(Hash)
    120 			opts.keys.each { |key|
    121 				expect(FFI::Clang::Lib::TranslationUnitFlags.symbols).to include(key)
    122 			}
    123 		end
    124 	end
    125 
    126 	describe "#default_save_options" do
    127 		let (:opts) { translation_unit.default_save_options }
    128 		it "returns hash with symbols of SaveTranslationUnitFlags" do
    129 			expect(opts).to be_kind_of(Hash)
    130 			opts.keys.each { |key|
    131 				expect(FFI::Clang::Lib::SaveTranslationUnitFlags.symbols).to include(key)
    132 			}
    133 		end
    134 	end
    135 
    136 	describe "#save" do
    137 		let (:filepath) { "#{TMP_DIR}/save_translation_unit" }
    138 		let (:may_not_exist_filepath) { "#{TMP_DIR}/not_writable_directory/save_translation_unit" }
    139 		it "saves translation unit as a file" do
    140 			expect{translation_unit.save(filepath)}.not_to raise_error
    141 			expect(FileTest.exist?(filepath)).to be true
    142 		end
    143 
    144 		it "raises exception if save path is not writable" do
    145 			FileUtils.mkdir_p File.dirname(may_not_exist_filepath)
    146 			File.chmod(0444, File.dirname(may_not_exist_filepath))
    147 			expect{translation_unit.save(may_not_exist_filepath)}.to raise_error(FFI::Clang::Error)
    148 			expect(FileTest.exist?(may_not_exist_filepath)).to be false
    149 		end
    150 	end
    151 
    152 	describe "#default_reparse_options" do
    153 		let (:opts) { translation_unit.default_reparse_options }
    154 		it "returns hash with symbols of ReparseFlags" do
    155 			expect(opts).to be_kind_of(Hash)
    156 			opts.keys.each { |key|
    157 				expect(FFI::Clang::Lib::ReparseFlags.symbols).to include(key)
    158 			}
    159 		end
    160 	end
    161 
    162 	describe "#reparse" do
    163 		let (:path) { "#{TMP_DIR}/reparse_tmp.c" }
    164 		before :each do
    165 			FileUtils.touch path
    166 			@reparse_translation_unit = Index.new.parse_translation_unit(path)
    167 		end
    168 		after :each do
    169 			FileUtils.rm path, :force => true
    170 		end
    171 
    172 		it "recretes translation unit" do
    173 			File::open(path, "w+") { |io|
    174 			   io.write("int a;")
    175 			}
    176 			expect(find_first(@reparse_translation_unit.cursor, :cursor_variable)).to be nil
    177 			expect{@reparse_translation_unit.reparse}.not_to raise_error
    178 			expect(find_first(@reparse_translation_unit.cursor, :cursor_variable).spelling).to eq("a")
    179 		end
    180 
    181 		it "raises exception if the file is not found when reparsing" do
    182 			FileUtils.rm path, :force => true
    183 			expect{@reparse_translation_unit.reparse}.to raise_error(FFI::Clang::Error)
    184 		end
    185 	end
    186 
    187 	describe "#resource_usage" do
    188 		let (:ru) { translation_unit.resource_usage }
    189 		it "returns ResourceUsage instance that represents memory usage of TU" do
    190 			expect(ru).to be_kind_of(TranslationUnit::ResourceUsage)
    191 		end
    192 	end
    193 
    194 	describe TranslationUnit::ResourceUsage do
    195 		let (:ru) { translation_unit.resource_usage }
    196 		describe "#entries" do
    197 			let (:entries) { translation_unit.resource_usage.entries }
    198 			it "returns array of CXTUResourceUsageEntry" do
    199 				expect(entries).to be_kind_of(Array)
    200 				expect(entries.first).to be_kind_of(Lib::CXTUResourceUsageEntry)
    201 				expect(entries.first[:kind]).to be_kind_of(Symbol)
    202 				expect(entries.first[:amount]).to be_kind_of(Integer)
    203 			end
    204 		end
    205 
    206 		describe "#self.name" do
    207 			let(:name) { FFI::Clang::TranslationUnit::ResourceUsage.name(:ast) }
    208 			it "returns the name of the memory category" do
    209 				expect(name).to be_kind_of(String)
    210 			end
    211 		end
    212 
    213 		describe "#self.release" do
    214 			it "releases data by calling 'clang_disposeCXTUResourceUsage'" do
    215 				ru.autorelease = false
    216 				expect{ ru.free }.not_to raise_error
    217 			end
    218 		end
    219 	end
    220 end