linecache 0.2 → 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/tracelines.rb ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ # $Id: tracelines.rb 36 2008-01-23 12:07:02Z rockyb $
3
+ begin require 'rubygems' rescue LoadError end
4
+ # require 'ruby-debug' ; Debugger.start
5
+
6
+ module TraceLineNumbers
7
+ @@SRC_DIR = File.expand_path(File.dirname(__FILE__))
8
+ require File.join(@@SRC_DIR, '..', 'ext', 'trace_nums')
9
+
10
+ # Return an array of lines numbers that could be
11
+ # stopped at given a file name of a Ruby program.
12
+ def lnums_for_file(file)
13
+ lnums_for_str(File.read(file))
14
+ end
15
+ module_function :lnums_for_file
16
+
17
+ # Return an array of lines numbers that could be
18
+ # stopped at given a file name of a Ruby program.
19
+ # We assume the each line has \n at the end. If not
20
+ # set the newline parameters to \n.
21
+ def lnums_for_str_array(string_array, newline='')
22
+ lnums_for_str(string_array.join(newline))
23
+ end
24
+ module_function :lnums_for_str_array
25
+ end
26
+
27
+ if __FILE__ == $0
28
+ SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
29
+ # test_file = '../test/rcov-bug.rb'
30
+ test_file = '../test/lnum-data/begin1.rb'
31
+ if File.exists?(test_file)
32
+ puts TraceLineNumbers.lnums_for_file(test_file).inspect
33
+ load(test_file, 0) # for later
34
+ end
35
+ puts TraceLineNumbers.lnums_for_file(__FILE__).inspect
36
+ unless SCRIPT_LINES__.empty?
37
+ key = SCRIPT_LINES__.keys.first
38
+ puts key
39
+ puts SCRIPT_LINES__[key]
40
+ puts TraceLineNumbers.lnums_for_str_array(SCRIPT_LINES__[key]).inspect
41
+ end
42
+ end
data/test/lnum-diag.rb ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # begin require 'rubygems' rescue LoadError end
4
+ # require 'ruby-debug' ; Debugger.start
5
+
6
+ TEST_DIR = File.expand_path(File.dirname(__FILE__))
7
+ TOP_SRC_DIR = File.join(TEST_DIR, '..')
8
+ require File.join(TOP_SRC_DIR, 'lib', 'tracelines.rb')
9
+
10
+ def dump_file(file, opts)
11
+ puts file
12
+ begin
13
+ fp = File.open(file, 'r')
14
+ rescue Errno::ENOENT
15
+ puts "File #{file} is not readable."
16
+ return
17
+ end
18
+ lines = fp.read
19
+ if opts[:print_source]
20
+ puts '=' * 80
21
+ puts lines
22
+ end
23
+ if opts[:print_parse]
24
+ puts '=' * 80
25
+ cmd = "#{File.join(TEST_DIR, 'parse-show.rb')} #{file}"
26
+ system(cmd)
27
+ end
28
+ if opts[:print_trace]
29
+ require 'tracer'
30
+ puts '=' * 80
31
+ tracer = Tracer.new
32
+ tracer.add_filter lambda {|event, f, line, id, binding, klass|
33
+ __FILE__ != f && event == 'line'
34
+ }
35
+ tracer.on{load(file)}
36
+ end
37
+ expected_lnums = nil
38
+ if opts[:expect_line]
39
+ fp.rewind
40
+ first_line = fp.readline.chomp
41
+ expected_str = first_line[1..-1]
42
+ begin
43
+ expected_lnums = eval(expected_str, binding, __FILE__, __LINE__)
44
+ rescue SyntaxError
45
+ puts '=' * 80
46
+ puts "Failed reading expected values from #{file}"
47
+ end
48
+ end
49
+ fp.close()
50
+ got_lnums = TraceLineNumbers.lnums_for_str(lines)
51
+ if expected_lnums
52
+ puts "expecting: #{expected_lnums.inspect}"
53
+ puts '-' * 80
54
+ if expected_lnums
55
+ if got_lnums != expected_lnums
56
+ puts "mismatch: #{got_lnums.inspect}"
57
+ else
58
+ puts 'Got what was expected.'
59
+ end
60
+ else
61
+ puts got_lnums.inspect
62
+ end
63
+ else
64
+ puts got_lnums.inspect
65
+ end
66
+ end
67
+
68
+ require 'getoptlong'
69
+ program = File.basename($0)
70
+ opts = {
71
+ :print_source => true, # Print source file?
72
+ :print_trace => true, # Run Tracer over file?
73
+ :expect_line => true, # Source file has expected (correct) list of lines?
74
+ :print_parse => true, # Show ParseTree output?
75
+ }
76
+
77
+ getopts = GetoptLong.new(
78
+ [ '--expect', '-e', GetoptLong::NO_ARGUMENT ],
79
+ [ '--no-expect', '-E', GetoptLong::NO_ARGUMENT ],
80
+ [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
81
+ [ '--parse', '-p', GetoptLong::NO_ARGUMENT ],
82
+ [ '--no-parse', '-P', GetoptLong::NO_ARGUMENT ],
83
+ [ '--source', '-s', GetoptLong::NO_ARGUMENT ],
84
+ [ '--no-source', '-S', GetoptLong::NO_ARGUMENT ],
85
+ [ '--trace', '-t', GetoptLong::NO_ARGUMENT ],
86
+ [ '--no-trace', '-T', GetoptLong::NO_ARGUMENT ])
87
+
88
+ getopts.each do |opt, arg|
89
+ case opt
90
+ when '--help'
91
+ puts "usage
92
+ Usage: #{$program} [options] file1 file2 ...
93
+
94
+ Diagnostic program to make see what TraceLineNumbers does and compare
95
+ against other output.
96
+
97
+ options:
98
+ -e --expect Read source file expected comment (default)
99
+ -E --no-expect Don't look for source file expected comment
100
+ -p --parse Show ParseTree Output (default)
101
+ -P --no-parse Don't show ParseTree output
102
+ -s --source Show source file (default)
103
+ -S --no-source Don't print source
104
+ -t --trace Show Tracer output (default)
105
+ -T --no-trace Don't show Tracer output
106
+ "
107
+ when '--expect'
108
+ opts[:expect_line] = true
109
+ when '--no-expect'
110
+ opts[:expect_line] = false
111
+ when '--parse'
112
+ opts[:print_parse] = true
113
+ when '--no-parse'
114
+ opts[:print_parse] = false
115
+ when '--source'
116
+ opts[:print_source] = true
117
+ when '--no-source'
118
+ opts[:print_source] = false
119
+ when '--trace'
120
+ opts[:print_trace] = true
121
+ when '--no-trace'
122
+ opts[:print_trace] = false
123
+ else
124
+ puts "Unknown and ignored option #{opt}"
125
+ end
126
+ end
127
+
128
+ ARGV.each do |file|
129
+ dump_file(file, opts)
130
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pp'
4
+ begin require 'rubygems' rescue LoadError end
5
+ require 'parse_tree'
6
+
7
+ ARGV.push "-" if ARGV.empty?
8
+
9
+ parse_tree = ParseTree.new(true)
10
+
11
+ ARGV.each do |file|
12
+ ruby = file == "-" ? $stdin.read : File.read(file)
13
+ pp parse_tree.parse_tree_for_string(ruby, file).first
14
+ end
data/test/rcov-bug.rb ADDED
@@ -0,0 +1,10 @@
1
+ # [3, 10]
2
+ # Some rcov bugs.
3
+ z = "
4
+ Now is the time
5
+ "
6
+
7
+ z =~
8
+ /
9
+ 5
10
+ /ix
data/test/short-file CHANGED
@@ -1,2 +1,2 @@
1
- This is a small test file.
1
+ # This is a small test file.
2
2
 
@@ -1,7 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
- require "test/unit"
3
- require "fileutils"
4
- require "tempfile"
2
+ require 'test/unit'
3
+ require 'fileutils'
4
+ require 'tempfile'
5
+ require 'set'
6
+
7
+ # require 'rubygems'
8
+ # require 'ruby-debug'; Debugger.start
5
9
 
6
10
  # Test LineCache module
7
11
  class TestLineCache < Test::Unit::TestCase
@@ -34,7 +38,7 @@ class TestLineCache < Test::Unit::TestCase
34
38
  Dir.chdir(File.dirname(__FILE__)) do
35
39
  short_file = File.basename(__FILE__)
36
40
  test_line = 10
37
- line = LineCache::getline(__FILE__, test_line)
41
+ line = LineCache::getline(short_file, test_line)
38
42
  assert_equal(compare_lines[test_line-1], line,
39
43
  'Short filename lookup should work')
40
44
  end
@@ -69,6 +73,47 @@ class TestLineCache < Test::Unit::TestCase
69
73
  assert line
70
74
  assert_equal(true, LineCache::cached?(__FILE__),
71
75
  "file #{__FILE__} should now be cached")
76
+ assert_equal(false, LineCache::cached_script?('./short-file'),
77
+ "Should not find './short-file' in SCRIPT_LINES__")
78
+ assert_equal(true, 78 < LineCache.size(__FILE__))
79
+ Dir.chdir(File.dirname(__FILE__)) do
80
+ load('./short-file', 0)
81
+ assert_equal(true, LineCache::cached_script?('./short-file'),
82
+ "Should be able to find './short-file' in SCRIPT_LINES__")
83
+ end
84
+ end
85
+
86
+ def test_remap
87
+ LineCache::remap_file(__FILE__, 'another-name')
88
+ line1 = LineCache::getline('another-name', 1)
89
+ line2 = LineCache::getline(__FILE__, 1)
90
+ assert_equal(line1, line2, 'Both lines should be the same via remap_file')
91
+ end
92
+
93
+ def test_remap_lines
94
+ LineCache::remap_file_lines(__FILE__, 'test2', (10..11), 6)
95
+
96
+ line5 = LineCache::getline(__FILE__, 5)
97
+ LineCache::remap_file_lines(__FILE__, 'test2', 9, 5)
98
+ rline9 = LineCache::getline('test2', 9)
99
+ assert_equal(line5, rline9,
100
+ 'lines should be the same via remap_file_line - remap integer')
101
+
102
+ line6 = LineCache::getline(__FILE__, 6)
103
+ rline10 = LineCache::getline('test2', 10)
104
+ assert_equal(line6, rline10,
105
+ 'lines should be the same via remap_file_line - range')
106
+
107
+ line7 = LineCache::getline(__FILE__, 7)
108
+ rline11 = LineCache::getline('test2', 11)
109
+ assert_equal(line7, rline11,
110
+ 'lines should be the same via remap_file_line - range')
111
+
112
+ line8 = LineCache::getline(__FILE__, 8)
113
+ LineCache::remap_file_lines(__FILE__, nil, 20, 8)
114
+ rline20 = LineCache::getline(__FILE__, 20)
115
+ assert_equal(line8, rline20,
116
+ 'lines should be the same via remap_file_line - nil file')
72
117
  end
73
118
 
74
119
  def test_stat
@@ -80,10 +125,26 @@ class TestLineCache < Test::Unit::TestCase
80
125
  "file #{__FILE__} should now have a stat")
81
126
  end
82
127
 
128
+ def test_path
129
+ assert_equal(nil, LineCache::path(__FILE__),
130
+ "path for #{__FILE__} shouldn't be nil - just cleared cache.")
131
+ path = LineCache::cache(__FILE__)
132
+ assert path
133
+ assert_equal(path, LineCache::path(__FILE__),
134
+ "path of #{__FILE__} should be the same as we got before")
135
+ end
136
+
137
+ def test_trace_line_numbers
138
+ test_file = File.join(@@TEST_DIR, 'short-file')
139
+ assert_equal([], LineCache::trace_line_numbers(test_file))
140
+ test_file = File.join(@@TEST_DIR, 'rcov-bug.rb')
141
+ assert_equal([3, 10], LineCache::trace_line_numbers(test_file))
142
+ end
143
+
83
144
  def test_sha1
84
145
  test_file = File.join(@@TEST_DIR, 'short-file')
85
146
  LineCache::cache(test_file)
86
- assert_equal('d7ae2d65d8815607ddffa005e91a8add77ef4a1e',
147
+ assert_equal('1134f95ea84a3dcc67d7d1bf41390ee1a03af6d2',
87
148
  LineCache::sha1(test_file))
88
149
  end
89
150
 
data/test/test-lnum.rb ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ # $Id$
3
+ require 'test/unit'
4
+
5
+ # require 'rubygems'
6
+ # require 'ruby-debug'; Debugger.init
7
+
8
+ # Test TraceLineNumbers module
9
+ class TestLineNumbers2 < Test::Unit::TestCase
10
+ @@TEST_DIR = File.expand_path(File.dirname(__FILE__))
11
+ @@TOP_SRC_DIR = File.join(@@TEST_DIR, '..')
12
+ require File.join(@@TOP_SRC_DIR, 'lib', 'tracelines.rb')
13
+
14
+ def test_all_lnum_data
15
+ test_dir = File.join(@@TEST_DIR, 'data')
16
+ Dir.chdir(test_dir) do
17
+ Dir.glob('*.rb').sort.each do |f|
18
+ puts f
19
+ fp = File.open(f, 'r')
20
+ lines = fp.read
21
+ fp.rewind
22
+ first_line = fp.readline.chomp
23
+ fp.close()
24
+ expected_str = first_line[1..-1]
25
+ begin
26
+ expected_lnums = eval(expected_str, binding, __FILE__, __LINE__)
27
+ rescue
28
+ assert nil, "Failed reading expected values from #{f}"
29
+ else
30
+ got_lnums = TraceLineNumbers.lnums_for_str(lines)
31
+ assert_equal(expected_lnums, got_lnums)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # $Id: test-tracelines.rb 51 2008-01-26 10:18:26Z rockyb $
3
+ require 'test/unit'
4
+ require 'fileutils'
5
+ require 'tempfile'
6
+
7
+ # require 'rubygems'
8
+ # require 'ruby-debug'; Debugger.init
9
+
10
+ SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__
11
+ # Test TestLineNumbers module
12
+ class TestLineNumbers1 < Test::Unit::TestCase
13
+
14
+ @@TEST_DIR = File.expand_path(File.dirname(__FILE__))
15
+ @@TOP_SRC_DIR = File.join(@@TEST_DIR, '..', 'lib')
16
+ require File.join(@@TOP_SRC_DIR, 'tracelines.rb')
17
+
18
+ @@rcov_file = File.join(@@TEST_DIR, 'rcov-bug.rb')
19
+ File.open(@@rcov_file, 'r') {|fp|
20
+ first_line = fp.readline[1..-2]
21
+ @@rcov_lnums = eval(first_line, binding, __FILE__, __LINE__)
22
+ }
23
+
24
+ def test_for_file
25
+ rcov_lines = TraceLineNumbers.lnums_for_file(@@rcov_file)
26
+ assert_equal(@@rcov_lnums, rcov_lines)
27
+ end
28
+
29
+ def test_for_string
30
+ string = "# Some rcov bugs.\nz = \"\nNow is the time\n\"\n\nz =~ \n /\n 5\n /ix\n"
31
+ rcov_lines = TraceLineNumbers.lnums_for_str(string)
32
+ assert_equal([2, 9], rcov_lines)
33
+ end
34
+
35
+ def test_for_string_array
36
+ load(@@rcov_file, 0)
37
+ rcov_lines =
38
+ TraceLineNumbers.lnums_for_str_array(SCRIPT_LINES__[@@rcov_file])
39
+ assert_equal(@@rcov_lnums, rcov_lines)
40
+ end
41
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: linecache
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.2"
7
- date: 2008-01-15 00:00:00 -05:00
6
+ version: "0.3"
7
+ date: 2008-03-04 00:00:00 -05:00
8
8
  summary: Read file with caching
9
9
  require_paths:
10
10
  - lib
@@ -36,8 +36,19 @@ files:
36
36
  - README
37
37
  - Rakefile
38
38
  - VERSION
39
+ - ext/trace_nums.o
40
+ - ext/trace_nums.so
41
+ - ext/trace_nums.c
42
+ - ext/trace_nums.h
43
+ - ext/extconf.rb
44
+ - lib/tracelines.rb
39
45
  - lib/linecache.rb
46
+ - test/rcov-bug.rb
47
+ - test/test-tracelines.rb
48
+ - test/test-lnum.rb
40
49
  - test/test-linecache.rb
50
+ - test/parse-show.rb
51
+ - test/lnum-diag.rb
41
52
  - test/short-file
42
53
  test_files: []
43
54
 
@@ -46,6 +57,7 @@ rdoc_options: []
46
57
  extra_rdoc_files:
47
58
  - README
48
59
  - lib/linecache.rb
60
+ - lib/tracelines.rb
49
61
  executables: []
50
62
 
51
63
  extensions: []