spec/ffi-inliner/inliner_spec.rb in ffi-inliner-0.2.2 vs spec/ffi-inliner/inliner_spec.rb in ffi-inliner-0.2.3

- old
+ new

@@ -1,8 +1,8 @@ require File.expand_path(File.join(File.dirname(__FILE__), "../spec_helper")) -describe Inliner do +describe 'Inliner' do before do module Foo extend Inliner end @@ -73,11 +73,11 @@ Foo.updated_func.should == 4 end - it 'should be configured using the block form' do + it 'should be configured using the block form' do module Foo inline do |builder| builder.c %q{ int func_1() { @@ -112,10 +112,31 @@ end end my_struct = MyStruct.new Foo.use_my_struct(my_struct).should == my_struct.to_ptr end + + it 'should allow users to include header files' do + module Foo + inline do |builder| + builder.include "stdio.h" + builder.include "local_header.h", :quoted => true + builder.code.should == "#include <stdio.h>\n#include \"local_header.h\"\n" + builder.stub!(:build) + end + end + end + + it 'should allow users to add libraries' do + module Foo + inline do |builder| + builder.library 'foolib1', 'foolib2' + builder.stub!(:build) + end + end + end + it 'should generate C struct from FFI::Struct' do pending do class MyStruct < FFI::Struct layout :a, :int, \ :b, :char, @@ -136,29 +157,37 @@ EOC end end end end -# it 'should use different compiler as specified in the configuration block' do -# tcc = mock('tcc', :exists? => true, :compile => nil) -# Inliner::Compilers::TCC.should_receive(:new).and_return(tcc) -# module Foo -# inline do |builder| -# builder.code = "int func_1() { return 0; }" -# builder.compiler = Inliner::Compilers::TCC -# end -# end -# end -# it 'should be configured using the hash form' do -# tcc = mock('tcc', :exists? => true, :compile => nil) -# Inliner::Compilers::TCC.should_receive(:new).and_return(tcc) -# module Foo -# inline "int func_1() { return 1; }", :compiler => Inliner::Compilers::TCC -# end -# end + it 'should use different compiler as specified in the configuration block' do + module Foo + inline do |builder| + builder.use_compiler Inliner::Compilers::TCC + builder.c "int func_1() { return 1 + 1; }" + end + end + Foo.func_1.should == 2 + end + it 'should return the current compiler' do + module Foo + inline do |builder| + builder.compiler.should == Inliner::Compilers::GCC + end + end + end + + # it 'should be configured using the hash form' do + # tcc = mock('tcc', :exists? => true, :compile => nil) + # Inliner::Compilers::TCC.should_receive(:new).and_return(tcc) + # module Foo + # inline "int func_1() { return 1; }", :compiler => Inliner::Compilers::TCC + # end + # end + it 'should raise errors' do lambda { module Foo inline "int boom(" end @@ -167,20 +196,57 @@ module Foo inline "int boom() { printf \"Hello\" }" end }.should raise_error(/Compile error/) end - -end -describe Inliner::Compilers::Compiler do - before do - class DummyCC < Inliner::Compilers::Compiler - def cmd - "dummycc -shared" + describe 'Compiler' do + before do + class DummyCC < Inliner::Compilers::Compiler + def cmd + "dummycc -shared" + end end end + it 'should return the progname' do + DummyCC.new.progname.should == 'dummycc' + end end - it 'should return the progname' do - DummyCC.new.progname.should == 'dummycc' + + describe 'GPlusPlus compiler' do + + it 'should compile and link a shim C library that encapsulates C++ code' do + + module Foo + inline do |builder| + builder.use_compiler Inliner::Compilers::GPlusPlus + builder.c_raw <<-code + #include <iostream> + #include <string> + using namespace std; + class Greeter { + public: + Greeter(); + string say_hello(); + }; + Greeter::Greeter() { }; + string Greeter::say_hello() { + return "Hello foos!"; + }; + code + builder.map 'char *' => 'string' + builder.c <<-code + const char* say_hello() + { + Greeter greeter; + return greeter.say_hello().c_str(); + } + code + end + end + Foo.say_hello.should == 'Hello foos!' + end + end + end +