test/rack/blogengine/document_parser_test.rb in rack-blogengine-0.2.4 vs test/rack/blogengine/document_parser_test.rb in rack-blogengine-0.2.5
- old
+ new
@@ -4,150 +4,65 @@
# Test Class for DocumentParser
#
# @author [benny]
#
class DocumentParserTest < MiniTest::Unit::TestCase
- # parallelize_me!
-
def setup
- cli = Rack::Blogengine::CommandLineInterface.new
- capture_stdout { cli.generate(testpath) }
- cli.send(:get_config, testpath)
+ @cli = Rack::Blogengine::CommandLineInterface.new
+ capture_stdout { @cli.generate(testpath) }
+ @cli.send(:get_config, testpath)
end
- # Test DocumentParser.parse_in_documents(path)
def test_parse_in_documents
- @documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath)
+ documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath)
- @documents.each do |document|
+ documents.each do |document|
# Check Hash keys
- assert(document.key?(:html), 'Documents should contain a path')
- assert(document.key?(:path), 'Documents should contain a response')
+ assert(document.key?(:html), 'Documents should contain content')
+ assert(document.key?(:path), 'Documents should contain a path')
end
end
- # Test DocumentParser.fill_file_contents(layout, title, content, date)
- def test_fill_file_contents
- layout_file = ::File.open("#{testpath}/assets/layout/layout.html", 'r')
- layout = layout_file.read
- title = 'testtitle'
- content = 'testcontent'
- date = Date.new
-
- html = Rack::Blogengine::DocumentParser.fill_file_contents(layout, title, content, date)
-
- assert(html.include?(title), 'Parsed and filled in HTML should contain Title')
- assert(html.include?(content), 'Parsed and filled in HTML should contain Content')
- assert(html.include?(date.strftime('%d.%m.%Y')), 'Parsed and filled in HTML should contain Date')
+ def test_invalid_date
+ system("rm #{testpath}/index.content")
+ capture_stdout { @cli.send(:setup, "date_error.content", "#{testpath}", true) }
+ assert_raises(RuntimeError) { documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath) }
end
- def test_fill_file_contents_with_pygments
- layout_file = ::File.open("#{testpath}/assets/layout/layout.html", 'r')
- layout = layout_file.read
- title = 'testtitle'
- content = '<pre class="brush:ruby">def TestMethod</pre>'
- date = Date.new
-
- html = Rack::Blogengine::DocumentParser.fill_file_contents(layout, title, content, date)
-
- assert(html.include?(title), 'Parsed and filled in HTML should contain Title')
- assert(html.include?("highlight"), 'Parsed and filled in HTML with pygment handling should contain .highlight class')
- assert(html.include?(date.strftime('%d.%m.%Y')), 'Parsed and filled in HTML should contain Date')
+ def test_invalid_content
+ system("rm #{testpath}/index.content")
+ capture_stdout { @cli.send(:setup, "content_error.content", "#{testpath}", true) }
+ assert_raises(RuntimeError) { documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath) }
end
- # Test DocumentParser.get_file_contents('file.content')
- def test_get_file_contents
- Rack::Blogengine::DocumentParser.title = ''
- Rack::Blogengine::DocumentParser.content = ''
- Rack::Blogengine::DocumentParser.date = ''
-
- Rack::Blogengine::DocumentParser.target = "#{Rack::Blogengine.root}/assets"
- Rack::Blogengine::DocumentParser.get_file_contents('index.content')
-
- assert_equal('INDEX', Rack::Blogengine::DocumentParser.title, 'Parsed in Title should eql Title in test .content file')
- assert_equal('<h2>This is the Index Page</h2>', Rack::Blogengine::DocumentParser.content, 'Parsed in Content should eql Content in test .content file')
- assert_instance_of(Date, Rack::Blogengine::DocumentParser.date, 'Parsed in Date should be of Class Date')
+ def test_invalid_title
+ system("rm #{testpath}/index.content")
+ capture_stdout { @cli.send(:setup, "title_error.content", "#{testpath}", true) }
+ assert_raises(RuntimeError) { documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath) }
end
- def test_get_file_contents_invalid_date
- Rack::Blogengine::DocumentParser.target = "#{Rack::Blogengine.root}/assets"
- assert_raises(RuntimeError) { Rack::Blogengine::DocumentParser.get_file_contents('date_error.content') }
+ def test_documents_with_pygments
+ capture_stdout { @cli.send(:setup, "pygment.content", "#{testpath}", true) }
+ system("rm #{testpath}/index.content")
+ documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath)
+ documents.each do |document|
+ assert(document[:html].include?('class="highlight"'), 'Highlighted code should be wrapped in a div.highlight')
+ end
end
- def test_get_file_contents_invalid_content
- Rack::Blogengine::DocumentParser.target = "#{Rack::Blogengine.root}/assets"
- assert_raises(RuntimeError) { Rack::Blogengine::DocumentParser.get_file_contents('content_error.content') }
+ def test_documents_with_operator
+ capture_stdout { @cli.send(:setup, "operator.content", "#{testpath}", true) }
+ system("rm #{testpath}/index.content")
+ documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath)
+ documents.each do |document|
+ assert(document[:html].include?('test'), 'Test Operator should return test')
+ end
end
- def test_get_file_contents_invalid_title
- Rack::Blogengine::DocumentParser.target = "#{Rack::Blogengine.root}/assets"
- assert_raises(RuntimeError) { Rack::Blogengine::DocumentParser.get_file_contents('title_error.content') }
- end
-
- # Test DocumentParser.get_content_array(contentstring)
- # Should split up the content for each content section (Path, Title, Date, Content)
- def test_get_content_array
- content = '[path]:[/path]
- [title]:INDEX[/title]
- [date]:2013,01,01[/date]
- [content]:
- <h2>This is the Index Page</h2>
- [/content]'
- contentarray = Rack::Blogengine::DocumentParser.get_content_array(content)
-
- assert_equal(4, contentarray.length, 'The content Array should contain 4 members (Path, Title, Date, Content)')
- assert(contentarray[0].include?('path'), 'First Entry should contain the path')
- assert(contentarray[1].include?('title'), 'Second Entry should contain the title')
- assert(contentarray[2].include?('date'), 'Third Entry should contain the date')
- assert(contentarray[3].include?('content'), 'Fourth Entry should contain the content')
- end
-
- # Test DocumentParser.sort(documents)
- def test_sort
- documents = []
-
- document1 = Rack::Blogengine::Document.new
- document1.date = Date.new(2013, 12, 12)
-
- document2 = Rack::Blogengine::Document.new
- document2.date = Date.new(2012, 12, 12)
-
- documents << document1 << document2
- documents = Rack::Blogengine::DocumentParser.sort(documents)
-
- assert_equal(Date.new(2012, 12, 12), documents[0].date, 'Documents should be sorted by date (earlier first)')
- end
-
- def test_highlight
- content = "def TestMethod"
-
- highlighted = Rack::Blogengine::DocumentParser.highlight(content, 'ruby', testpath)
- assert_match(/.highlight/, highlighted, "Highlighted html should contain a element with class highlight")
- end
-
- def test_highlight_fail
- content = "def TestMethod"
-
- highlighted = Rack::Blogengine::DocumentParser.highlight(content, nil, testpath)
- assert_equal(content, highlighted, "If Language is not defined Content should be returned unmodified")
- end
-
- def test_generate_highlight_css
- Rack::Blogengine::DocumentParser.generate_highlight_css(testpath)
- # css = Pygments.css(Rack::Blogengine.config["pygments_style"])
- highlightcss = ::File.read("#{testpath}/assets/style/highlight.css")
- # assert_equal(css, highlightcss, "Highlight Css file should be automatically filled in")
- refute_empty(highlightcss, "Highlight Css file should be automatically filled in")
- end
-
- def test_get_highlight_code
- content = "<html><head><title>Test</title></head><body><pre class='brush:ruby'>def TestMethod</pre></body></html>"
-
- seperator = Rack::Blogengine.config["pygments_seperator"]
-
- highlight_code = Rack::Blogengine::DocumentParser.get_highlight_code(content, seperator)
- assert_equal('def TestMethod', highlight_code[:text], "Code between #{seperator} should be returned")
- assert_equal('ruby', highlight_code[:brush], "Brush should be recognised by the class attribute")
+ def test_document_sort
+ capture_stdout { @cli.send(:setup, "date_test.content", "#{testpath}", true) }
+ documents = Rack::Blogengine::DocumentParser.parse_in_documents(testpath)
+ assert(documents[0][:html].include?('This is 2012'), 'The Document with lower date should be first')
end
def teardown
system("rm -rf #{testpath}")
end