Browse files

examples converted to the new api, tested with examples.rb which stil…

…l uses the old api

No more:
  Check(foo).is("bar")

Instead do:
  Assert(foo == "bar")
  • Loading branch information...
1 parent ab22115 commit f7b223c7dfa38b6544d67779a71c37a7b2932b5d @quackingduck quackingduck committed Apr 13, 2010
View
10 Rakefile
@@ -16,8 +16,14 @@ rescue LoadError
puts "Install jeweler to build gem"
end
-task :examples do
- ruby "-rubygems", "examples.rb"
+desc "runs the examples with the rubygems version of exemplor (you must gem install the gem for this to work)"
+task :examples, [:filter] do |_,args|
+ ruby "-rubygems", "examples.rb", (args.filter || '')
+end
+
+desc "runs the examples with the development version (i.e. the one in this dir) of exemplor"
+task :dev, [:filter] do |_,args|
+ ruby '-rubygems', '-I', 'lib', 'examples.rb', (args.filter || '')
end
task :test => :examples
View
19 examples.rb
@@ -1,6 +1,8 @@
-# uses the gem version, not the one being tested
require 'exemplor'
+# Each test runs in a subshell, exemplor is tested with exemplor but from
+# a version running in a different process. Exemplor hates unit tests.
+
eg "Exemplor.version comes from the version file" do
version = `ruby -rubygems -Ilib -e "require 'exemplor' ; print Exemplor.version"`
Check(version).is(File.read(__FILE__.sub('examples.rb','VERSION')))
@@ -24,25 +26,30 @@ def examples filenames
end
end
-# slow because each test runs in a subshell
examples %w[
no_checks
- oneliner
no_checks_non_string
- with_checks
- check_with_disambiguation
+
+ simple_show
+ multi_show
+ show_with_disambiguation
+
assertion_success
assertion_failure
assertion_success_and_failure
assertion_success_and_info
failure_halts_execution
+
helpers
with_setup
checking_nil
- dumping_classes
+ showing_classes
check_parsing
]
+# I never use this guy, candidate for removal
+examples %w[oneliner]
+
eg.helpers do
# Exemplor outputs valid yaml, for some of our assertions it's easier to use
# the parsed structure
View
8 examples/assertion_failure.rb
@@ -2,15 +2,13 @@
eg 'Assertion failure' do
list = [1, 2, 3]
- Check(list.first).is(2)
+ Assert(list.first == 2)
end
__END__
- name: Assertion failure
status: failure
result:
- - name: list.first
- status: failure
- expected: 2
- actual: 1
+ - name: list.first == 2
+ status: failure
View
5 examples/assertion_success.rb
@@ -2,7 +2,7 @@
eg 'Asserting first is first' do
list = [1, 2, 3]
- Check(list.first).is(1)
+ Assert(list.first)
end
__END__
@@ -11,5 +11,4 @@
status: success
result:
- name: list.first
- status: success
- result: 1
+ status: success
View
20 examples/assertion_success_and_failure.rb
@@ -2,24 +2,20 @@
eg 'Some successes, then a fail' do
list = [1, 2, 3]
- Check(list.first).is(1)
- Check(list[1]).is(2)
- Check(list.last).is(1) # fail!
- Check(list[2]).is(3) # would be successful but we never get here
+ Assert(list.first == 1)
+ Assert(list[1] == 2)
+ Assert(list.last == 1) # fail!
+ Assert(list[2] == 3) # would be successful but we never get here
end
__END__
- name: Some successes, then a fail
status: failure
result:
- - name: list.first
+ - name: list.first == 1
status: success
- result: 1
- - name: list[1]
+ - name: list[1] == 2
status: success
- result: 2
- - name: list.last
- status: failure
- expected: 1
- actual: 3
+ - name: list.last == 1
+ status: failure
View
19 examples/assertion_success_and_info.rb
@@ -2,26 +2,23 @@
eg 'Some successes, then an info' do
list = [1, 2, 3]
- Check(list.first).is(1)
- Check(list[1]).is(2)
- Check(list.last) # the info one
- Check(list[2]).is(3)
+ Assert(list.first ==1)
+ Assert(list[1] == 2)
+ Show(list.last) # the info one
+ Assert(list[2] == 3)
end
__END__
- name: Some successes, then an info
status: info (with checks)
result:
- - name: list.first
+ - name: list.first ==1
status: success
- result: 1
- - name: list[1]
+ - name: list[1] == 2
status: success
- result: 2
- name: list.last
status: info
result: 3
- - name: list[2]
- status: success
- result: 3
+ - name: list[2] == 3
+ status: success
View
25 examples/check_parsing.rb
@@ -5,29 +5,26 @@ def foo() 'bar' end
end
eg "plain call" do
- Check(foo)
+ Show(foo)
end
eg "whitespace after the call (seriously)" do
- Check(foo)
+ Show(foo)
end
eg "comment after the call" do
- Check(foo) # comment!
+ Show(foo) # comment!
end
eg "with brackets" do
- Check(String.new('test'))
-end
-
-eg "with brackets and is" do
- Check(String.new('test')).is("test")
+ Show(String.new('test'))
end
eg "with disambiguation" do
- Check(foo)['bar'].is('bar')
+ Show(foo)['bar']
end
+
__END__
- name: plain call
@@ -54,15 +51,9 @@ def foo() 'bar' end
- name: String.new('test')
status: info
result: test
-- name: with brackets and is
- status: success
- result:
- - name: String.new('test')
- status: success
- result: test
- name: with disambiguation
- status: success
+ status: info (with checks)
result:
- name: foo bar
- status: success
+ status: info
result: bar
View
11 examples/checking_nil.rb
@@ -1,11 +1,13 @@
require 'exemplor'
+# this is kind of a compromise, looking for better ideas
+
eg "checking nil works correctly" do
- Check(nil)
+ Show(nil)
end
eg "asserting for nil works correctly" do
- Check(nil).is(nil)
+ Assert(nil == nil)
end
__END__
@@ -19,6 +21,5 @@
- name: asserting for nil works correctly
status: success
result:
- - name: nil
- status: success
- result:
+ - name: nil == nil
+ status: success
View
8 examples/failure_halts_execution.rb
@@ -2,7 +2,7 @@
eg 'Failures halt execution' do
list = [1, 2, 3]
- Check(list.first).is(1_000_000) # fail!
+ Assert(list.first == 1_000_000) # fail!
raise "foo" # we never get here
end
@@ -11,7 +11,5 @@
- name: Failures halt execution
status: failure
result:
- - name: list.first
- status: failure
- expected: 1000000
- actual: 1
+ - name: list.first == 1_000_000
+ status: failure
View
6 examples/with_checks.rb → examples/multi_show.rb
@@ -2,9 +2,9 @@
eg 'Accessing different parts of an array' do
list = [1, 2, 3]
- Check(list.first)
- Check(list[1])
- Check(list.last)
+ Show(list.first)
+ Show(list[1])
+ Show(list.last)
end
__END__
View
4 examples/check_with_disambiguation.rb → examples/show_with_disambiguation.rb
@@ -2,9 +2,9 @@
eg 'Array appending' do
list = [1, 42]
- Check(list.last)["before append"]
+ Show(list.last)["before append"]
list << 2
- Check(list.last)["after append"]
+ Show(list.last)["after append"]
end
__END__
View
2 examples/dumping_classes.rb → examples/showing_classes.rb
@@ -5,7 +5,7 @@
end
eg "class name is printed when check is given a class" do
- Check(Object)
+ Show(Object)
end
__END__
View
15 examples/simple_show.rb
@@ -0,0 +1,15 @@
+require 'exemplor'
+
+eg 'Showing the value of some expression' do
+ list = [1, 2, 3]
+ Show(list.first)
+end
+
+__END__
+
+- name: Showing the value of some expression
+ status: info (with checks)
+ result:
+ - name: list.first
+ status: info
+ result: 1
View
20 examples/ten_percent_failures.rb
@@ -1,12 +1,12 @@
require 'exemplor'
-eg { Check(1).is(1) }
-eg { Check(2).is(2) }
-eg { Check(3).is(3) }
-eg { Check(4).is(4) }
-eg { Check(5).is(5) }
-eg { Check(6).is(6) }
-eg { Check(7).is(7) }
-eg { Check(8).is(8) }
-eg { Check(9).is(9) }
-eg { Check(false).is(true) }
+eg { Assert(1 == 1) }
+eg { Assert(2 == 2) }
+eg { Assert(3 == 3) }
+eg { Assert(4 == 4) }
+eg { Assert(5 == 5) }
+eg { Assert(6 == 6) }
+eg { Assert(7 == 7) }
+eg { Assert(8 == 8) }
+eg { Assert(9 == 9) }
+eg { Assert(false == true) }
View
12 examples/with_setup.rb
@@ -4,24 +4,22 @@
eg 'Modified env' do
@str << " bar"
- Check(@str).is("foo bar")
+ Assert(@str == 'foo bar')
end
eg 'Unmodified env' do
- Check(@str).is("foo")
+ Assert(@str == 'foo')
end
__END__
- name: Modified env
status: success
result:
- - name: "@str"
+ - name: "@str == 'foo bar'"
status: success
- result: foo bar
- name: Unmodified env
status: success
result:
- - name: "@str"
- status: success
- result: foo
+ - name: "@str == 'foo'"
+ status: success
View
40 lib/checker.rb
@@ -1,12 +1,9 @@
module Exemplor
class Check
- attr_reader :expectation, :value, :status
-
def initialize(name, value)
@name = name
@value = value
- @status = :info
end
def [](disambiguate)
@@ -18,15 +15,6 @@ def name
@name + (defined?(@disambiguate) ? " #{@disambiguate}" : '')
end
- # might be better to use throw here
- class Failure < StandardError; end
-
- def is(expectation)
- @expectation = expectation
- @status = (value == expectation) ? :success : :failure
- raise Failure if failure?
- end
-
def success?
status == :success
end
@@ -40,4 +28,32 @@ def info?
end
end
+
+ class Show < Check
+
+ attr_reader :value
+
+ def status
+ :info
+ end
+
+ end
+
+ class Assert < Check
+
+ attr_reader :status
+
+ # todo remove
+ attr_reader :value
+
+ # might be better to use throw here
+ class Failure < StandardError; end
+
+ def run
+ @status = !!@value ? :success : :failure
+ raise Failure if failure?
+ end
+
+ end
+
end
View
31 lib/environment.rb
@@ -24,7 +24,7 @@ def run(&code)
[env._status, render_checks(env._checks)]
end
- rescue Check::Failure => failure
+ rescue Assert::Failure => failure
[:failure, render_checks(env._checks)]
rescue Object => error
[:error, render_error(error)]
@@ -63,15 +63,15 @@ def render_checks(checks)
out << OrderedHash do |o|
o['name'] = check.name
o['status'] = check.status.to_s
- o['result'] = render_value check.value
+ o['result'] = render_value check.value if check.info?
end
end
if failure
out << OrderedHash do |o|
o['name'] = failure.name
o['status'] = failure.status.to_s
- o['expected'] = failure.expectation
- o['actual'] = render_value failure.value
+ # o['expected'] = failure.expectation
+ # o['actual'] = render_value(failure.value)
end
end
out
@@ -101,6 +101,29 @@ def Check(value)
_checks << check
check
end
+
+ def Show(value)
+ name = extract_argstring_from :Show, caller
+ check = Show.new(name, value)
+ _checks << check
+ check
+ end
+
+ def Assert(value)
+ name = extract_argstring_from :Assert, caller
+ check = Assert.new(name, value)
+ _checks << check
+ check.run
+ check
+ end
+
+ def extract_argstring_from name, call_stack
+ file, line_number = call_stack.first.match(/^(.+):(\d+)/).captures
+ line = File.readlines(file)[line_number.to_i - 1].strip
+ argstring = line[/#{name}\((.+?)\)\s*($|#|\[|\})/,1]
+ raise "unable to extract name for #{name} from #{file} line #{line_number}:\n #{line}" unless argstring
+ argstring
+ end
def _status
(:success if _checks.all? { |c| c.success? }) ||

0 comments on commit f7b223c

Please sign in to comment.