File: test_ronn_document.rb

package info (click to toggle)
ruby-ronn 0.7.3-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 512 kB
  • sloc: ruby: 1,405; makefile: 2
file content (186 lines) | stat: -rw-r--r-- 5,988 bytes parent folder | download | duplicates (3)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
require 'contest'
require 'ronn/document'

class DocumentTest < Test::Unit::TestCase
  SIMPLE_FILE = "#{File.dirname(__FILE__)}/basic_document.ronn"

  def canonicalize(text)
    text.
    gsub(/^ +/, '').
    gsub(/\n/m, '').
    gsub(/ +/, ' ').
    gsub(/"/, "'")
  end

  test "new with path" do
    doc = Ronn::Document.new(SIMPLE_FILE)
    assert_equal File.read(SIMPLE_FILE), doc.data
  end

  test "new with path and block" do
    doc = Ronn::Document.new('hello.1.ronn') { "# hello(1) -- hello world" }
    assert_equal "# hello(1) -- hello world", doc.data
  end

  test "new with path and block but missing name section" do
    doc = Ronn::Document.new('foo.7.ronn') { '' }
    assert_equal 'foo', doc.name
    assert_equal '7', doc.section
  end

  test "new with non conventional path and missing name section" do
    doc = Ronn::Document.new('bar.ronn') { '' }
    assert_equal 'bar', doc.name
    assert_equal nil, doc.section
    assert_equal "./bar.html", doc.path_for('html')
    assert_equal "./bar", doc.path_for('roff')
    assert_equal "./bar", doc.path_for('')
    assert_equal "./bar", doc.path_for(nil)
  end

  test "new with path and name section mismatch" do
    doc = Ronn::Document.new('foo/rick.7.ronn') { "# randy(3) -- I'm confused." }
    assert_equal 'randy', doc.name
    assert_equal 'rick', doc.path_name
    assert_equal '3', doc.section
    assert_equal '7', doc.path_section
    assert_equal 'rick.7', doc.basename
    assert_equal 'foo/rick.7.bar', doc.path_for(:bar)
  end

  test "new with no path and a name section" do
    doc = Ronn::Document.new { "# brandy(5) -- wootderitis" }
    assert_equal nil, doc.path_name
    assert_equal nil, doc.path_section
    assert_equal 'brandy', doc.name
    assert_equal '5', doc.section
    assert_equal 'brandy.5', doc.basename
    assert_equal 'brandy.5.foo', doc.path_for(:foo)
  end

  1.upto(5) do |i|
    dashes = '-' * i

    test "new with no path and #{i} dashes in name" do
      doc = Ronn::Document.new { "# brandy #{dashes} wootderitis" }
      assert_equal 'brandy', doc.name
      assert_equal nil, doc.section
      assert_equal 'wootderitis', doc.tagline
    end

    test "new with no path and a name section and #{i} dashes in name" do
      doc = Ronn::Document.new { "# brandy(5) #{dashes} wootderitis" }
      assert_equal 'brandy', doc.name
      assert_equal '5', doc.section
      assert_equal 'wootderitis', doc.tagline
    end
  end

  context "simple conventionally named document" do
    setup do
      @now = Time.now
      @doc = Ronn::Document.new('hello.1.ronn') { "# hello(1) -- hello world" }
      @doc.date = @now
    end

    should "load data" do
      assert_equal "# hello(1) -- hello world", @doc.data
    end

    should "extract the manual page name from the filename or document" do
      assert_equal 'hello', @doc.name
    end

    should "extract the manual page section from the filename or document" do
      assert_equal '1', @doc.section
    end

    should "convert to an HTML fragment with no wrap div" do
      assert_equal %[<h2 id='NAME'>NAME</h2><p class='man-name'><code>hello</code> - <span class='man-whatis'>hello world</span></p>],
        canonicalize(@doc.to_html_fragment(wrap=nil))
    end

    should "convert to an HTML fragment with a wrap class" do
      assert_equal %[<div class='pm'><h2 id='NAME'>NAME</h2><p class='man-name'><code>hello</code> - <span class='man-whatis'>hello world</span></p></div>],
        canonicalize(@doc.to_html_fragment(wrap_class='pm'))
    end

    should "convert to HTML with a layout" do
      assert_match %r{^<!DOCTYPE html.*}m, @doc.to_html
      assert_match %[<h2 id='NAME'>NAME</h2><p class='man-name'><code>hello</code> - <span class='man-whatis'>hello world</span></p>],
        canonicalize(@doc.to_html)
    end

    should "construct a path to related documents" do
      assert_equal "./hello.1.html", @doc.path_for(:html)
      assert_equal "./hello.1", @doc.path_for(:roff)
      assert_equal "./hello.1", @doc.path_for('')
      assert_equal "./hello.1", @doc.path_for(nil)
    end

    test "uses default styles" do
      assert_equal %w[man], @doc.styles
    end

    test "converting to a hash" do
      assert_equal({
        "section"      => "1",
        "name"         => "hello",
        "date"         => @now,
        "tagline"      => "hello world",
        "styles"       => ["man"],
        "toc"          => [["NAME", "NAME"]],
        "organization" => nil,
        "manual"       => nil
      }, @doc.to_h)
    end

    test "converting to yaml" do
      require 'yaml'
      assert_equal({
        "section"      => "1",
        "name"         => "hello",
        "date"         => @now,
        "tagline"      => "hello world",
        "styles"       => ["man"],
        "toc"          => [["NAME", "NAME"]],
        "organization" => nil,
        "manual"       => nil
      }, YAML.load(@doc.to_yaml))
    end

    test "converting to json" do
      require 'json'
      assert_equal({
        "section"      => "1",
        "name"         => "hello",
        "date"         => @now.iso8601,
        "tagline"      => "hello world",
        "styles"       => ["man"],
        "toc"          => [["NAME", "NAME"]],
        "organization" => nil,
        "manual"       => nil
      }, JSON.parse(@doc.to_json))
    end
  end

  test 'extracting toc' do
    @doc = Ronn::Document.new(File.expand_path('../markdown_syntax.ronn', __FILE__))
    expected = [
      ["NAME", "NAME"],
      ["SYNOPSIS", "SYNOPSIS"],
      ["DESCRIPTION", "DESCRIPTION"],
      ["BLOCK-ELEMENTS", "BLOCK ELEMENTS"],
      ["SPAN-ELEMENTS", "SPAN ELEMENTS"],
      ["MISCELLANEOUS", "MISCELLANEOUS"],
      ["AUTHOR", "AUTHOR"],
      ["SEE-ALSO", "SEE ALSO"]
    ]
    assert_equal expected, @doc.toc
  end

  test "passing a list of styles" do
    @doc = Ronn::Document.new('hello.1.ronn', :styles => %w[test boom test]) { '' }
    assert_equal %w[man test boom], @doc.styles
  end
end