Skip to content

Commit

Permalink
add tests for haml_parser ... its not that hard ...
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jan 10, 2012
1 parent 1a7859f commit 2c18603
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Expand Up @@ -3,6 +3,10 @@ source :rubygems
gem 'fast_gettext'

group :dev do
gem 'haml'
gem 'slim'
gem 'ruby_parser'
gem 'gettext'
gem 'sqlite3', '~>1.3.4'
gem 'rails', ENV['RAILS'] || '~>3'
gem 'rake'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Expand Up @@ -36,13 +36,17 @@ GEM
diff-lcs (1.1.2)
erubis (2.7.0)
fast_gettext (0.5.13)
gettext (2.1.0)
locale (>= 2.0.5)
git (1.2.5)
haml (3.1.4)
hike (1.2.1)
i18n (0.6.0)
jeweler (1.6.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
locale (2.0.5)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand Down Expand Up @@ -84,11 +88,18 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
ruby_parser (2.3.1)
sexp_processor (~> 3.0)
sexp_processor (3.0.10)
slim (1.1.0)
temple (~> 0.3.5)
tilt (~> 1.3.2)
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.4)
temple (0.3.5)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
Expand All @@ -101,8 +112,12 @@ PLATFORMS

DEPENDENCIES
fast_gettext
gettext
haml
jeweler
rails (~> 3)
rake
rspec (~> 2)
ruby_parser
slim
sqlite3 (~> 1.3.4)
18 changes: 10 additions & 8 deletions lib/gettext_i18n_rails/haml_parser.rb
Expand Up @@ -14,18 +14,18 @@ def target?(file)
end

def parse(file, msgids = [])
return msgids unless load_haml
require 'gettext_i18n_rails/ruby_gettext_extractor'

text = IO.readlines(file).join
return msgids unless prepare_haml_parsing
code = haml_to_code(File.read(file))
RubyGettextExtractor.parse_string(code, file, msgids)
end

haml = Haml::Engine.new(text)
code = haml.precompiled
return RubyGettextExtractor.parse_string(code, file, msgids)
def haml_to_code(haml)
Haml::Engine.new(haml).precompiled
end

def load_haml
def prepare_haml_parsing
return true if @haml_loaded

begin
require "#{::Rails.root.to_s}/vendor/plugins/haml/lib/haml"
rescue LoadError
Expand All @@ -36,6 +36,8 @@ def load_haml
return false
end
end

require 'gettext_i18n_rails/ruby_gettext_extractor'
@haml_loaded = true
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/gettext_i18n_rails/haml_parser_spec.rb
@@ -0,0 +1,32 @@
require 'spec_helper'
require 'gettext_i18n_rails/haml_parser'

describe GettextI18nRails::HamlParser do
let(:parser){ GettextI18nRails::HamlParser }

describe "#target?" do
it "targets .haml" do
parser.target?('foo/bar/xxx.haml').should == true
end

it "does not target anything else" do
parser.target?('foo/bar/xxx.erb').should == false
end
end

describe "#parse" do
it "finds messages in haml" do
with_file '= _("xxxx")' do |path|
parser.parse(path, []).should == [
["xxxx", "#{path}:1"]
]
end
end

it "does not find messages in text" do
with_file '_("xxxx")' do |path|
parser.parse(path, []).should == []
end
end
end
end
22 changes: 22 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,9 +2,31 @@

$LOAD_PATH << File.expand_path("../lib", File.dirname(__FILE__))

require 'tempfile'
require 'active_support'
require 'active_record'
require 'action_controller'
require 'action_mailer'
require 'fast_gettext'
require 'gettext_i18n_rails'

begin
Gem.all_load_paths
rescue
puts "Fixing Gem.all_load_paths"
module Gem;def self.all_load_paths;[];end;end
end

module Rails
def self.root
File.dirname(__FILE__)
end
end

def with_file(content)
Tempfile.open('gettext_i18n_rails_specs') do |f|
f.write(content)
f.close
yield f.path
end
end

0 comments on commit 2c18603

Please sign in to comment.