# encoding=utf-8 require 'spec_helper' describe Polytexnic::Pipeline do before(:all) do FileUtils.rm('.highlight_cache') if File.exist?('.highlight_cache') end subject(:processed_text) { Polytexnic::Pipeline.new(polytex).to_html } describe "code blocks" do context "without syntax highlighting" do let(:polytex) do <<-'EOS' \begin{code} def foo "bar" end \end{code} EOS end it { should resemble 'def foo' } it { should resemble '
def foo
"bar"
end
' }
end
context "with Unicode in the highlighting cache" do
let(:polytex) do <<-'EOS'
%= lang:console
\begin{code}
'foo★bar'
\end{code}
%= lang:console
\begin{code}
foo
\end{code}
EOS
end
before do
# Create the broken highlight cache.
Polytexnic::Pipeline.new(polytex).to_html
end
it "should not crash" do
expect(File.exist?('.highlight_cache')).to be_true
expect { Polytexnic::Pipeline.new(polytex).to_html }.not_to raise_error
end
end
end
context "with a space after 'lang'" do
let(:polytex) do <<-'EOS'
%= lang: ruby
\begin{code}
def foo
"bar"
end
\end{code}
EOS
end
it do
should resemble <<-'EOS'
def foo
"bar"
end
EOS
end
end
context "with highlight and line numbering options" do
let(:polytex) do <<-'EOS'
%= lang:ruby, options: "hl_lines": [1, 2], "linenos": true
\begin{code}
def foo
"bar"
end
\end{code}
EOS
end
it do
should resemble <<-'EOS'
1
def foo
2
"bar"
3
end
EOS
end
end
describe "code inclusion" do
context "for an existing file" do
context "with no extension" do
let(:polytex) do <<-'EOS'
%= <<(Rakefile)
EOS
end
it { should include 'require' }
end
context "with an extension" do
let(:polytex) do <<-'EOS'
%= <<(spec/to_html/literal_environments/code_spec.rb)
EOS
end
let(:output) do <<-'EOS'
# encoding=utf-8
EOS
end
it { should resemble output }
it { should_not include '' }
end
context "with a section" do
let(:polytex) do <<-'EOS'
%= <<(spec/to_html/literal_environments/code_spec.rb[section_z])
EOS
end
let(:output) do <<-'EOS'
"This is section_z; it's used by a test."
"Section Z is your friend."
EOS
end
it { should resemble output }
it { should_not include '#// begin section_z' }
it { should_not include '#// end' }
context "that does not exist" do
let(:polytex) do <<-'EOS'
%= <<(spec/to_html/literal_environments/code_spec.rb[section_that_does_not_exist])
EOS
end
let(:output) do <<-'EOS'
ERROR: Could not find section header '#// begin section_that_does_not_exist' in file 'spec/to_html/literal_environments/code_spec.rb'
EOS
end
it { should resemble output }
end
end
context "with a custom language override" do
let(:polytex) do <<-'EOS'
%= <<(polytexnic_commands.sty, lang: tex)
EOS
end
let(:output) do <<-'EOS'
% Add some custom commands needed by PolyTeXnic.
EOS
end
it { should resemble output }
it { should_not include '' }
end
context "with custom options" do
let(:polytex) do <<-'EOS'
%= <<(polytexnic_commands.sty, lang: tex, options: "hl_lines": [5])
EOS
end
let(:output) do <<-'EOS'
EOS
end
it { should resemble output }
end
end
context "for a nonexistent file" do
let(:polytex) do <<-'EOS'
%= <<(foobar.rb)
EOS
end
it { should include "ERROR: File 'foobar.rb' does not exist" }
end
end
end
###################################################
'The following lines are used to test code sections'
#// begin section_a
"This is the code inside of section_a."
"Sections begin with a line containing only '#// begin section_name' and end with '#// end'"
"You many divide a file into multiple sections and include them individually in your book."
#// end
#// begin section_z
"This is section_z; it's used by a test."
"Section Z is your friend."
#// end