spec/crush/engine_spec.rb in crush-0.2.0 vs spec/crush/engine_spec.rb in crush-0.3.0

- old
+ new

@@ -1,110 +1,66 @@ require "spec_helper" describe Crush::Engine do - class MockEngine < Crush::Engine - end - - describe ".engine_name" do - it "returns an undescored version of the class name" do - MockEngine.engine_name.should == "mock_engine" + class Stripper < Crush::Engine + def evaluate(*args) + case options[:direction] + when :left + data.lstrip + when :right + data.rstrip + else + data.strip + end end end - describe "#file" do - it "returns the file the engine was initialized with" do - engine = MockEngine.new("application.js") {} - engine.file.should == "application.js" + describe ".compress" do + it "compresses the given data" do + Stripper.compress(" data ").should == "data" end - end - - describe "#options" do - it "returns the options passed to the engine" do - engine = MockEngine.new(nil, :foo => "bar") {} - engine.options[:foo].should == "bar" - engine = MockEngine.new(:bar => "foo") {} - engine.options[:bar].should == "foo" + + it "accepts options" do + Stripper.compress(" data ", :direction => :left).should == "data " + Stripper.compress(" data ", :direction => :right).should == " data" end - end - - describe "#data" do - it "returns the data from the file" do - File.stub(:binread => "Hello", :read => "Hello") - engine = MockEngine.new("application.js") - engine.data.should == "Hello" - end - it "returns the data from the given block" do - engine = MockEngine.new("application.js") { "Hello" } - engine.data.should == "Hello" + it "is aliased as `compile`" do + Stripper.compile(" data ").should == "data" end end - class InitializingMockEngine < Crush::Engine - class << self - attr_accessor :initialized_count + describe "#initialize" do + it "does not raise errors without a file or block" do + expect { + Stripper.new + }.to_not raise_error(ArgumentError) end - - def initialize_engine - self.class.initialized_count += 1 - end end - describe "#initialize_engine" do - it "is called one time to require the library" do - InitializingMockEngine.initialized_count = 0 - InitializingMockEngine.new - InitializingMockEngine.initialized_count.should == 1 - InitializingMockEngine.new - InitializingMockEngine.initialized_count.should == 1 + describe "#compress" do + it "compresses the given data" do + Stripper.new.compress(" data ").should == "data" end - end - - class InitializedMockEngine < Crush::Engine - end - - describe ".engine_initialized?" do - it "returns false before the engine has been initialized" do - InitializedMockEngine.engine_initialized?.should be_false + + it "uses options set when initializing" do + Stripper.new(:direction => :left).compress(" data ").should == "data " + Stripper.new(:direction => :right).compress(" data ").should == " data" end - it "returns true once the engine has been initialized" do - InitializedMockEngine.new - InitializedMockEngine.engine_initialized?.should be_true + it "is aliased as `compile`" do + Stripper.new.compile(" data ").should == "data" end - end - - class PreparingMockEngine < Crush::Engine - class << self - attr_accessor :prepared_count - end - def prepare - self.class.prepared_count += 1 + it "can be used without an argument" do + Stripper.new { " data " }.compress.should == "data" end end - describe "#prepare" do - it "is called each time an engine is created" do - PreparingMockEngine.prepared_count = 0 - PreparingMockEngine.new - PreparingMockEngine.prepared_count.should == 1 - PreparingMockEngine.new - PreparingMockEngine.prepared_count.should == 2 - end - end - - class SimpleMockEngine < Crush::Engine - def evaluate - @data.strip - end - end - - describe "#compress" do - it "compresses the data using the engine" do - File.stub(:binread => " hello ", :read => " hello ") - SimpleMockEngine.new("file.txt").compile.should == "hello" - SimpleMockEngine.new { " hello " }.render.should == "hello" - SimpleMockEngine.new.compress(" hello ").should == "hello" + describe "#render" do + it "throws an error if used without data" do + expect { + Stripper.new.render + }.to raise_error(ArgumentError) end end end