Skip to content

Commit

Permalink
Merge 7821ea0 into 7423689
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jan 2, 2014
2 parents 7423689 + 7821ea0 commit fb89566
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 64 deletions.
18 changes: 16 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
env:
- TRAVIS=true
before_install:
- gem update bundler
- bundle --version
- gem update --system 2.1.11
- gem --version
bundler_args: --without development
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.0
- jruby-head
- rbx
- ruby-head
26 changes: 22 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in naught.gemspec
gemspec
gem 'rake'

group :development do
platforms :ruby_19, :ruby_20, :ruby_21 do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
end
end

group :test do
gem "libnotify"
gem 'coveralls', require: false
gem 'libnotify'
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
gem 'coveralls', :require => false
gem 'rspec', '~> 2.14'
end

platforms :rbx do
gem 'rubinius-coverage', '~> 2.0'
gem 'rubysl', '~> 2.0'
gem 'rubysl-json', '~> 2.0'
end

# Specify your gem's dependencies in naught.gemspec
gemspec
14 changes: 14 additions & 0 deletions lib/naught/basic_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Naught
if defined? ::BasicObject
class BasicObject < ::BasicObject
end
else
class BasicObject #:nodoc:
keep = %w[! != == __id__ __send__ equal? instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined]
instance_methods.each do |method_name|
undef_method(method_name) unless keep.include?(method_name)
end
end
end
end

28 changes: 19 additions & 9 deletions lib/naught/null_class_builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'naught/basic_object'
require 'naught/null_class_builder/conversions_module'

module Naught
Expand All @@ -10,8 +11,8 @@ module Commands

def initialize
@interface_defined = false
@base_class = BasicObject
@inspect_proc = ->{ "<null>" }
@base_class = Naught::BasicObject
@inspect_proc = lambda { "<null>" }
@stub_strategy = :stub_method_returning_nil
define_basic_methods
end
Expand Down Expand Up @@ -70,11 +71,20 @@ def method_missing(method_name, *args, &block)
end
end

def respond_to_missing?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
if RUBY_VERSION >= '1.9'
def respond_to_missing?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
end
else
def respond_to?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
end
end

############################################################################
Expand All @@ -88,7 +98,7 @@ def black_hole
end

def respond_to_any_message
defer(prepend: true) do |subject|
defer(:prepend => true) do |subject|
subject.module_eval do
def respond_to?(*)
true
Expand Down Expand Up @@ -136,7 +146,7 @@ def initialize(*)
end

def define_basic_class_methods
defer(class: true) do |subject|
defer(:class => true) do |subject|
subject.module_eval do
class << self
alias get new
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'complex'
require 'naught/null_class_builder/command'

module Naught::NullClassBuilder::Commands
Expand All @@ -8,8 +9,13 @@ def call
def to_s; ""; end
def to_i; 0; end
def to_f; 0.0; end
def to_c; 0.to_c; end
def to_r; 0.to_r; end
if RUBY_VERSION >= '1.9'
def to_c; 0.to_c; end
def to_r; 0.to_r; end
else
def to_c; Complex(0); end
def to_r; Rational(0); end
end
def to_a; []; end
def to_h; {}; end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/naught/null_class_builder/commands/mimic.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'naught/basic_object'
require 'naught/null_class_builder/command'

module Naught::NullClassBuilder::Commands
Expand All @@ -11,7 +12,7 @@ def initialize(builder, class_to_mimic, options={})
@include_super = options.fetch(:include_super) { true }

builder.base_class = root_class_of(class_to_mimic)
builder.inspect_proc = -> { "<null:#{class_to_mimic}>" }
builder.inspect_proc = lambda { "<null:#{class_to_mimic}>" }
builder.interface_defined = true
end

Expand All @@ -29,7 +30,7 @@ def root_class_of(klass)
if klass.ancestors.include?(Object)
Object
else
BasicObject
Naught::BasicObject
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/naught/null_class_builder/commands/singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Naught::NullClassBuilder::Commands
class Singleton < Naught::NullClassBuilder::Command
def call
defer(class: true) do |subject|
defer(:class => true) do |subject|
require 'singleton'
subject.module_eval do
include ::Singleton
Expand Down
6 changes: 5 additions & 1 deletion lib/naught/null_class_builder/commands/traceable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def call
attr_reader :__file__, :__line__

def initialize(options={})
backtrace = options.fetch(:caller) { Kernel.caller(4) }
if RUBY_VERSION >= '2.1' || RUBY_PLATFORM == 'java'
backtrace = options.fetch(:caller) { Kernel.caller(3) }
else
backtrace = options.fetch(:caller) { Kernel.caller(4) }
end
@__file__, line, _ = backtrace[0].split(':')
@__line__ = line.to_i
end
Expand Down
4 changes: 2 additions & 2 deletions lib/naught/null_class_builder/conversions_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def Null(object=:nothing_passed)
case object
when NullObjectTag then object
when :nothing_passed, *null_equivs
null_class.get(caller: caller(1))
null_class.get(:caller => caller(1))
else raise ArgumentError, "#{object.inspect} is not null!"
end
end
Expand All @@ -28,7 +28,7 @@ def Maybe(object=nil, &block)
case object
when NullObjectTag then object
when *null_equivs
null_class.get(caller: caller(1))
null_class.get(:caller => caller(1))
else
object
end
Expand Down
5 changes: 0 additions & 5 deletions naught.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 2.14"
spec.add_development_dependency "guard"
spec.add_development_dependency "guard-rspec"
spec.add_development_dependency "guard-bundler"
end
2 changes: 1 addition & 1 deletion spec/base_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Naught.build do |b|
default_base_class = b.base_class
end
expect(default_base_class).to eq(BasicObject)
expect(default_base_class).to eq(Naught::BasicObject)
end

describe 'singleton null object' do
Expand Down
2 changes: 1 addition & 1 deletion spec/basic_null_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
expect(null_class.get.class).to be(null_class)
end

end
end
2 changes: 1 addition & 1 deletion spec/blackhole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
expect(null.foobaz).to be(null)
expect(null << "bar").to be(null)
end
end
end
2 changes: 1 addition & 1 deletion spec/conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
expect(null.to_r).to eq(Rational(0))
expect(null.to_h).to eq({})
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
expect(Actual{ConvertableNull.new}).to be_nil
expect(Actual{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/just_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
expect{Just{nil}.class}.to raise_error(ArgumentError)
expect(Just{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/maybe_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
expect(Maybe{nil}.class).to eq(ConvertableNull)
expect(Maybe{"foo"}).to eq("foo")
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/null_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
expect(null.__file__).to eq(__FILE__)
end

end
end
2 changes: 1 addition & 1 deletion spec/implicit_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
expect(null.to_str).to eq("")
end

end
end
19 changes: 1 addition & 18 deletions spec/mimic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def notify_of_overdue_books(titles); puts 'Notifying...'; end
describe 'with include_super: false' do
let(:mimic_class) {
Naught.build do |b|
b.mimic LibraryPatron, include_super: false
b.mimic LibraryPatron, :include_super => false
end
}

Expand Down Expand Up @@ -103,20 +103,3 @@ def self.it_behaves_like_a_black_hole_mimic
end

end

describe 'mimicking a non-Object-derived class' do
class MinimalClass < BasicObject
end

subject(:null) { mimic_class.new }
let(:mimic_class) {
Naught.build do |b|
b.mimic MinimalClass
end
}

it 'generates a BasicObject-derived null class' do
expect(BasicObject).to be === null
expect(Object).not_to be === null
end
end
4 changes: 2 additions & 2 deletions spec/naught_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def y; 42; end
end

def make_null
trace_null_class.get(caller: caller(1))
trace_null_class.get(:caller => caller(1))
end

it 'can accept custom backtrace info' do
Expand Down Expand Up @@ -85,7 +85,7 @@ def to_s
TestNull = Naught.build

describe 'a named null object class' do
it 'has named ancestor modules' do
it 'has named ancestor modules', :pending => rubinius? do
expect(TestNull.ancestors[0..2].map(&:name)).to eq([
'TestNull',
'TestNull::Customizations',
Expand Down
8 changes: 4 additions & 4 deletions spec/pebble_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ def call_method_inside_nested_block(thing)
end

context "when is called from a block" do
it "prints the indication of a block" do
it "prints the indication of a block", :pending => jruby? || rubinius? || ruby_18? do
expect(test_output).to receive(:puts).twice.
with(/from block/)
Caller.new.call_method_inside_block(null)
end

it "prints the name of the method that has the block" do
expect(test_output).to receive(:puts).twice.
with(/in call_method_inside_block$/)
with(/call_method_inside_block$/)
Caller.new.call_method_inside_block(null)
end
end

context "when is called from many levels blocks" do
it "prints the indication of blocks and its levels" do
it "prints the indication of blocks and its levels", :pending => jruby? || rubinius? || ruby_18? do
expect(test_output).to receive(:puts).exactly(4).times.
with(/from block \(2 levels\)/)
Caller.new.call_method_inside_nested_block(null)
end

it "prints the name of the method that has the block" do
expect(test_output).to receive(:puts).exactly(4).times.
with(/in call_method_inside_nested_block$/)
with(/call_method_inside_nested_block$/)
Caller.new.call_method_inside_nested_block(null)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/singleton_null_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
expect(null_class.get).to be null_class.instance
end
it 'permits arbitrary arguments to be passed to .get' do
null_class.get(42, foo: "bar")
null_class.get(42, :foo => "bar")
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
end

require 'naught'
Dir[File.join(GEM_ROOT, "spec", "support", "**/*.rb")].each { |f| require f }
Dir[File.join(GEM_ROOT, "spec", "support", "**/*.rb")].each { |f| require f }
2 changes: 1 addition & 1 deletion spec/support/convertable_null.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ConvertableNull = Naught.build do |b|
b.null_equivalents << ""
b.traceable
end
end
3 changes: 3 additions & 0 deletions spec/support/jruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def jruby?
RUBY_PLATFORM == 'java'
end
3 changes: 3 additions & 0 deletions spec/support/rubinius.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def rubinius?
defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
end
Loading

0 comments on commit fb89566

Please sign in to comment.