diff --git a/vendor/plugins/silverline/lib/silverline/essential/html.rb b/vendor/plugins/silverline/lib/silverline/essential/html.rb index 887722b..1f788c9 100755 --- a/vendor/plugins/silverline/lib/silverline/essential/html.rb +++ b/vendor/plugins/silverline/lib/silverline/essential/html.rb @@ -1,11 +1,11 @@ module Silverline::Essential::Html + require 'erb' def silverlight_include_tag(options = :defaults) templatify("head.html.erb", binding) end def silverlight_object(options = {}) - require 'erb' defaults = { :start => "app", :debug => false, @@ -32,29 +32,32 @@ def silverlight_object(options = {}) retval << templatify("body.html.erb", binding) end - private - - def http_host - request = session.cgi.instance_variable_get(:"@request") - return session.cgi.env_table['HTTP_HOST'] if request.nil? # for WEBrick - request.params["HTTP_HOST"] # Mongrel - end +private - def generate_init_params(options) - options.delete(:properties) - p = options.collect { |k,v| "#{k.to_s}=#{v.to_s}" }.join(", ") - p << ", http_host=#{http_host}" - end + def http_host + request = session.cgi.instance_variable_get(:"@request") + return session.cgi.env_table['HTTP_HOST'] if request.nil? # for WEBrick + request.params["HTTP_HOST"] # Mongrel + end - def public_xap_file - "/#{Silverline.const_get(:XAP_FILE).split("/").last}" - end - - def jsonify(o) - o.to_json.gsub("\"", "'").gsub(",", "==>") - end - - def templatify(path, b) - ERB.new(File.open("#{Silverline::PLUGIN_ROOT}/templates/#{path}"){|f| f.read}).result(b) - end -end + def generate_init_params(options) + options.delete(:properties) + p = options.collect { |k,v| "#{k.to_s}=#{v.to_s}" }.join(", ") + p << ", http_host=#{http_host}" + end + + def public_xap_file + "/#{Silverline.const_get(:XAP_FILE).split("/").last}" + end + + def jsonify(o) + o.to_json.gsub("\"", "'").gsub(",", "==>") + end + + def templatify(path, b) + body = File.open("#{RAILS_ROOT}/vendor/plugins/silverline/templates/#{path}"){|f| f.read} + puts body.inspect + ::ERB.new(body).result(b) + end + +end \ No newline at end of file diff --git a/vendor/plugins/silverline/spec/silverline/essential/html_spec.rb b/vendor/plugins/silverline/spec/silverline/essential/html_spec.rb index d1fe265..e229c7d 100644 --- a/vendor/plugins/silverline/spec/silverline/essential/html_spec.rb +++ b/vendor/plugins/silverline/spec/silverline/essential/html_spec.rb @@ -8,6 +8,7 @@ Object.instance_eval{remove_const :HtmlTesttHost} if defined? HtmlTesttHost class HtmlTesttHost include Silverline::Essential::Html + attr_accessor :session end describe "Public functionality of essential HTML" do @@ -16,37 +17,77 @@ class HtmlTesttHost end it "should render a silverlight include tag" do - @html.should_receive(:templatify).with("head.html.erb", anything) - @html.silverlight_include_tag + @html.should_receive(:templatify).with("head.html.erb", an_instance_of(Binding)).and_return("Head HTML") + @html.silverlight_include_tag.should == "Head HTML" end # TODO: test more of this ... it "should render a silverlight control" do - @html.should_receive(:require).with('erb') - @html.should_receive(:templatify).with("body.html.erb", anything).and_return "" - @html.silverlight_object + @html.should_receive(:templatify).with("body.html.erb", an_instance_of(Binding)).and_return("Body HTML") + result = @html.silverlight_object + result['Body HTML'].should_not be_nil + result['position: absolute'].should_not be_nil end end describe "Private functionality of essential HTML" do before(:each) do @html = HtmlTesttHost.new + @html.session = mock('Session') + @cgi = mock('cgi') + @request = mock('request') + + Object.instance_eval{remove_const :File} if defined?(File) + File = mock("FileClass", :null_object => true) + file = mock("file") + file.stub!(:read).and_return "This is a string" + File.stub!(:open).and_yield(file) + + Object.instance_eval{remove_const :ERB} if defined?(ERB) + ERB = mock("ERBClass") + @erb = mock("ERB") end - - it "should know the HTTP_HOST" - + + it "should know the HTTP_HOST for mongrel" do + @html.session.stub!(:cgi).and_return @cgi + @cgi.stub!(:instance_variable_get).with(:@request).and_return(@request) + @request.stub!(:params).and_return({"HTTP_HOST" => "http://foo.com"}) + + @html.send(:http_host).should == "http://foo.com" + end + it "should know the HTTP_HOST for WEBrick" do + @html.session.stub!(:cgi).and_return @cgi + @cgi.stub!(:instance_variable_get).with(:@request).and_return(nil) + @cgi.stub!(:env_table).and_return({"HTTP_HOST" => "http://foo.com"}) + + @html.send(:http_host).should == "http://foo.com" + end + it "should generate initParams" do @html.should_receive(:http_host).and_return "baz" + result = @html.send(:generate_init_params, {:foo => "hi", :bar => "bye", :properties => {:boom => 42}}) - result = "foo=hi, bar=bye, http_host=baz" + result.should == "foo=hi, bar=bye, http_host=baz" end - + it "should know the XAP file location" do Silverline.should_receive(:const_get).with(:XAP_FILE).and_return "path/to/foo.xap" + @html.send(:public_xap_file).should == "/foo.xap" end - - it "should know how to put JSON in initParams" - - it "should know how to render a template" + + it "should know how to put JSON in initParams" do + h = {:a => "hi", :b => "bye"} + h.should_receive(:to_json).and_return("{'a': 'hi', 'b': 'bye'}") + + @html.send(:jsonify, h).should == "{'a': 'hi'==> 'b': 'bye'}" + end + + it "should know how to render a template" do + File.should_receive(:open).with("#{RAILS_ROOT}/vendor/plugins/silverline/templates/body.html.erb").and_return "I'm the file contents" + ERB.should_receive(:new).with("I'm the file contents").and_return @erb + @erb.should_receive(:result).with(an_instance_of(Binding)).and_return("I'm the template string!") + + @html.send(:templatify, "body.html.erb", @html.send(:binding)).should == "I'm the template string!" + end end \ No newline at end of file diff --git a/vendor/plugins/silverline/spec/spec_init.rb b/vendor/plugins/silverline/spec/spec_init.rb index 9759c10..07e2aa2 100644 --- a/vendor/plugins/silverline/spec/spec_init.rb +++ b/vendor/plugins/silverline/spec/spec_init.rb @@ -3,8 +3,4 @@ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path) require 'spec' -require 'spec/mocks' - -require 'rubygems' -require 'ruby-debug' -#Debugger.start \ No newline at end of file +require 'spec/mocks' \ No newline at end of file