diff --git a/Rakefile b/Rakefile index 45e3fac..3a59e22 100644 --- a/Rakefile +++ b/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 \ No newline at end of file diff --git a/examples.rb b/examples.rb index 275017f..5670e0b 100644 --- a/examples.rb +++ b/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 diff --git a/examples/assertion_failure.rb b/examples/assertion_failure.rb index 7abf25d..2fdb083 100644 --- a/examples/assertion_failure.rb +++ b/examples/assertion_failure.rb @@ -2,7 +2,7 @@ eg 'Assertion failure' do list = [1, 2, 3] - Check(list.first).is(2) + Assert(list.first == 2) end __END__ @@ -10,7 +10,5 @@ - name: Assertion failure status: failure result: - - name: list.first - status: failure - expected: 2 - actual: 1 \ No newline at end of file + - name: list.first == 2 + status: failure \ No newline at end of file diff --git a/examples/assertion_success.rb b/examples/assertion_success.rb index 90cf19b..06e577e 100644 --- a/examples/assertion_success.rb +++ b/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 \ No newline at end of file + status: success \ No newline at end of file diff --git a/examples/assertion_success_and_failure.rb b/examples/assertion_success_and_failure.rb index ee05079..46b92d5 100644 --- a/examples/assertion_success_and_failure.rb +++ b/examples/assertion_success_and_failure.rb @@ -2,10 +2,10 @@ 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__ @@ -13,13 +13,9 @@ - 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 \ No newline at end of file + - name: list.last == 1 + status: failure \ No newline at end of file diff --git a/examples/assertion_success_and_info.rb b/examples/assertion_success_and_info.rb index fec9a13..e3de00c 100644 --- a/examples/assertion_success_and_info.rb +++ b/examples/assertion_success_and_info.rb @@ -2,10 +2,10 @@ 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__ @@ -13,15 +13,12 @@ - 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 \ No newline at end of file + - name: list[2] == 3 + status: success \ No newline at end of file diff --git a/examples/check_parsing.rb b/examples/check_parsing.rb index ed31679..6db9b0c 100644 --- a/examples/check_parsing.rb +++ b/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 \ No newline at end of file diff --git a/examples/checking_nil.rb b/examples/checking_nil.rb index 06b2c8a..90cb41f 100644 --- a/examples/checking_nil.rb +++ b/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: \ No newline at end of file + - name: nil == nil + status: success \ No newline at end of file diff --git a/examples/failure_halts_execution.rb b/examples/failure_halts_execution.rb index baf3212..229c186 100644 --- a/examples/failure_halts_execution.rb +++ b/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 \ No newline at end of file + - name: list.first == 1_000_000 + status: failure \ No newline at end of file diff --git a/examples/with_checks.rb b/examples/multi_show.rb similarity index 82% rename from examples/with_checks.rb rename to examples/multi_show.rb index 24d453d..7f4c833 100644 --- a/examples/with_checks.rb +++ b/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__ diff --git a/examples/check_with_disambiguation.rb b/examples/show_with_disambiguation.rb similarity index 76% rename from examples/check_with_disambiguation.rb rename to examples/show_with_disambiguation.rb index f26e47b..043d8e7 100644 --- a/examples/check_with_disambiguation.rb +++ b/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__ diff --git a/examples/dumping_classes.rb b/examples/showing_classes.rb similarity index 92% rename from examples/dumping_classes.rb rename to examples/showing_classes.rb index 81614f1..5320f33 100644 --- a/examples/dumping_classes.rb +++ b/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__ diff --git a/examples/simple_show.rb b/examples/simple_show.rb new file mode 100644 index 0000000..282c747 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/examples/ten_percent_failures.rb b/examples/ten_percent_failures.rb index d37c2e5..803b30b 100644 --- a/examples/ten_percent_failures.rb +++ b/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) } \ No newline at end of file +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) } \ No newline at end of file diff --git a/examples/with_setup.rb b/examples/with_setup.rb index 0f97ed1..cf4b1c9 100644 --- a/examples/with_setup.rb +++ b/examples/with_setup.rb @@ -4,11 +4,11 @@ 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__ @@ -16,12 +16,10 @@ - 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 \ No newline at end of file + - name: "@str == 'foo'" + status: success \ No newline at end of file diff --git a/lib/checker.rb b/lib/checker.rb index cd6c806..463c8ec 100644 --- a/lib/checker.rb +++ b/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 \ No newline at end of file diff --git a/lib/environment.rb b/lib/environment.rb index fd75025..dda6139 100644 --- a/lib/environment.rb +++ b/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? }) ||