require File.expand_path(File.dirname(__FILE__) + '/spec_helper') require 'em-http' def compiler_test file, ext, src, dir = nil, &test FakeFS.without { dir ||= Dir.mktmpdir begin path = "#{dir}/#{file}" File.open(path, "w+"){|f| f.write(src)} cf = Slinky::Compilers.cfile_for_file(path) called_back = false $stdout.should_receive(:puts).with("Compiled #{path}".foreground(:green)) cf.compile{|cpath, _, _, error| error.should == nil cpath.nil?.should == false cpath.end_with?(ext).should == true test.call(File.open(cpath).read).should == true called_back = true } called_back.should == true ensure FileUtils.rm_rf(dir) end } end describe "Compilers" do context "SassCompiler" do it "should be able to compile SASS files" do src = < console.log(x)} test.do("Hello, world") eos compiler_test("test.coffee", ".js", src){|s| s.include?("function(x) {") } end end context "HamlCompiler" do it "should be able to compile .haml files" do src = <Hello!") } end end context "JSXCompiler" do it "should be able to compile .jsx files" do src = <<-EOF /** @jsx React.DOM */ React.renderComponent(

Hello, world!

, document.getElementById('example') ); EOF compiler_test("test.jsx", ".js", src){|s| s.include?("Hello, world!") } end end context "TypescriptCompiler" do it "should be able to compile .ts files" do src = <<-EOF function greeter(person: string) { return "Hello, " + person; } var user = "Jane User"; EOF compiler_test("test.ts", ".js", src){|s| s.include?("function greeter(person)") } end it "should be able to reference definition files" do dts = <<-EOF declare var Test: {x: number} EOF src = <<-EOF /// console.log(Test.x + 5); EOF FakeFS.without { dir = Dir.mktmpdir FileUtils.mkdir("#{dir}/types") File.open("#{dir}/types/my.d.ts", "w+"){|f| f.write(dts)} compiler_test("test.ts", ".js", src, dir){|s| s.include?("Test.x") } } end end end