starscope 0.1.10 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/lib/test_db.rb CHANGED
@@ -1,9 +1,26 @@
1
1
  require File.expand_path('../../test_helper', __FILE__)
2
+ require 'tempfile'
2
3
 
3
4
  describe StarScope::DB do
4
5
 
6
+ def validate(db)
7
+ files = db.instance_eval('@meta[:files]')
8
+ files.keys.must_include GOLANG_SAMPLE
9
+ files.keys.must_include RUBY_SAMPLE
10
+ files[GOLANG_SAMPLE][:last_updated].must_equal File.mtime(GOLANG_SAMPLE).to_i
11
+ files[RUBY_SAMPLE][:last_updated].must_equal File.mtime(RUBY_SAMPLE).to_i
12
+
13
+ tbls = db.instance_eval('@tables')
14
+ defs = tbls[:defs].map {|x| x[:name][-1]}
15
+ assert defs.include? :DB
16
+ assert defs.include? :NoTableError
17
+ assert defs.include? :load
18
+ assert defs.include? :update
19
+ assert defs.include? :files_from_path
20
+ end
21
+
5
22
  before do
6
- @db = StarScope::DB.new(false)
23
+ @db = StarScope::DB.new(false, false)
7
24
  end
8
25
 
9
26
  it "must raise on invalid tables" do
@@ -11,34 +28,81 @@ describe StarScope::DB do
11
28
  end
12
29
 
13
30
  it "must correctly add paths" do
14
- paths = [GOLANG_SAMPLE, 'test/files']
31
+ paths = [GOLANG_SAMPLE, 'test/files/**/*']
15
32
  @db.add_paths(paths)
16
- @db.instance_eval('@paths').must_equal paths
17
- @db.instance_eval('@files').keys.must_include GOLANG_SAMPLE
18
- @db.instance_eval('@files').keys.must_include RUBY_SAMPLE
33
+ @db.instance_eval('@meta[:paths]').must_equal paths
34
+ validate(@db)
19
35
  end
20
36
 
21
37
  it "must correctly pick up new files in old paths" do
22
- @db.instance_eval('@paths = ["test/files"]')
38
+ @db.instance_eval('@meta[:paths] = ["test/files/**/*"]')
23
39
  @db.update
24
- files = @db.instance_eval('@files').keys
25
- files.must_include GOLANG_SAMPLE
26
- files.must_include RUBY_SAMPLE
40
+ validate(@db)
27
41
  end
28
42
 
29
43
  it "must correctly remove old files in existing paths" do
30
- @db.instance_eval('@paths = ["test/files"]')
31
- @db.instance_eval('@files = {"test/files/foo"=>"2012-01-01"}')
32
- @db.instance_eval('@files').keys.must_include 'test/files/foo'
44
+ @db.instance_eval('@meta[:paths] = ["test/files"]')
45
+ @db.instance_eval('@meta[:files] = {"test/files/foo" => {:last_update=>1}}')
46
+ @db.instance_eval('@meta[:files]').keys.must_include 'test/files/foo'
33
47
  @db.update
34
- @db.instance_eval('@files').keys.wont_include 'test/files/foo'
48
+ @db.instance_eval('@meta[:files]').keys.wont_include 'test/files/foo'
35
49
  end
36
50
 
37
51
  it "must correctly load an old DB file" do
38
52
  @db.load('test/files/db_old.json.gz')
39
- @db.instance_eval('@paths').must_equal ['test/files']
40
- @db.instance_eval('@files').keys.must_include GOLANG_SAMPLE
41
- @db.instance_eval('@files').keys.must_include RUBY_SAMPLE
53
+ @db.instance_eval('@meta[:paths]').must_equal ['test/files/**/*']
54
+ validate(@db)
55
+ end
56
+
57
+ it "must correctly round-trip a database" do
58
+ file = Tempfile.new('starscope_test')
59
+ begin
60
+ @db.add_paths(['test/files'])
61
+ @db.save(file.path)
62
+ tmp = StarScope::DB.new(false, false)
63
+ tmp.load(file.path)
64
+ validate(tmp)
65
+ ensure
66
+ file.close
67
+ file.unlink
68
+ end
69
+ end
70
+
71
+ it "must correctly export to ctags" do
72
+ file = Tempfile.new('starscope_test')
73
+ begin
74
+ @db.add_paths(['test/files'])
75
+ @db.export_ctags(file)
76
+ file.rewind
77
+ lines = file.lines.to_a
78
+ lines.must_include "NoTableError\ttest/files/sample_ruby.rb\t/^ class NoTableError < StandardError; end$/;\"\tkind:c\n"
79
+ ensure
80
+ file.close
81
+ file.unlink
82
+ end
83
+ end
84
+
85
+ it "must correctly export to cscope" do
86
+ file = Tempfile.new('starscope_test')
87
+ begin
88
+ @db.add_paths(['test/files'])
89
+ @db.export_cscope(file)
90
+ file.rewind
91
+ lines = file.lines.to_a
92
+ lines.must_include "\t@test/files/sample_golang.go\n"
93
+ lines.must_include "\tgSunday\n"
94
+ lines.must_include "\t`add_file\n"
95
+ ensure
96
+ file.close
97
+ file.unlink
98
+ end
99
+ end
100
+
101
+ it "must correctly run queries" do
102
+ @db.add_paths(['test/files'])
103
+ @db.query(:calls, "abc").must_equal []
104
+ @db.query(:defs, "xyz").must_equal []
105
+ @db.query(:calls, "add_file").length.must_equal 3
42
106
  end
43
107
 
44
108
  end
@@ -3,11 +3,9 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  class TestGolang < Minitest::Test
4
4
  def setup
5
5
  @db = {}
6
- StarScope::Lang::Go.extract(GOLANG_SAMPLE) do |tbl, key, args|
7
- key = key.to_sym
8
- @db[tbl] ||= {}
9
- @db[tbl][key] ||= []
10
- @db[tbl][key] << args
6
+ StarScope::Lang::Go.extract(GOLANG_SAMPLE) do |tbl, name, args|
7
+ @db[tbl] ||= []
8
+ @db[tbl] << StarScope::Record.build(GOLANG_SAMPLE, name, args)
11
9
  end
12
10
  end
13
11
 
@@ -19,7 +17,7 @@ class TestGolang < Minitest::Test
19
17
 
20
18
  def test_defs
21
19
  assert @db.keys.include? :defs
22
- defs = @db[:defs].keys
20
+ defs = @db[:defs].map {|x| x[:name][-1]}
23
21
  assert defs.include? :a
24
22
  assert defs.include? :b
25
23
  assert defs.include? :c
@@ -29,31 +27,36 @@ class TestGolang < Minitest::Test
29
27
  assert defs.include? :v2
30
28
  assert defs.include? :Sunday
31
29
  assert defs.include? :Monday
30
+ assert defs.include? :single_var
31
+ assert defs.include? :single_const
32
+
33
+ refute defs.include? :"0x00"
34
+ refute defs.include? :"0x01"
35
+ refute defs.include? :"0x02"
36
+ refute defs.include? :"0x03"
32
37
  end
33
38
 
34
- def test_function_ends
39
+ def test_ends
35
40
  assert @db.keys.include? :end
36
- ends = @db[:end]
37
- assert ends.keys.count == 1
38
- assert ends.values.first.count == 5
41
+ assert @db[:end].count == 6
39
42
  end
40
43
 
41
44
  def test_function_calls
42
45
  assert @db.keys.include? :calls
43
- calls = @db[:calls]
46
+ calls = @db[:calls].group_by {|x| x[:name][-1]}
44
47
  assert calls.keys.include? :a
45
48
  assert calls.keys.include? :b
46
49
  assert calls.keys.include? :c
47
50
  assert calls.keys.include? :ttt
48
51
  assert calls[:a].count == 3
49
52
  assert calls[:b].count == 4
50
- assert calls[:c].count == 2
53
+ assert calls[:c].count == 4
51
54
  assert calls[:ttt].count == 2
52
55
  end
53
56
 
54
57
  def test_variable_assigns
55
58
  assert @db.keys.include? :assigns
56
- assigns = @db[:assigns]
59
+ assigns = @db[:assigns].group_by {|x| x[:name][-1]}
57
60
  assert assigns.keys.include? :x
58
61
  assert assigns.keys.include? :y
59
62
  assert assigns.keys.include? :z
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe StarScope::Matcher do
4
+
5
+ SAMPLE_RECORDS = [
6
+ {:name => [:"[abc"]},
7
+ {:name => [:"not a match"]},
8
+ {:name => [:a, :b, :c, :d]},
9
+ ]
10
+
11
+ it "must handle empty input" do
12
+ matcher = StarScope::Matcher.new("foo", [])
13
+ matcher.query.must_be_empty
14
+ end
15
+
16
+ it "must handle scoped queries" do
17
+ matcher = StarScope::Matcher.new("a::b::", SAMPLE_RECORDS)
18
+ matcher.query.must_equal [SAMPLE_RECORDS[2]]
19
+ end
20
+
21
+ it "must handle regex queries" do
22
+ matcher = StarScope::Matcher.new("a[bc]{2}", SAMPLE_RECORDS)
23
+ matcher.query.must_equal [SAMPLE_RECORDS[0]]
24
+
25
+ matcher = StarScope::Matcher.new("a.*d", SAMPLE_RECORDS)
26
+ matcher.query.must_equal [SAMPLE_RECORDS[2]]
27
+ end
28
+
29
+ it "must handle malformed regexes" do
30
+ matcher = StarScope::Matcher.new("[abc", SAMPLE_RECORDS)
31
+ matcher.query.must_equal [SAMPLE_RECORDS[0]]
32
+ end
33
+
34
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ describe StarScope::Record do
4
+
5
+ it "must symbolize compound name" do
6
+ rec = StarScope::Record.build(:foo, ["a", :b], {})
7
+ rec[:name].must_equal [:a, :b]
8
+ end
9
+
10
+ it "must symbolize and array-wrap simple name" do
11
+ rec = StarScope::Record.build(:foo, "a", {})
12
+ rec[:name].must_equal [:a]
13
+ end
14
+
15
+ it "must read correct line from file" do
16
+ rec = StarScope::Record.build(GOLANG_SAMPLE, :a, {:line_no => 1})
17
+ rec[:line].must_equal "package main"
18
+ end
19
+
20
+ end
@@ -3,11 +3,9 @@ require File.expand_path('../../test_helper', __FILE__)
3
3
  class TestRuby < Minitest::Test
4
4
  def setup
5
5
  @db = {}
6
- StarScope::Lang::Ruby.extract(RUBY_SAMPLE) do |tbl, key, args|
7
- key = key.to_sym
8
- @db[tbl] ||= {}
9
- @db[tbl][key] ||= []
10
- @db[tbl][key] << args
6
+ StarScope::Lang::Ruby.extract(RUBY_SAMPLE) do |tbl, name, args|
7
+ @db[tbl] ||= []
8
+ @db[tbl] << StarScope::Record.build(RUBY_SAMPLE, name, args)
11
9
  end
12
10
  end
13
11
 
@@ -20,7 +18,7 @@ class TestRuby < Minitest::Test
20
18
 
21
19
  def test_function_defs
22
20
  assert @db.keys.include? :defs
23
- defs = @db[:defs].keys
21
+ defs = @db[:defs].map {|x| x[:name][-1]}
24
22
  assert defs.include? :DB
25
23
  assert defs.include? :NoTableError
26
24
  assert defs.include? :load
@@ -28,16 +26,18 @@ class TestRuby < Minitest::Test
28
26
  assert defs.include? :files_from_path
29
27
  end
30
28
 
31
- def test_function_ends
29
+ def test_constant_defs
30
+ assert @db[:defs].map {|x| x[:name][-1]}.include? :PBAR_FORMAT
31
+ end
32
+
33
+ def test_ends
32
34
  assert @db.keys.include? :end
33
- ends = @db[:end]
34
- assert ends.keys.count == 1
35
- assert ends.values.first.count == 13
35
+ assert @db[:end].count == 13
36
36
  end
37
37
 
38
38
  def test_function_calls
39
39
  assert @db.keys.include? :calls
40
- calls = @db[:calls]
40
+ calls = @db[:calls].group_by {|x| x[:name][-1]}
41
41
  assert calls.keys.include? :add_file
42
42
  assert calls.keys.include? :each
43
43
  assert calls[:add_file].count == 3
@@ -46,7 +46,7 @@ class TestRuby < Minitest::Test
46
46
 
47
47
  def test_variable_assigns
48
48
  assert @db.keys.include? :assigns
49
- assigns = @db[:assigns]
49
+ assigns = @db[:assigns].group_by {|x| x[:name][-1]}
50
50
  assert assigns.keys.include? :pbar
51
51
  assert assigns.keys.include? :PBAR_FORMAT
52
52
  assert assigns[:pbar].count == 2
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ class TestStarScope < Minitest::Test
4
+
5
+ EXEC = 'bundle exec bin/starscope --no-read --no-write --no-progress ./test/files/'
6
+
7
+ def test_help
8
+ `#{EXEC} -h`.each_line do |line|
9
+ assert line.length <= 80
10
+ end
11
+ end
12
+
13
+ def test_version
14
+ assert `#{EXEC} -v`.chomp == StarScope::VERSION
15
+ end
16
+
17
+ def test_summary
18
+ lines = `#{EXEC} -s`.lines
19
+ end
20
+
21
+ def test_dump
22
+ lines = `#{EXEC} -d requires`.lines.to_a
23
+ assert lines[1].split.first == 'date'
24
+ assert lines[2].split.first == 'zlib'
25
+ end
26
+
27
+ def test_query
28
+ `#{EXEC} -q calls,add_file`.each_line do |line|
29
+ assert line.split[0..2] == ["StarScope", "DB", "add_file"]
30
+ end
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-24 00:00:00.000000000 Z
12
+ date: 2014-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oj
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '2.5'
21
+ version: '2.7'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '2.5'
29
+ version: '2.7'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: parser
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -140,10 +140,14 @@ files:
140
140
  - Rakefile
141
141
  - bin/starscope
142
142
  - lib/starscope.rb
143
- - lib/starscope/datum.rb
144
143
  - lib/starscope/db.rb
144
+ - lib/starscope/langs/coffeescript.rb
145
145
  - lib/starscope/langs/go.rb
146
+ - lib/starscope/langs/lua.rb
146
147
  - lib/starscope/langs/ruby.rb
148
+ - lib/starscope/matcher.rb
149
+ - lib/starscope/output.rb
150
+ - lib/starscope/record.rb
147
151
  - lib/starscope/version.rb
148
152
  - starscope.gemspec
149
153
  - test/files/db_old.json.gz
@@ -152,8 +156,10 @@ files:
152
156
  - test/files/sample_ruby.rb
153
157
  - test/lib/test_db.rb
154
158
  - test/lib/test_golang.rb
159
+ - test/lib/test_matcher.rb
160
+ - test/lib/test_record.rb
155
161
  - test/lib/test_ruby.rb
156
- - test/lib/test_version.rb
162
+ - test/lib/test_starscope.rb
157
163
  - test/test_helper.rb
158
164
  homepage: https://github.com/eapache/starscope
159
165
  licenses:
@@ -176,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
182
  version: '0'
177
183
  segments:
178
184
  - 0
179
- hash: 818285020742325812
185
+ hash: 4554607803448639637
180
186
  requirements: []
181
187
  rubyforge_project:
182
188
  rubygems_version: 1.8.23
@@ -190,6 +196,8 @@ test_files:
190
196
  - test/files/sample_ruby.rb
191
197
  - test/lib/test_db.rb
192
198
  - test/lib/test_golang.rb
199
+ - test/lib/test_matcher.rb
200
+ - test/lib/test_record.rb
193
201
  - test/lib/test_ruby.rb
194
- - test/lib/test_version.rb
202
+ - test/lib/test_starscope.rb
195
203
  - test/test_helper.rb
@@ -1,91 +0,0 @@
1
- class StarScope::Datum
2
-
3
- def self.build(file, key, args)
4
- args[:file] = file
5
- args[:key] = key
6
-
7
- if args[:line_no]
8
- args[:line] = File.readlines(file)[args[:line_no]-1].chomp
9
- end
10
-
11
- if args[:scope]
12
- if args[:scope].empty?
13
- args.delete(:scope)
14
- else
15
- args[:scope] = args[:scope].map {|x| x.to_sym}
16
- end
17
- end
18
-
19
- args
20
- end
21
-
22
- def self.score_match(dat, fqn)
23
- return 0 if not dat[:scope]
24
-
25
- score = 0
26
-
27
- i = -1
28
- fqn[0...-1].reverse.each do |test|
29
- if test.to_sym == dat[:scope][i]
30
- score += 5
31
- elsif Regexp.new(test, Regexp::IGNORECASE).match(dat[:scope][i])
32
- score += 2
33
- end
34
- i -= 1
35
- end
36
-
37
- score - dat[:scope].count - i + 1
38
- end
39
-
40
- def self.location(dat)
41
- "#{dat[:file]}:#{dat[:line_no]}"
42
- end
43
-
44
- def self.to_s(dat)
45
- str = ""
46
- str << "#{dat[:scope].join " "} " if dat[:scope]
47
- str << "#{dat[:key]} -- #{location dat}"
48
- str << " (#{dat[:line].strip})"
49
- end
50
-
51
- def self.ctag_line(dat)
52
- "#{dat[:key]}\t#{dat[:file]}\t/^#{dat[:line]}$/;"
53
- end
54
-
55
- def self.cscope_mark(tbl, dat)
56
- case tbl
57
- when :end
58
- case dat[:type]
59
- when :func
60
- ret = "}"
61
- else
62
- return ""
63
- end
64
- when :file
65
- ret = "@"
66
- when :defs
67
- case dat[:type]
68
- when :func
69
- ret = "$"
70
- when :class, :module
71
- ret = "c"
72
- when :type
73
- ret = "t"
74
- else
75
- ret = "g"
76
- end
77
- when :calls
78
- ret = "`"
79
- when :requires
80
- ret = "~\""
81
- when :imports
82
- ret = "~<"
83
- when :assigns
84
- ret = "="
85
- else
86
- return ""
87
- end
88
-
89
- return "\t" + ret
90
- end
91
- end