parser_test.rb 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. require_relative '../../../test_helper'
  2. require_relative '../../../../lib/docs'
  3. class DocsParserTest < Minitest::Spec
  4. def parser(content)
  5. Docs::Parser.new(content)
  6. end
  7. describe "#html" do
  8. it "returns a Nokogiri Node" do
  9. assert_kind_of Nokogiri::XML::Node, parser('').html
  10. end
  11. context "with an HTML fragment" do
  12. it "returns the fragment" do
  13. body = '<div>Test</div>'
  14. html = parser(body).html
  15. assert_equal '#document-fragment', html.name
  16. assert_equal body, html.inner_html
  17. end
  18. end
  19. context "with an HTML document" do
  20. it "returns the document" do
  21. body = '<!-- foo --> <!doctype html><meta charset=utf-8><title></title><div>Test</div>'
  22. html = parser(body).html
  23. assert_equal 'document', html.name
  24. assert_equal '<div>Test</div>', html.at_css('body').inner_html
  25. body = '<html><meta charset=utf-8><title></title><div>Test</div></html>'
  26. html = parser(body).html
  27. assert_equal 'document', html.name
  28. assert_equal '<div>Test</div>', html.at_css('body').inner_html
  29. end
  30. end
  31. end
  32. describe "#title" do
  33. it "returns nil when there is no <title>" do
  34. body = '<!doctype html><meta charset=utf-8><div>Test</div>'
  35. assert_nil parser(body).title
  36. end
  37. it "returns the <title> when there is one" do
  38. body = '<!doctype html><meta charset=utf-8><title>Title</title><div>Test</div>'
  39. assert_equal 'Title', parser(body).title
  40. end
  41. end
  42. end