Skip to content

Commit

Permalink
Add boxing spec
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Sep 6, 2010
1 parent 09c0cff commit cf7ffa4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
64 changes: 64 additions & 0 deletions spec/boxing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "boxing support" do

def box expected
expected = remove_common_indent(expected)
unless expected.lines.first.strip =~ %r,^- \(id\)box:\(([^)]+)\)(\w+) \{$,
raise StandardError, "Invalid first line: #{expected.lines.first}"
end
type_string, var_name = $1.strip, $2.strip
type = XDry::VarType.parse(type_string)
raise "Invalid variable type '#{type_string}'" if type.nil?
boxer = XDry::Boxing.converter_for(type)
raise "No boxer available for variable type '#{type_string}'" if boxer.nil?
lines = XDry::Emitter.capture do |o|
o.block "- (id)box:(#{type.to_s})#{var_name}" do
o << "return " + boxer.box(o, var_name, "#{var_name}fix") + ";"
end
end
result = lines.join("\n").gsub("\t", " ")
result.strip.should == expected.strip
end

it "should box NSString verbatim" do
box <<-END
- (id)box:(NSString *)foo {
return foo;
}
END
end

it "should box int via NSNumber" do
box <<-END
- (id)box:(int)foo {
return [NSNumber numberWithInt:foo];
}
END
end

it "should box NSInteger via NSNumber" do
box <<-END
- (id)box:(NSInteger)foo {
return [NSNumber numberWithInteger:foo];
}
END
end

it "should box float via NSNumber" do
box <<-END
- (id)box:(float)foo {
return [NSNumber numberWithFloat:foo];
}
END
end

it "should box double via NSNumber" do
box <<-END
- (id)box:(double)foo {
return [NSNumber numberWithDouble:foo];
}
END
end

end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
require 'spec'
require 'spec/autorun'

def remove_common_indent content
first_line_indent = content.split("\n", 2).first.gsub(/\S.*$/, '')
content.gsub(/^#{first_line_indent}/, '')
end

def xdry gens, content
config = XDry::Config.new
config.verbose = ((ENV['VERBOSE'] || '0').to_i != 0)
Expand Down

0 comments on commit cf7ffa4

Please sign in to comment.