Skip to content

Commit

Permalink
fix: an issue with proc's ! operator (https://is.gd/N0K9yn)
Browse files Browse the repository at this point in the history
chore: bump the major version 0.1.0 → 1.0.0

Signed-off-by: Serge Bedzhyk <smileart21@gmail.com>
  • Loading branch information
smileart committed Sep 14, 2019
1 parent 94efab9 commit 13f09c6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
.vscode
60 changes: 31 additions & 29 deletions lib/testrocket.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
# frozen_string_literal: true

##
# TestRocket Module to refine lambdas an use them for lightweight tests
#
module TestRocket
VERSION = "0.1.0"
VERSION = '1.0.0'

extend Module.new { attr_accessor :out }

module Live
def _test(a, b); send((call rescue()) ? a : b) end
def _show(r); (TestRocket.out || STDERR) << r + "\n"; r end
def _pass; " OK" end
def _fail; " FAIL @ #{source_location * ':'}" end
def _pend; "PENDING '#{call}' @ #{source_location * ':'}" end
def _desc; " FIRE '#{call}'!" end
end

module Dummy
def _test(a, b); end
def _show(r); end
def _pend; end
def _desc; end
end

def +@; _show _test :_pass, :_fail end
def -@; _show _test :_fail, :_pass end
def ~; _show _pend end
def !; _show _desc end
end
refine Proc do
# Include TestRocket methods WITHOUT implementation selected
Proc.send :include, TestRocket

Proc.send :include, TestRocket
# If we're in a production environment, the tests shall do nothing.
if ENV['RACK_ENV'] == 'production' ||
(defined?(Rails) && Rails.env.production?) ||
ENV['RAILS_ENV'] == 'production'
def _test(a, b); end
def _show(r); end
def _pend; end
def _desc; end
else
def _test(a, b); send((call rescue()) ? a : b) end
def _show(r); (TestRocket.out || STDERR) << r + "\n"; r end
def _pass; ' OK' end
def _fail; " FAIL @ #{source_location * ':'}" end
def _pend; "PENDING '#{call}' @ #{source_location * ':'}" end
def _desc; " FIRE '#{call}'!" end
end

# If we're in a production environment, the tests shall do nothing.
if ENV['RACK_ENV'] == 'production' || (defined?(Rails) && Rails.env.production?) || ENV['RAILS_ENV'] == 'production'
Proc.send :include, TestRocket::Dummy
else
Proc.send :include, TestRocket::Live
end
def +@; _show _test :_pass, :_fail end
def -@; _show _test :_fail, :_pass end
def ~; _show _pend end
def !; _show _desc end
end
end
89 changes: 59 additions & 30 deletions test/test_testrocket.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
require_relative 'helper'

describe TestRocket do
it "should find emptiness non-truthful by default" do
(+->{}).must_match(/FAIL/)
(+->{}).must_match("#{__FILE__}:#{__LINE__}")
end

it "should pass a simple positive assertion" do
(+->{ 2 + 2 == 4 }).must_match(/OK/)
end
class RefinementTest
using TestRocket

it "should pass a simple negative assertion" do
(-->{ 2 + 2 == 5 }).must_match(/OK/)
end

it "should fail a simple erroneous assertion" do
(+->{ 2 + 2 == 5 }).must_match(/FAIL/)
(+->{ 2 + 2 == 5 }).must_match("#{__FILE__}:#{__LINE__}")
end
def self.test!
describe TestRocket do
it 'should find emptiness non-truthful by default' do
(+->{}).must_match(/FAIL/)
(+->{}).must_match("#{__FILE__}:#{__LINE__}")
end

it "should fail a simple correct assertion assumed to fail" do
(-->{ 2 + 2 == 4 }).must_match(/FAIL/)
(-->{ 2 + 2 == 4 }).must_match("#{__FILE__}:#{__LINE__}")
end

it "should give a pending notice" do
(~->{ "a pending test" }).must_match(/PENDING/)
(~->{ "a pending test" }).must_match(/a pending test/)
(~->{ "a pending test" }).must_match("#{__FILE__}:#{__LINE__}")
it 'should pass a simple positive assertion' do
(+->{ 2 + 2 == 4 }).must_match(/OK/)
end

it 'should pass a simple negative assertion' do
(-->{ 2 + 2 == 5 }).must_match(/OK/)
end

it 'should fail a simple erroneous assertion' do
(+->{ 2 + 2 == 5 }).must_match(/FAIL/)
(+->{ 2 + 2 == 5 }).must_match("#{__FILE__}:#{__LINE__}")
end

it 'should fail a simple correct assertion assumed to fail' do
(-->{ 2 + 2 == 4 }).must_match(/FAIL/)
(-->{ 2 + 2 == 4 }).must_match("#{__FILE__}:#{__LINE__}")
end

it 'should give a pending notice' do
(~->{ "a pending test" }).must_match(/PENDING/)
(~->{ "a pending test" }).must_match(/a pending test/)
(~->{ "a pending test" }).must_match("#{__FILE__}:#{__LINE__}")
end

it 'should fire a description rocket' do
(!->{ "a description" }).must_match(/FIRE/)
(!->{ "a description" }).must_match(/a description/)
end

it 'would influence Ruby Proc if TestRocket used explitly' do
(
ok = ->() { nil }
!ok
).must_equal(" FIRE ''!")
end
end
end

it "should fire a description rocket" do
(!->{ "a description" }).must_match(/FIRE/)
(!->{ "a description" }).must_match(/a description/)
end

class NoRefinementTest
def self.test!
describe 'Without `using TestRocket`' do
it "should not influence global Ruby scope and other libs" do
(
ok = ->() { nil }
!ok
).must_equal(false)
end
end
end
end

RefinementTest.test!
NoRefinementTest.test!
16 changes: 8 additions & 8 deletions testrocket.gemspec
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "testrocket"
$:.push File.expand_path('../lib', __FILE__)
require 'testrocket'

Gem::Specification.new do |s|
s.name = "testrocket"
s.name = 'testrocket'
s.version = TestRocket::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Peter Cooper","Christoph Grabo"]
s.email = ["git@peterc.org","chris@dinarrr.com"]
s.homepage = "http://github.com/peterc/testrocket"
s.authors = %w[Peter Cooper Christoph Grabo]
s.email = %w[git@peterc.org chris@dinarrr.com]
s.homepage = 'http://github.com/peterc/testrocket'
s.summary = %q{A super lightweight lamdba-based testing library for Ruby}
s.description = %q{A super lightweight lamdba-based testing library for Ruby}

s.rubyforge_project = "testrocket"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ['lib']
end

0 comments on commit 13f09c6

Please sign in to comment.