File: test_ronn.rb

package info (click to toggle)
ruby-ronn 0.10.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 764 kB
  • sloc: ruby: 1,587; sh: 22; makefile: 9
file content (124 lines) | stat: -rw-r--r-- 4,132 bytes parent folder | download | duplicates (2)
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
require 'contest'

class RonnTest < Test::Unit::TestCase
  testdir = File.dirname(__FILE__)

  # setup PATH so that we execute the right ronn command
  bindir = File.dirname(testdir) + '/bin'
  ENV['PATH'] = "#{bindir}:#{ENV.fetch('PATH', nil)}"

  # make sure the load path is setup correctly
  libdir = File.expand_path("#{testdir}/../lib")
  $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.any? { |path| File.expand_path(path) == libdir }
  ENV['RUBYLIB'] = $LOAD_PATH.join(':')

  require 'ronn'

  test 'takes ronn text on stdin and produces roff on stdout' do
    output = `echo '# hello(1) -- hello world' | #{RbConfig.ruby} -S ronn --date=2009-11-23`
    lines = output.split("\n")
    assert_equal 5, lines.size
    assert_equal %(.\\" generated with Ronn-NG/v#{Ronn.version}), lines.shift
    assert_equal %(.\\" http://github.com/apjanke/ronn-ng/tree/#{Ronn.revision}), lines.shift
    assert_equal %(.TH "HELLO" "1" "November 2009" ""), lines.shift
    assert_equal %(.SH "NAME"), lines.shift
    assert_equal %(\\fBhello\\fR \\- hello world), lines.shift
    assert_equal 0, lines.size
  end

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

  def flunk_with_diff(dest, wrong, output)
    File.binwrite(wrong, output)
    if ENV['RONN_QUIET_TEST'] == '1'
      flunk 'Output did not match expected.'
    else
      diff = `diff -u #{dest} #{wrong} 2>/dev/null`
      flunk diff
    end
  end

  test 'produces html instead of roff with the --html argument' do
    output = `echo '# hello(1) -- hello world' | #{RbConfig.ruby} -S ronn --html`
    output = canonicalize(output)
    assert_match(/<h2 id='NAME'>NAME<\/h2>/, output)
    advertising = "<meta name='generator' content='Ronn-NG/v" + Ronn.version +
                  ' (http://github.com/apjanke/ronn-ng/tree/' + Ronn.revision + ")'>"
    assert_match(/#{Regexp.escape(advertising)}/, output)
  end

  test 'produces html fragment with the --fragment argument' do
    output = `echo '# hello(1) -- hello world' | #{RbConfig.ruby} -S ronn --fragment`
    assert_equal [
      "<div class='mp'>",
      "<h2 id='NAME'>NAME</h2>",
      "<p class='man-name'><code>hello</code>",
      " - <span class='man-whatis'>hello world</span>",
      '</p></div>'
    ].join, canonicalize(output)
  end

  test 'abides by the RONN_MANUAL environment variable' do
    output = `echo '# hello(1) -- hello world' | RONN_MANUAL='Some Manual' #{RbConfig.ruby} -S ronn --html`
    assert_match(/Some Manual/, output)
  end

  test 'abides by the RONN_DATE environment variable' do
    output = `echo '# hello(1) -- hello world' | RONN_DATE=1979-01-01 #{RbConfig.ruby} -S ronn --html`
    assert_match(/January 1979/, output)
  end

  test 'abides by the RONN_ORGANIZATION environment variable' do
    output = `echo '# hello(1) -- hello world' | RONN_ORGANIZATION='GitHub' #{RbConfig.ruby} -S ronn --html`
    assert_match(/GitHub/, output)
  end

  # ronn -> HTML file based tests
  Dir[testdir + '/*.ronn'].each do |source|
    dest = source.sub(/ronn$/, 'html')
    next unless File.exist?(dest)

    wrong = dest + '.wrong'
    test File.basename(source, '.ronn') + ' HTML' do
      output = `#{RbConfig.ruby} -S ronn --pipe --html --fragment #{source}`
      expected = begin
        File.read(dest)
      rescue IOError
        ''
      end
      if expected != output
        flunk_with_diff(dest, wrong, output)
      else
        FileUtils.rm_f(wrong)
      end
    end
  end

  # ronn -> roff file based tests
  Dir[testdir + '/*.ronn'].each do |source|
    dest = source.sub(/ronn$/, 'roff')
    next unless File.exist?(dest)

    wrong = dest + '.wrong'
    test File.basename(source, '.ronn') + ' roff' do
      output = `#{RbConfig.ruby} -S ronn --pipe --roff --date=1979-01-01 #{source}`
               .split("\n", 3).last # remove ronn version comments
      expected = begin
        File.read(dest)
      rescue IOError
        ''
      end
      if expected != output
        flunk_with_diff(dest, wrong, output)
      else
        FileUtils.rm_f(wrong)
      end
    end
  end
end