exemplor 2010.0.1 → 2010.0.2

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/Rakefile CHANGED
@@ -17,7 +17,7 @@ rescue LoadError
17
17
  end
18
18
 
19
19
  task :examples do
20
- ruby "examples.rb"
20
+ ruby "-rubygems", "examples.rb"
21
21
  end
22
22
 
23
23
  task :test => :examples
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2010.0.1
1
+ 2010.0.2
data/examples.rb CHANGED
@@ -6,17 +6,17 @@ require 'exemplor'
6
6
  eg.helpers do
7
7
 
8
8
  def run_example(name, args = nil)
9
- `ruby -Ilib examples/#{name}.rb#{' ' + args if args}`
9
+ `ruby -rubygems -Ilib examples/#{name}.rb#{' ' + args if args}`
10
10
  end
11
11
 
12
12
  def extract_expected(name)
13
13
  File.read("examples/#{name}.rb").split('__END__').last
14
14
  end
15
-
15
+
16
16
  def expected_and_actual(example_name)
17
17
  [extract_expected(example_name).strip, run_example(example_name).strip]
18
18
  end
19
-
19
+
20
20
  def check_output_matches_expected_for(example_name)
21
21
  expected_output, output = expected_and_actual(example_name)
22
22
  Check(output).is(expected_output)
@@ -25,7 +25,7 @@ eg.helpers do
25
25
  end
26
26
 
27
27
  eg "version matches file" do
28
- version = `ruby -Ilib -e "require 'exemplor' ; print Exemplor.version"`
28
+ version = `ruby -rubygems -Ilib -e "require 'exemplor' ; print Exemplor.version"`
29
29
  Check(version).is(File.read(__FILE__.sub('examples.rb','VERSION')))
30
30
  end
31
31
 
@@ -45,6 +45,7 @@ eg { check_output_matches_expected_for :check_with_disambiguation }
45
45
  eg { check_output_matches_expected_for :assertion_success }
46
46
  eg { check_output_matches_expected_for :assertion_failure }
47
47
  eg { check_output_matches_expected_for :assertion_success_and_failure }
48
+ eg { check_output_matches_expected_for :assertion_success_and_info }
48
49
  eg { check_output_matches_expected_for :helpers }
49
50
  eg { check_output_matches_expected_for :with_setup }
50
51
  eg { check_output_matches_expected_for :checking_nil }
@@ -0,0 +1,27 @@
1
+ require 'exemplor'
2
+
3
+ eg 'Some successes, then an info' do
4
+ list = [1, 2, 3]
5
+ Check(list.first).is(1)
6
+ Check(list[1]).is(2)
7
+ Check(list.last) # the info one
8
+ Check(list[2]).is(3)
9
+ end
10
+
11
+ __END__
12
+
13
+ - name: Some successes, then a fail
14
+ status: failure
15
+ result:
16
+ - name: list.first
17
+ status: success
18
+ result: 1
19
+ - name: list[1]
20
+ status: success
21
+ result: 2
22
+ - name: list.last
23
+ status: info
24
+ result: 3
25
+ - name: list[2]
26
+ status: success
27
+ result: 3
data/lib/exemplor.rb CHANGED
@@ -18,64 +18,64 @@ class String
18
18
  end
19
19
 
20
20
  module Exemplor
21
-
21
+
22
22
  def self.version() File.read(__FILE__.sub('lib/exemplor.rb','VERSION')) end
23
-
23
+
24
24
  class ExampleDefinitionError < StandardError ; end
25
-
25
+
26
26
  class Check
27
-
27
+
28
28
  attr_reader :expectation, :value
29
-
29
+
30
30
  def initialize(name, value)
31
31
  @name = name
32
32
  @value = value
33
33
  end
34
-
34
+
35
35
  def [](disambiguate)
36
36
  @disambiguate = disambiguate
37
37
  self
38
38
  end
39
-
39
+
40
40
  def name
41
41
  @name + (defined?(@disambiguate) ? " #{@disambiguate}" : '')
42
42
  end
43
-
43
+
44
44
  def is(expectation)
45
45
  @expectation = expectation
46
46
  end
47
-
47
+
48
48
  def status
49
49
  return :info unless defined? @expectation
50
50
  @value == @expectation ? :success : :failure
51
51
  end
52
-
52
+
53
53
  def success?
54
54
  status == :success
55
55
  end
56
-
56
+
57
57
  def failure?
58
58
  status == :failure
59
59
  end
60
-
60
+
61
61
  def info?
62
62
  status == :info
63
63
  end
64
-
64
+
65
65
  end
66
-
66
+
67
67
  class ResultPrinter
68
-
68
+
69
69
  attr_reader :name,:status,:result,:stderr
70
-
70
+
71
71
  def initialize(name,status,result,stderr)
72
72
  @name,@status,@result,@stderr = name,status,result,stderr
73
73
  end
74
-
74
+
75
75
  def failure?
76
76
  [:error,:failure].include?(self.status)
77
77
  end
78
-
78
+
79
79
  def yaml
80
80
  hsh = OrderedHash do |o|
81
81
  o['name'] = self.name
@@ -88,7 +88,7 @@ module Exemplor
88
88
  end
89
89
  YAML.without_header([hsh])# prints an array
90
90
  end
91
-
91
+
92
92
  def fancy
93
93
  # •∙ are inverted in my terminal font (Incosolata) so I'm swapping them
94
94
  require 'term/ansicolor'
@@ -96,7 +96,8 @@ module Exemplor
96
96
  when :info : blue format_info("• #{name}", result)
97
97
  when :infos
98
98
  formatted_result = result.map do |r|
99
- format_info("• #{r['name']}", r['result']).rstrip
99
+ # TODO: successful ones should be green
100
+ format_info("#{{'success' => '✓', 'info' => '•' }[r['status']]} #{r['name']}", r['result']).rstrip
100
101
  end.join("\n")
101
102
  blue("∙ #{name}\n#{formatted_result.indent}")
102
103
  when :success
@@ -114,30 +115,30 @@ module Exemplor
114
115
  color(:red, "☠ #{name}\n#{class_and_message.indent}\n#{backtrace.indent}")
115
116
  end
116
117
  end
117
-
118
+
118
119
  def blue(str) color(:blue,str) end
119
120
  def green(str) color(:green,str) end
120
-
121
+
121
122
  def color(color, str)
122
123
  [Term::ANSIColor.send(color), str, Term::ANSIColor.reset].join
123
124
  end
124
-
125
+
125
126
  # whatahack
126
127
  def format_info(str, result)
127
128
  YAML.without_header({'FANCY' => result}).sub('FANCY', str)
128
129
  end
129
-
130
+
130
131
  end
131
-
132
+
132
133
  class ExampleEnv
133
-
134
+
134
135
  class << self
135
-
136
+
136
137
  alias_method :helpers, :class_eval
137
138
  attr_accessor :setup_block
138
-
139
+
139
140
  def setup(&blk) self.setup_block = blk end
140
-
141
+
141
142
  # runs the block in the example environment, returns triple:
142
143
  # [status, result, stderr]
143
144
  def run(&code)
@@ -145,13 +146,13 @@ module Exemplor
145
146
  stderr = StringIO.new
146
147
  status, result = begin
147
148
  real_stderr = $stderr ; $stderr = stderr # swap stderr
148
-
149
+
149
150
  env.instance_eval(&self.setup_block) if self.setup_block
150
151
  value = env.instance_eval(&code)
151
- result = env._status == :info ?
152
+ result = env._status == :info ?
152
153
  render_value(value) : render_checks(env._checks)
153
154
  [env._status, result]
154
-
155
+
155
156
  rescue Object => error
156
157
  [:error, render_error(error)]
157
158
  ensure
@@ -159,14 +160,14 @@ module Exemplor
159
160
  end
160
161
  [status, result, stderr.rewind && stderr.read]
161
162
  end
162
-
163
+
163
164
  # -- these "render" methods could probably be factored away
164
-
165
+
165
166
  # yaml doesn't want to print a class
166
167
  def render_value(value)
167
168
  value.kind_of?(Class) ? value.inspect : value
168
169
  end
169
-
170
+
170
171
  def render_checks(checks)
171
172
  failure = nil
172
173
  out = []
@@ -197,15 +198,15 @@ module Exemplor
197
198
  o['backtrace'] = error.backtrace
198
199
  end
199
200
  end
200
-
201
+
201
202
  end
202
-
203
+
203
204
  attr_accessor :_checks
204
-
205
+
205
206
  def initialize
206
207
  @_checks = []
207
208
  end
208
-
209
+
209
210
  def Check(value)
210
211
  file, line_number = caller.first.match(/^(.+):(\d+)/).captures
211
212
  line = File.readlines(file)[line_number.to_i - 1].strip
@@ -214,26 +215,26 @@ module Exemplor
214
215
  _checks << check
215
216
  check
216
217
  end
217
-
218
+
218
219
  def _status
219
220
  (:info if _checks.empty?) ||
220
- (:infos if _checks.all? { |c| c.info? }) ||
221
+ (:failure if _checks.any? { |c| c.failure? }) ||
221
222
  (:success if _checks.all? { |c| c.success? }) ||
222
- (:failure if _checks.any? { |c| c.failure? })
223
+ :infos
223
224
  end
224
-
225
+
225
226
  end
226
-
227
+
227
228
  class Examples
228
-
229
+
229
230
  def initialize
230
231
  @examples = OrderedHash.new
231
232
  end
232
-
233
+
233
234
  def add(name, &body)
234
235
  @examples[name] = body
235
236
  end
236
-
237
+
237
238
  def run(patterns)
238
239
  fails = 0
239
240
  # unoffically supports multiple patterns
@@ -247,33 +248,33 @@ module Exemplor
247
248
  end
248
249
  (fails.to_f/examples_to_run.size)*100
249
250
  end
250
-
251
+
251
252
  def list(patterns)
252
253
  patterns = Regexp.new(patterns.join('|'))
253
254
  list = @examples.keys.select { |name| name =~ patterns }
254
255
  print YAML.without_header(list)
255
256
  end
256
-
257
+
257
258
  end
258
-
259
+
259
260
  class << self
260
-
261
+
261
262
  def examples
262
263
  @examples ||= Examples.new
263
264
  end
264
-
265
+
265
266
  def extract_example_file(caller_trace)
266
267
  @example_file ||= caller_trace.first.split(":").first
267
268
  end
268
-
269
+
269
270
  # attr_reader :example_file
270
-
271
+
271
272
  def run_directly?
272
273
  @example_file == $0
273
274
  end
274
-
275
+
275
276
  end
276
-
277
+
277
278
  end
278
279
 
279
280
  # Defines an example. After definition, an example is an entry in the
@@ -301,4 +302,4 @@ at_exit do
301
302
  exit Exemplor.examples.run(args)
302
303
  end
303
304
  end
304
- end
305
+ end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exemplor
3
3
  version: !ruby/object:Gem::Version
4
- version: 2010.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 2010
7
+ - 0
8
+ - 2
9
+ version: 2010.0.2
5
10
  platform: ruby
6
11
  authors:
7
12
  - Myles Byrne
@@ -9,29 +14,37 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-01-06 00:00:00 +11:00
17
+ date: 2010-03-27 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: orderedhash
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 0
30
+ - 6
23
31
  version: 0.0.6
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: term-ansicolor
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ">="
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 0
44
+ - 3
33
45
  version: 1.0.3
34
- version:
46
+ type: :runtime
47
+ version_requirements: *id002
35
48
  description:
36
49
  email: myles@myles.id.au
37
50
  executables: []
@@ -40,6 +53,7 @@ extensions: []
40
53
 
41
54
  extra_rdoc_files:
42
55
  - README
56
+ - TODO
43
57
  files:
44
58
  - .gitignore
45
59
  - README
@@ -51,6 +65,7 @@ files:
51
65
  - examples/assertion_failure.rb
52
66
  - examples/assertion_success.rb
53
67
  - examples/assertion_success_and_failure.rb
68
+ - examples/assertion_success_and_info.rb
54
69
  - examples/check_parsing.rb
55
70
  - examples/check_with_disambiguation.rb
56
71
  - examples/checking_nil.rb
@@ -76,18 +91,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
91
  requirements:
77
92
  - - ">="
78
93
  - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
79
96
  version: "0"
80
- version:
81
97
  required_rubygems_version: !ruby/object:Gem::Requirement
82
98
  requirements:
83
99
  - - ">="
84
100
  - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
85
103
  version: "0"
86
- version:
87
104
  requirements: []
88
105
 
89
106
  rubyforge_project:
90
- rubygems_version: 1.3.5
107
+ rubygems_version: 1.3.6
91
108
  signing_key:
92
109
  specification_version: 3
93
110
  summary: A light-weight, low-fi way to provide executable usage examples of your code.
@@ -96,6 +113,7 @@ test_files:
96
113
  - examples/assertion_failure.rb
97
114
  - examples/assertion_success.rb
98
115
  - examples/assertion_success_and_failure.rb
116
+ - examples/assertion_success_and_info.rb
99
117
  - examples/check_parsing.rb
100
118
  - examples/check_with_disambiguation.rb
101
119
  - examples/checking_nil.rb