require 'spec_helper' module Deface describe HamlConverter do include_context "mock Rails.application" def haml_to_erb(src) haml_engine = Deface::HamlConverter.new(src) haml_engine.render.gsub("\n", "") end describe "convert haml to erb" do it "should hanlde simple tags" do haml_to_erb("%%strong.code#message Hello, World!").should == "Hello, World!" end it "should handle complex tags" do haml_to_erb(%q{#content .left.column %h2 Welcome to our site! %p= print_information .right.column = render :partial => "sidebar"}).should == "

Welcome to our site!

<%= print_information %>

<%= render :partial => \"sidebar\" %>
" end it "should handle simple haml attributes" do haml_to_erb("%meta{:charset => 'utf-8'}").should == "" haml_to_erb("%p(alt='hello world')Hello World!").should == "

Hello World!

" end it "should handle haml attributes with commas" do haml_to_erb("%meta{'http-equiv' => 'X-UA-Compatible', :content => 'IE=edge,chrome=1'}").should == "" haml_to_erb("%meta(http-equiv='X-UA-Compatible' content='IE=edge,chrome=1')").should == "" haml_to_erb('%meta{:name => "author", :content => "Example, Inc."}').should == "" haml_to_erb('%meta(name="author" content="Example, Inc.")').should == "" if RUBY_VERSION > "1.9" haml_to_erb('%meta{name: "author", content: "Example, Inc."}').should == "" end end it "should handle haml attributes with evaluated values" do haml_to_erb("%p{ :alt => hello_world}Hello World!").should == "

Hello World!

" if RUBY_VERSION > "1.9" haml_to_erb("%p{ alt: @hello_world}Hello World!").should == "

Hello World!

" end haml_to_erb("%p(alt=hello_world)Hello World!").should == "

Hello World!

" haml_to_erb("%p(alt=@hello_world)Hello World!").should == "

Hello World!

" end it "should handle erb loud" do haml_to_erb("%h3.title= entry.title").should == "

<%= entry.title %>

" end it "should handle single erb silent" do haml_to_erb("- some_method").should == "<% some_method %>" end it "should handle implicitly closed erb loud" do haml_to_erb("= if @this == 'this' %p hello ").should == "<%= if @this == 'this' %>

hello

<% end %>" end it "should handle implicitly closed erb silent" do haml_to_erb("- if foo? %p hello ").should == "<% if foo? %>

hello

<% end %>" end it "should handle blocks passed to erb loud" do haml_to_erb("= form_for Post.new do |f| %p = f.text_field :name").should == "<%= form_for Post.new do |f| %>

<%= f.text_field :name %>

<% end %>" end it "should handle blocks passed to erb silent" do haml_to_erb("- @posts.each do |post| %p = post.name").should == "<% @posts.each do |post| %>

<%= post.name %>

<% end %>" end end end end