Skip to content

Commit

Permalink
100% passing specs for essential (missing Xap test though)
Browse files Browse the repository at this point in the history
  • Loading branch information
jschementi committed Jun 22, 2008
1 parent b2b1bd5 commit 1687fc1
Show file tree
Hide file tree
Showing 19 changed files with 267 additions and 79 deletions.
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -3,7 +3,7 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'spec'
debugger

require 'spec/rails'

Spec::Runner.configure do |config|
Expand Down
8 changes: 7 additions & 1 deletion vendor/plugins/silverline/init.rb
@@ -1 +1,7 @@
require 'silverline'
require 'patch/ruby'

require 'silverline'

require 'silverline/essential'
require 'silverline/visualize'
require 'silverline/teleport'
10 changes: 3 additions & 7 deletions vendor/plugins/silverline/lib/silverline.rb
@@ -1,7 +1,7 @@
require 'patch/ruby'

module Silverline
RAILS_VIEWS = "#{RAILS_ROOT}/app/views"
RAILS_CTRLRS = "#{RAILS_ROOT}/app/controllers"
RAILS_MODELS = "#{RAILS_ROOT}/app/models"
XAP_FILE = "#{RAILS_ROOT}/public/client.xap"
CLIENT_ROOT = "#{RAILS_ROOT}/lib/client"
TMP_CLIENT = "#{RAILS_ROOT}/tmp/client"
Expand All @@ -13,8 +13,4 @@ module FileExtensions
XAML = "xaml"
RB = "rb"
end
end

require 'silverline/essential'
require 'silverline/visualize'
require 'silverline/teleport'
end
4 changes: 4 additions & 0 deletions vendor/plugins/silverline/lib/silverline/essential.rb
@@ -1,5 +1,9 @@
module Silverline::Essential

# make sure to undefine Xap if it already exists; this file should always
# have first crack at defining it
instance_eval { remove_const :Xap } if defined?(Xap)

# What should generate the Xap?
#
# Uses rubyzip to generate the XAP. This is the default behavior, so
Expand Down
44 changes: 22 additions & 22 deletions vendor/plugins/silverline/lib/silverline/essential/generator.rb
Expand Up @@ -2,6 +2,7 @@
# http://www.jhorman.org/FileSystemWatcher/index.html
require "filesystemwatcher"

require 'fileutils'
require 'silverline/essential/xap'

def logger
Expand All @@ -10,52 +11,51 @@ def logger

# Generates the XAP on modification of watched files
module Silverline::Essential::Generator
include Silverline

# List of files/directories to watch for modification.
# Triggers generation of the Silverlight package (XAP)
def self.register
create_directories
watcher = FileSystemWatcher.new
@watcher = FileSystemWatcher.new

[ Silverline::CLIENT_ROOT, Silverline::PLUGIN_CLIENT, "#{Silverline::PLUGIN_ROOT}/public",
"#{RAILS_ROOT}/public/ironruby"].each do |dir|
@watcher.addDirectory dir
end

watcher.addDirectory CLIENT_ROOT
watcher.addDirectory PLUGIN_CLIENT
watcher.addDirectory "#{PLUGIN_ROOT}/public"
watcher.addDirectory "#{RAILS_ROOT}/public/ironruby"

# TODO: watch all client controllers, as well as all views
watcher.addFile "#{RAILS_ROOT}/app/controllers/client_controller.rb"
watcher.addDirectory RAILS_VIEWS
@watcher.addFile "#{RAILS_ROOT}/app/controllers/client_controller.rb"
@watcher.addDirectory Silverline::RAILS_VIEWS

watcher.sleepTime = 1
watcher.start { |status, file| generate }
@watcher.sleepTime = 1
@watcher.start { |status, file| generate }
generate # make sure to generate XAPs the first time
end

def self.generate
logger.info "Silverline: Generating client.xap"
%W(#{XAP_FILE}).each do |file|
%W(#{Silverline::XAP_FILE}).each do |file|
File.delete(file) if File.exists?(file)
end

# First copy the plugin's client folder to tmp folder
FileUtils.cp_r "#{Silverline::PLUGIN_CLIENT}/.", TMP_CLIENT
FileUtils.cp_r "#{Silverline::PLUGIN_CLIENT}/.", Silverline::TMP_CLIENT

# TODO: copy all controllers, views, and models to tmp folder
FileUtils.mkdir_p "#{TMP_CLIENT}/controllers"
FileUtils.mkdir_p "#{TMP_CLIENT}/views"
FileUtils.cp 'app/controllers/client_controller.rb', "#{TMP_CLIENT}/controllers"
FileUtils.cp_r "#{RAILS_VIEWS}/.", "#{TMP_CLIENT}/views"
FileUtils.mkdir_p "#{Silverline::TMP_CLIENT}/controllers"
FileUtils.mkdir_p "#{Silverline::TMP_CLIENT}/views"
FileUtils.cp 'app/controllers/client_controller.rb', "#{Silverline::TMP_CLIENT}/controllers"
FileUtils.cp_r "#{Silverline::RAILS_VIEWS}/.", "#{Silverline::TMP_CLIENT}/views"

# Lastly, client root wins
FileUtils.cp_r "#{CLIENT_ROOT}/.", TMP_CLIENT
Silverline::Essential::XAP.new(XAP_FILE, TMP_CLIENT).generate
FileUtils.cp_r "#{Silverline::CLIENT_ROOT}/.", Silverline::TMP_CLIENT
Silverline::Essential::XAP.new(Silverline::XAP_FILE, Silverline::TMP_CLIENT).generate

FileUtils.rm_r TMP_CLIENT
FileUtils.rm_r Silverline::TMP_CLIENT
end

def self.create_directories
FileUtils.mkdir_p TMP_CLIENT
FileUtils.mkdir_p CLIENT_ROOT
FileUtils.mkdir_p Silverline::TMP_CLIENT
FileUtils.mkdir_p Silverline::CLIENT_ROOT
end
end
1 change: 1 addition & 0 deletions vendor/plugins/silverline/lib/silverline/visualize.rb
Expand Up @@ -2,6 +2,7 @@ module Silverline::Visualize
# Nothing on purpose =)
end

require 'silverline/visualize/helpers'
require 'silverline/visualize/rendering'

ActionController::Base.class_eval do
Expand Down
@@ -1,3 +1,6 @@
module Silverline::Visualize::Helpers; end

require 'silverline/visualize/helpers/types'
module Silverline::Visualize::Helpers
include Types
end
end
Expand Up @@ -2,10 +2,10 @@
module Silverline::Visualize::Helpers::Formats

def ___ag_render_ruby_partial(filename, options, other)
return silverlight_object options.merge({
return silverlight_object(options.merge({
:start => "app_render_rb",
:rb_to_run => "views/#{@__cpath}/#{filename}"
})
}))
end

def ___ag_ruby_filename(config)
Expand All @@ -20,7 +20,7 @@ def ___ag_render_xaml_partial(filename, options, other)
}
xaml_type = options[:properties].delete(:type)
defaults[:xaml_type] = xaml_type unless xaml_type.nil?
return silverlight_object options.merge(defaults)
return silverlight_object(options.merge(defaults))
end

def ___ag_xaml_filename(config)
Expand Down
@@ -1,7 +1,10 @@
require 'silverline/visualize/helpers/configuration'
require 'silverline/visualize/helpers/formats'

# Render based on the template-key given in render(options)
module Silverline::Visualize::Helpers::Templates
include Configuration
include Formats
include Silverline::Visualize::Helpers::Configuration
include Silverline::Visualize::Helpers::Formats

# TODO: Ruby/XAML files should be supported actions
def __ag_render_a_action(key, template, options, other, &block)
Expand Down
@@ -1,6 +1,8 @@
require 'silverline/visualize/helpers/templates'

# Render based on the type passed to render's options parameter
module Silverline::Visualize::Helpers::Types
include Templates
include Silverline::Visualize::Helpers::Templates

def _ag_render_for_hash(options, other, &block)
[:action, :partial, :inline, :template].each do |key|
Expand Down
Expand Up @@ -24,6 +24,6 @@ def _ag_render_without_silverlight(key, template, options, other, &block)
end

# See visualize/helpers for what gets included here
include Helpers
include ::Silverline::Visualize::Helpers

end
15 changes: 4 additions & 11 deletions vendor/plugins/silverline/spec/matchers/relative_rails_root.rb
@@ -1,5 +1,3 @@
RAILS_ROOT = "config/.."

module CustomPathMatchers
class BeRelativeTo
def initialize(expected)
Expand All @@ -8,24 +6,19 @@ def initialize(expected)

def matches?(target)
@target = target
@target == "#{RAILS_ROOT}#{@expected}"
@target == "#{RAILS_ROOT}/#{@expected}"
end

def failure_message
"expected #{@target.inspect} to be '#{RAILS_ROOT}#{@expected}'"
"expected #{@target.inspect} to be '#{RAILS_ROOT}/#{@expected}'"
end

def negative_failure_message
"expected #{@target.inspect} not to be '#{RAILS_ROOT}#{@expected}'"
"expected #{@target.inspect} not to be '#{RAILS_ROOT}/#{@expected}'"
end
end

def be_relative_to(expected)
BeRelativeTo.new(expected)
end
end

#Spec::Runner.configure do |config|
# config.include(CustomGameMatchers)
#end

end
@@ -0,0 +1,40 @@
require File.dirname(__FILE__) + "/../../spec_nonrails.rb"

module Silverline
module Essential
module Html
end
end
end

module ActionView
class Base
end
end

describe Silverline::Essential do

before do
require 'silverline/essential'
end

it "should define Xap" do
Silverline::Essential::Xap.should_not be_nil
end

it "should use Chiron for xapping" do
Silverline::Essential::Xap.should == :chiron
end

it "should mixin essential HTML module into ActionView's Base class" do
ActionView::Base.included_modules.include?(Silverline::Essential::Html)
end

it "should tell the generator to register itself" do
Silverline::Essential.instance_eval{remove_const :Generator}
gen = Silverline::Essential::Generator = mock("Generator")
gen.should_receive(:register)
load 'silverline/essential.rb'
end

end
@@ -0,0 +1,93 @@
require File.dirname(__FILE__) + '/../../spec_nonrails.rb'

Silverline.instance_eval{remove_const :Essential} if defined?(Silveline::Essential)
Silverline::Essential = Module.new

Silverline::Essential.instance_eval{remove_const :Xap} if defined?(Silveline::Essential::Xap)
Silverline::Essential::Xap = :chiron

load 'silverline/essential/generator.rb'

describe Silverline::Essential::Generator do

before do
Silverline::Essential.instance_eval{remove_const :Generator}
Silverline::Essential::Generator = Module.new
end

before(:each) do
load 'silverline/essential/generator.rb'

Object.instance_eval{ remove_const :FileUtils} if defined?(FileUtils)
Object.instance_eval{ remove_const :File} if defined?(File)

@watcher = mock("FileSystemWatcher")
FileSystemWatcher.stub!(:new).and_return @watcher

@gen = Silverline::Essential::Generator
end

it "should watch directories to put into XAP" do
FileUtils = mock("FileUtils")
FileUtils.should_receive(:mkdir_p).with(Silverline::TMP_CLIENT)
FileUtils.should_receive(:mkdir_p).with(Silverline::CLIENT_ROOT)

@watcher.should_receive(:addDirectory).with(Silverline::CLIENT_ROOT)
@watcher.should_receive(:addDirectory).with(Silverline::PLUGIN_CLIENT)
@watcher.should_receive(:addDirectory).with("#{Silverline::PLUGIN_ROOT}/public")
@watcher.should_receive(:addDirectory).with("#{RAILS_ROOT}/public/ironruby")

#@watcher.should_receive(:addDirectory).with(Silverline::RAILS_CTRLRS)
@watcher.stub!(:addFile)
@watcher.should_receive(:addDirectory).with(Silverline::RAILS_VIEWS)

@watcher.should_receive(:sleepTime=).with(1)

@watcher.should_receive(:start)

@gen.should_receive(:generate)

@gen.register
end

it "should delete the XAP" do
File = mock("File")
FileUtils = mock("FileUtils", :null_object => true)
File.stub!(:exists?).with(Silverline::XAP_FILE).and_return true
File.should_receive(:delete).with(Silverline::XAP_FILE).ordered
@gen.generate
end

it "should not try to delete the XAP" do
File = mock("File")
FileUtils = mock("FileUtils", :null_object => true)
File.stub!(:exists?).with(Silverline::XAP_FILE).and_return false
File.should_not_receive(:delete).with(Silverline::XAP_FILE)
@gen.generate
end

it "should generate the XAP from the watched folders" do
@xap = mock("XAP")
File = mock("File", :null_object => true)
FileUtils = mock("FileUtils")

logger.should_receive(:info).with("Silverline: Generating client.xap")

FileUtils.should_receive(:cp_r).with("#{Silverline::PLUGIN_CLIENT}/.", Silverline::TMP_CLIENT).ordered

FileUtils.should_receive(:mkdir_p).with("#{Silverline::TMP_CLIENT}/controllers").ordered
FileUtils.should_receive(:mkdir_p).with("#{Silverline::TMP_CLIENT}/views").ordered
FileUtils.should_receive(:cp).with('app/controllers/client_controller.rb', "#{Silverline::TMP_CLIENT}/controllers").ordered
FileUtils.should_receive(:cp_r).with("#{Silverline::RAILS_VIEWS}/.", "#{Silverline::TMP_CLIENT}/views").ordered

FileUtils.should_receive(:cp_r).with("#{Silverline::CLIENT_ROOT}/.", Silverline::TMP_CLIENT).ordered

Silverline::Essential::XAP.should_receive(:new).with(Silverline::XAP_FILE, Silverline::TMP_CLIENT).and_return @xap
@xap.should_receive(:generate)

FileUtils.should_receive(:rm_r).with(Silverline::TMP_CLIENT).ordered

@gen.generate
end

end

0 comments on commit 1687fc1

Please sign in to comment.