~hacktivista/tilt-emacs_org

ref: 0a5ed778f4117aca2faa75edb1999ab305ac7d77 tilt-emacs_org/test/emacs_org_test.rb -rw-r--r-- 2.3 KiB
0a5ed778Felix Freeman Fix: Ignore emacs *Messages* + Fix: :body_with_resources option. 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true

require 'nokogiri'
require 'tempfile'
require 'test_helper'

# Tilt.
module Tilt
  # EmacsOrgTemplate tests.
  class EmacsOrgTemplateTest < Minitest::Test
    def setupfile
      @setupfile ||= begin
        file = Tempfile.new(['', '.org'])
        file.write('#+OPTIONS: toc:nil num:nil')
        file.rewind
        file.path
      end
    end

    def test_render__without_setupfile
      doc = render_and_inspect{ '* anything' }
      assert_equal('Table of Contents', doc.at_css('h2').inner_html)
    end

    def test_render__body_only
      doc = render_and_inspect({ setupfile: setupfile }){ '* jelou' }
      assert_equal('jelou', doc.at_css('h2').inner_html)
      assert_nil(doc.at_css('head'))
    end

    def test_render__body_with_resources
      doc = render_and_inspect(
        { setupfile: setupfile, format: :body_with_resources }
      ) do
        <<~ORG
          #+TITLE: Title
          * Content
        ORG
      end
      assert_equal('Content', doc.at_css('h2').inner_html)
      assert_nil(doc.at_css('head title'))
    end

    def test_render__full
      doc = render_and_inspect(
        { setupfile: setupfile, format: :full }
      ) do
        <<~ORG
          #+TITLE: Title
          #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="style.css" />
        ORG
      end
      assert_equal('Title', doc.at_css('head title').inner_html)
      assert_equal('style.css', doc.at_css('head link').attribute('href').value)
      assert_equal('Title', doc.at_css('body h1').inner_html)
    end

    def test_head
      template = Tilt['org'].new do
        <<~ORG
          #+HTML_HEAD: <script type="text/css" src="test_script.js"></script>
          Sup
        ORG
      end
      assert_includes(
        template.head,
        '<script type="text/css" src="test_script.js"></script>'
      )
    end

    def test_title
      template = Tilt['org'].new{ '#+TITLE: My title' }
      assert_equal('My title', template.title)
    end

    def test_body
      template = Tilt['org'].new{ 'My body' }
      assert_includes(template.body, 'My body')
      refute_includes(template.body, '<head>')
    end

    protected

    def render_and_inspect(options = {}, &block)
      Nokogiri::HTML(
        Tilt['org'].new(nil, 1, options){ block.call }.render
      )
    end
  end
end