Skip to content

Commit

Permalink
Merged magic coding tests from kemitchell's commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cjheath committed Jun 4, 2013
2 parents 8de4cd6 + 5ecfa5e commit 0b3b4b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/compiler/grammar_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
@source_path_with_treetop_extension = "#{dir}/test_grammar.treetop"
@source_path_with_do = "#{dir}/test_grammar_do.treetop"
@source_path_with_tt_extension = "#{dir}/test_grammar.tt"
@source_path_with_magic_coding = "#{dir}/test_grammar_magic_coding.treetop"
@source_path_with_magic_encoding = "#{dir}/test_grammar_magic_encoding.treetop"
@target_path = "#{@tmpdir}/test_grammar.rb"
@target_path_with_do = "#{@tmpdir}/test_grammar_do.rb"
@target_path_with_magic_coding = "#{@tmpdir}/test_grammar_magic_coding.rb"
@target_path_with_magic_encoding = "#{@tmpdir}/test_grammar_magic_encoding.rb"
@alternate_target_path = "#{@tmpdir}/test_grammar_alt.rb"
delete_target_files
end
Expand Down Expand Up @@ -82,6 +86,24 @@
Test::GrammarParser.new.parse('foo').should_not be_nil
end

specify "grammars with magic 'encoding' comments keep those comments at the top" do
src_copy = "#{@tmpdir}/test_grammar_magic_encoding.treetop"
File.open(@source_path_with_magic_encoding) do |f|
File.open(src_copy,'w'){|o|o.write(f.read)}
end
compiler.compile(src_copy)
File.open(@target_path_with_magic_encoding).readline.should == "# encoding: UTF-8\n"
end

specify "grammars with magic 'coding' comments keep those comments at the top" do
src_copy = "#{@tmpdir}/test_grammar_magic_coding.treetop"
File.open(@source_path_with_magic_coding) do |f|
File.open(src_copy,'w'){|o|o.write(f.read)}
end
compiler.compile(src_copy)
File.open(@target_path_with_magic_coding).readline.should == "# coding: UTF-8\n"
end

def delete_target_files
File.delete(target_path) if File.exists?(target_path)
File.delete(@target_path_with_do) if File.exists?(@target_path_with_do)
Expand Down
8 changes: 8 additions & 0 deletions spec/compiler/test_grammar_magic_coding.treetop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# coding: UTF-8
module Test
grammar Grammar do
rule foo do
'foo'
end
end
end
8 changes: 8 additions & 0 deletions spec/compiler/test_grammar_magic_encoding.treetop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# encoding: UTF-8
module Test
grammar Grammar do
rule foo do
'foo'
end
end
end

0 comments on commit 0b3b4b3

Please sign in to comment.