token_spec.rb (2862B)
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 Tokens do 22 let(:translation_unit) { Index.new.parse_translation_unit(fixture_path("list.c")) } 23 let(:cursor) { translation_unit.cursor } 24 let(:range) { find_first(cursor, :cursor_struct).extent } 25 let(:tokens) { translation_unit.tokenize(range) } 26 27 it "can be obtained from a translation unit" do 28 expect(tokens).to be_kind_of(Tokens) 29 expect(tokens.size).to be >= 12 30 expect(tokens.tokens).to be_kind_of(Array) 31 expect(tokens.tokens.first).to be_kind_of(Token) 32 end 33 34 it "calls dispose_tokens on GC" do 35 tokens.autorelease = false 36 expect(Lib).to receive(:dispose_tokens).at_least(:once) 37 expect{tokens.free}.not_to raise_error 38 end 39 40 it "#each" do 41 spy = double(stub: nil) 42 expect(spy).to receive(:stub).exactly(tokens.size).times 43 tokens.each { spy.stub } 44 end 45 46 it "#cursors" do 47 expect(tokens.cursors).to be_kind_of(Array) 48 expect(tokens.cursors.size).to eq(tokens.size) 49 expect(tokens.cursors.first).to be_kind_of(Cursor) 50 end 51 end 52 53 describe Token do 54 let(:translation_unit) { Index.new.parse_translation_unit(fixture_path("list.c")) } 55 let(:cursor) { translation_unit.cursor } 56 let(:range) { find_first(cursor, :cursor_struct).extent } 57 let(:token) { translation_unit.tokenize(range).first } 58 59 it "can be obtained from a translation unit" do 60 expect(token).to be_kind_of(Token) 61 end 62 63 it "#kind" do 64 expect(token.kind).to be_kind_of(Symbol) 65 expect(token.kind).to eq(:keyword) 66 end 67 68 it "#spelling" do 69 expect(token.spelling).to be_kind_of(String) 70 expect(token.spelling).to eq('struct') 71 end 72 73 it "#location" do 74 expect(token.location).to be_kind_of(SourceLocation) 75 expect(token.location.line).to eq(1) 76 end 77 78 it "#extent" do 79 expect(token.extent).to be_kind_of(SourceRange) 80 expect(token.extent.start.line).to eq(1) 81 end 82 end