require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'rid/design_document' describe "Makros" do before do @doc = Rid::DesignDocument.new end describe "inject" do describe "code makro" do it "should expand code" do @doc.hash = { "hash" => { "key" => "// !code code.js" }, "lib" => { "code.js" => "value" } } @doc.inject_makros! @doc.hash["hash"]["key"].should == "value" end it "should not remove code that follows" do @doc.hash = { "hash" => { "key" => "// !code code.js\nvalue" }, "lib" => { "code.js" => "value" } } @doc.inject_makros! @doc.hash["hash"]["key"].should == "value\nvalue" end it "should store injection" do @doc.hash = { "hash" => { "key" => "// !code code.js" }, "lib" => { "code.js" => "value" } } @doc.inject_makros! @doc.hash["injections"].should == [{ "makro" => "code", "target" => "hash/key", "lib" => "code.js", "start" => 0, "size" => 5 }] end end describe "json makro" do it "should expand json" do @doc.hash = { "hash" => { "key" => "// !json json.json" }, "lib" => { "json.json" => "value" } } @doc.inject_makros! @doc.hash["hash"]["key"].should == 'var json = "value";' end it "should not remove code that follows" do @doc.hash = { "hash" => { "key" => "// !json json.json\nvalue" }, "lib" => { "json.json" => "value" } } @doc.inject_makros! @doc.hash["hash"]["key"].should == "var json = \"value\";\nvalue" end it "should store injection" do @doc.hash = { "hash" => { "key" => "// !json json.json" }, "lib" => { "json.json" => "value" } } @doc.inject_makros! @doc.hash["injections"].should == [{ "makro" => "json", "target" => "hash/key", "lib" => "json.json", "start" => 0, "size" => 19 }] end end end describe "reject" do describe "code makro" do it "should reject code" do @doc.hash = { "lib" => { "code.js" => "value" }, "key" => "// !code code.js" } @doc.inject_makros! @doc.reject_makros! @doc.hash["key"].should == "// !code code.js" end it "should reject nested code" do @doc.hash = { "lib" => { "hash" => { "code.js" => "value" } }, "key" => "// !code hash/code.js" } @doc.inject_makros! @doc.reject_makros! @doc.hash["key"].should == "// !code hash/code.js" end end describe "json makro" do it "should reject json" do @doc.hash = { "lib" => { "json.json" => "value" }, "key" => "// !json json.json" } @doc.inject_makros! @doc.reject_makros! @doc.hash["key"].should == "// !json json.json" end it "should reject nested json" do @doc.hash = { "lib" => { "hash" => { "json.json" => "value" } }, "key" => "// !json hash/json.json" } @doc.inject_makros! @doc.reject_makros! @doc.hash["key"].should == "// !json hash/json.json" end end describe "many makros" do it "should reject properly" do @doc.hash = { "lib" => { "code.js" => "value", "other.js" => "value" }, "key" => "// !code code.js\n// !code other.js" } @doc.inject_makros! @doc.hash["injections"].should == [{"makro"=>"code", "size"=>5, "lib"=>"code.js", "start"=>0, "target"=>"key"}, {"makro"=>"code", "size"=>5, "lib"=>"other.js", "start"=>6, "target"=>"key"}] @doc.reject_makros! @doc.hash["key"].should == "// !code code.js\n// !code other.js" end end end end