rdoctest 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,7 +25,7 @@ Use the CLI:
25
25
  .
26
26
  Finished in ... seconds.
27
27
 
28
- 1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
28
+ 1 tests, 6 assertions, 0 failures, 0 errors, 0 skips
29
29
 
30
30
 
31
31
  == Examples
@@ -64,7 +64,7 @@ Rdoctest expects well-formatted RDoc. This means:
64
64
  # >> 1 + 1
65
65
  # => 2
66
66
  #
67
- # More complex:
67
+ # More complex, testing output:
68
68
  #
69
69
  # >> def hello_world
70
70
  # >> puts "Hello, world!"
@@ -88,6 +88,7 @@ Rdoctest's CLI works a lot like Ruby's.
88
88
  Usage: rdoctest [options] [file ...]
89
89
  -Idirectory
90
90
  -rlibrary
91
+ -f Don't automatically require files
91
92
  ...
92
93
 
93
94
  If you're testing a complex project, make sure you prepare necessary libraries
@@ -96,6 +97,12 @@ and load paths:
96
97
  % rdoctest -Ilib -rmylibrary lib/**/*.rb
97
98
 
98
99
 
100
+ If your application handles the loading of files, suppress Rdoctest's autoload
101
+ mechanism:
102
+
103
+ $ rdoctest -f -r./config/environment app/**/*.rb
104
+
105
+
99
106
  === Rake
100
107
 
101
108
  Rdoctest comes with a Rake task that you can load in your Rakefile.
@@ -104,12 +111,12 @@ Rdoctest comes with a Rake task that you can load in your Rakefile.
104
111
  Rdoctest::Task.new
105
112
 
106
113
 
107
- And run in your project:
114
+ Running it is simple.
108
115
 
109
116
  % rake doctest
110
117
 
111
118
 
112
- It comes with a similar configuration to Rake's TestTask.
119
+ It implements a configuration similar to Rake's TestTask.
113
120
 
114
121
  Rdoctest::Task.new :test do |t|
115
122
  t.libs << 'lib' # The 'lib' directory is loaded by default,
@@ -117,17 +124,34 @@ It comes with a similar configuration to Rake's TestTask.
117
124
  end
118
125
 
119
126
 
127
+ === Rails
128
+
129
+ I haven't done much testing in Rails yet, but the following should work:
130
+
131
+ First, update your Gemfile to include the gem in your test group.
132
+
133
+ group :test do
134
+ gem 'rdoctest'
135
+ end
136
+
137
+
138
+ Run <tt>bundle install</tt>, and you can immediately invoke the CLI.
139
+
140
+ % rdoctest -f -r./config/environment app/**/*.rb lib/**/*.rb
141
+
142
+
120
143
  == Roadmap
121
144
 
122
- * Better detection of code blocks (the 3-space indent is too restrictive).
145
+ * Better detection of code blocks (the 2-space indent is too restrictive).
123
146
  * Test plain text files (READMEs, for example).
124
147
  * Autotest integration?
148
+ * Better Rails support?
125
149
  * Test shell snippets (beginning with <tt>$</tt> and <tt>%</tt>)?
126
150
 
127
151
 
128
152
  == Prior Art
129
153
 
130
- {rubydoctest}[http://github.com/tablatom/rubydoctest].
154
+ {rubydoctest}[http://github.com/tablatom/rubydoctest]
131
155
 
132
156
 
133
157
  == License
@@ -3,14 +3,21 @@ require 'optparse'
3
3
  lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
  $LOAD_PATH.unshift lib
5
5
 
6
+ config = { :load_path => [] }
7
+
6
8
  options = OptionParser.new do |option|
7
9
  option.banner = "Usage: #{basename = File.basename $0} [options] [file ...]"
8
10
  option.on('-Idirectory') { |v|
9
- $LOAD_PATH.concat v.split File::PATH_SEPARATOR
11
+ directories = v.split File::PATH_SEPARATOR
12
+ $LOAD_PATH.concat directories
13
+ config[:load_path].concat directories
10
14
  }
11
15
  option.on('-rlibrary') { |v|
12
16
  require v
13
17
  }
18
+ option.on('-f', "Don't automatically require files") { |v|
19
+ config[:force] = v
20
+ }
14
21
  option.on('--version') {
15
22
  require 'rdoctest/version'
16
23
  puts "#{basename} #{Rdoctest::Version::VERSION}"
@@ -29,3 +36,4 @@ if ARGF.argv.empty?
29
36
  end
30
37
 
31
38
  load 'rdoctest/runner.rb'
39
+ Rdoctest::Runner.new(config).run
@@ -1,2 +1,4 @@
1
+ require 'rdoctest/version'
2
+
1
3
  module Rdoctest # :nodoc:
2
4
  end
@@ -1,6 +1,6 @@
1
1
  require 'stringio'
2
2
  require 'strscan'
3
- require 'test/unit'
3
+ require 'rdoctest/test_case'
4
4
 
5
5
  module Rdoctest
6
6
  # The Runner takes RDoc example text and creates tests for them.
@@ -29,15 +29,17 @@ module Rdoctest
29
29
  #
30
30
  # Use ellipses for partial matches.
31
31
  #
32
- # >> a
32
+ # >> ok
33
33
  # NameError: undefined local variable or method `a'...
34
34
  class Runner
35
35
  @@ruby = /(.+?)# =>([^\n]+\n)/m
36
36
  @@irb = /((?:^>> [^\n]+\n)+)((?:(?!^(?:>>|=>))[^\n]+\n)*)(^=> [^\n]+)?/m
37
37
 
38
- attr_reader :files
38
+ attr_reader :files, :options
39
+
40
+ def initialize options = {}
41
+ @options = options
39
42
 
40
- def initialize
41
43
  @files = {}
42
44
  end
43
45
 
@@ -56,15 +58,10 @@ module Rdoctest
56
58
  lineno += 1
57
59
 
58
60
  next in_comment = nil unless line.sub! /^ *# ?/, ''
59
- in_comment = lineno if line =~ /^={1,6} \S/
61
+ in_comment = lineno if line =~ /^={1,6} \S/
60
62
  in_comment ||= lineno
61
63
 
62
- if in_test
63
- in_test = false if line !~ /^(?: {2,}|$)/
64
- else
65
- in_test = true if line =~ /^ {2}\S/
66
- end
67
-
64
+ in_test = in_test ? line =~ (/^(?: {2,}|$)/) : line =~ /^ {2}\S/
68
65
  line = "\n" unless in_test
69
66
  ((files[filename] ||= {})[in_comment] ||= '') << line if in_comment
70
67
  end
@@ -78,25 +75,12 @@ module Rdoctest
78
75
  class_name = 'Rdoctest' if class_name.empty?
79
76
  require_filename filename
80
77
 
81
- test_class = Class.new Test::Unit::TestCase do
78
+ test_class = Class.new Rdoctest::TestCase do
82
79
  lineno_and_lines.each_pair do |lineno, lines|
83
80
  next unless lines =~ /\S/ # /(?:^|# )=>/
84
81
  lines.gsub! /^ /, ''
85
82
 
86
- define_method "assert_eval" do |expected, result, filename, lineno|
87
- if expected.gsub!(/\.{3,}/, '.*')
88
- assertion, expected = 'match', /#{expected}/
89
- else
90
- assertion = 'equal'
91
- end
92
-
93
- instance_eval <<ASSERTION, filename, lineno
94
- assert_#{assertion} #{expected.inspect}, #{result.inspect}
95
- ASSERTION
96
- end
97
-
98
83
  define_method "test_line_#{lineno}" do
99
- assertions = []
100
84
  scanner = StringScanner.new lines
101
85
 
102
86
  binding = send :binding
@@ -104,13 +88,12 @@ ASSERTION
104
88
  code_lineno = lineno + scanner.pre_match.count("\n")
105
89
  expected_lineno = code_lineno + scanner[1].count("\n")
106
90
 
107
- result = eval scanner[1], binding #, filename, code_lineno
91
+ result = eval scanner[1], binding
108
92
  assert_eval scanner[2].strip, result.inspect, filename,
109
93
  expected_lineno
110
94
  end
111
-
112
95
  scanner.pos = 0
113
- binding = send :binding
96
+
114
97
  while scanner.skip_until(@@irb)
115
98
  code_lineno = lineno + scanner.pre_match.count("\n")
116
99
  output_lineno = code_lineno + scanner[2].to_s.count("\n")
@@ -125,9 +108,10 @@ ASSERTION
125
108
  $stdout, stdout = stdout, $stdout
126
109
  end
127
110
 
128
- stdout.rewind
129
- output = stdout.read
130
- assert_eval scanner[2], output, filename, output_lineno
111
+ stdout.rewind and output = stdout.read
112
+ unless output.empty?
113
+ assert_eval scanner[2], output, filename, output_lineno
114
+ end
131
115
 
132
116
  if scanner[3]
133
117
  expected = scanner[3].sub(/^=> /, '').strip
@@ -144,9 +128,9 @@ ASSERTION
144
128
  end
145
129
 
146
130
  def require_filename filename
147
- require filename.gsub(%r{^(?:lib)/|.rb$}, '') unless filename == '-'
131
+ return if options[:force] || filename == '-'
132
+ load_path = options[:load_path].join '|'
133
+ require filename.gsub(%r{^(?:#{load_path})/|.rb$}, '')
148
134
  end
149
135
  end
150
136
  end
151
-
152
- Rdoctest::Runner.new.run
@@ -31,7 +31,7 @@ module Rdoctest
31
31
  def define
32
32
  desc name ? "Run doctests for #{name}" : 'Run doctests'
33
33
  task name do
34
- ruby "#{run_code} #{ruby_opts_string} #{file_list_string}"
34
+ system "#{run_code} #{ruby_opts_string} #{file_list_string}"
35
35
  end
36
36
  end
37
37
 
@@ -0,0 +1,19 @@
1
+ require 'test/unit'
2
+
3
+ module Rdoctest
4
+ class TestCase < Test::Unit::TestCase
5
+ private
6
+
7
+ def assert_eval expected, result, filename, lineno
8
+ if expected.gsub!(/\.{3,}/, '.*')
9
+ assertion, expected = 'match', /#{expected}/
10
+ else
11
+ assertion = 'equal'
12
+ end
13
+
14
+ instance_eval <<ASSERTION, filename, lineno
15
+ assert_#{assertion} #{expected.inspect}, #{result.inspect}
16
+ ASSERTION
17
+ end
18
+ end
19
+ end
@@ -2,8 +2,9 @@ module Rdoctest
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 1
5
+ PATCH = 2
6
+ BETA = nil
6
7
 
7
- VERSION = [MAJOR, MINOR, PATCH].compact.join('.').freeze
8
+ VERSION = [MAJOR, MINOR, PATCH, BETA].compact.join('.').freeze
8
9
  end
9
10
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Stephen Celis
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-05 00:00:00 -06:00
17
+ date: 2010-12-08 00:00:00 -06:00
18
18
  default_executable: rdoctest
19
19
  dependencies: []
20
20
 
@@ -31,6 +31,7 @@ files:
31
31
  - Rakefile
32
32
  - lib/rdoctest/runner.rb
33
33
  - lib/rdoctest/task.rb
34
+ - lib/rdoctest/test_case.rb
34
35
  - lib/rdoctest/version.rb
35
36
  - lib/rdoctest.rb
36
37
  - bin/rdoctest