File: line_spec.rb

package info (click to toggle)
ruby-org 0.9.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 3,044; lisp: 50; makefile: 4
file content (216 lines) | stat: -rw-r--r-- 7,061 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require 'spec_helper'

describe Orgmode::Line do

  it "should tell comments" do
    comments = ["# hello", " # hello" ]
    comments.each do |c|
      line = Orgmode::Line.new c
      expect(line.comment?).to be_truthy
    end

    not_comments = ["", "\n", "hello\n", "  foo ### bar\n"]
    not_comments.each do |c|
      line = Orgmode::Line.new c
      expect(line.comment?).to be_falsy
    end
  end

  it "should tell blank lines" do
    blank = ["", " ", "\t", "\n", "  \t\t\n\n"]
    blank.each do |b|
      line = Orgmode::Line.new b
      expect(line.blank?).to be_truthy
    end
  end

  [": inline", " : inline", "\t\t:\tinline"].each do |inline_example|
    it "should recognize this inline example: #{inline_example}" do
      expect(Orgmode::Line.new(inline_example).inline_example?).to be_truthy
    end
  end

  list_formats = ["- ",
                  "+ ",
                  "  - ",
                  "  + ",
                  " 1. ",
                  " 2) "]
  list_formats.each do |list|
    it "should recognize this list format: '#{list}'" do
      line = Orgmode::Line.new list
      expect(line.plain_list?).to be_truthy
    end
  end

  ["-foo", "+foo", "1.foo", "2.foo"].each do |invalid_list|
    it "should not recognize this invalid list: '#{invalid_list}'" do
      line = Orgmode::Line.new invalid_list
      expect(line.plain_list?).to be_falsy
    end
  end

  it "should recognize horizontal rules" do
    expect(Orgmode::Line.new("-----").horizontal_rule?).to be_truthy
    expect(Orgmode::Line.new("----------").horizontal_rule?).to be_truthy
    expect(Orgmode::Line.new("   \t ----- \t\t\t").horizontal_rule?).to be_truthy
    expect(Orgmode::Line.new("----").horizontal_rule?).to be_falsy
  end

  it "should recognize table rows" do
   expect(Orgmode::Line.new("| One   | Two   | Three |").table_row?).to be_truthy
   expect(Orgmode::Line.new("  |-------+-------+-------|\n").table_separator?).to be_truthy
   expect(Orgmode::Line.new("| Four  | Five  | Six   |").table_row?).to be_truthy
   expect(Orgmode::Line.new("| Seven | Eight | Nine  |").table_row?).to be_truthy
  end

  it "should recognize indentation" do
    expect(Orgmode::Line.new("").indent).to eql(0)
    expect(Orgmode::Line.new(" a").indent).to eql(1)
    expect(Orgmode::Line.new("   ").indent).to eql(0)
    expect(Orgmode::Line.new("   \n").indent).to eql(0)
    expect(Orgmode::Line.new("   a").indent).to eql(3)
  end

  it "should return paragraph type" do
    expect(Orgmode::Line.new("").paragraph_type).to eql(:blank)
    expect(Orgmode::Line.new("1. foo").paragraph_type).to eql(:list_item)
    expect(Orgmode::Line.new("- [ ] checkbox").paragraph_type).to eql(:list_item)
    expect(Orgmode::Line.new("hello!").paragraph_type).to eql(:paragraph)
  end

  it "should recognize BEGIN and END comments" do
    begin_examples = {
      "#+BEGIN_SRC emacs-lisp -n -r\n" => "SRC",
      "#+BEGIN_EXAMPLE" => "EXAMPLE",
      "\t#+BEGIN_QUOTE  " => "QUOTE"
    }

    end_examples = {
      "#+END_SRC" => "SRC",
      "#+END_EXAMPLE" => "EXAMPLE",
      "\t#+END_QUOTE  " => "QUOTE"
    }

    begin_examples.each_key do |str|
      line = Orgmode::Line.new str
      expect(line.begin_block?).to be_truthy
      expect(line.block_type).to eql(begin_examples[str])
    end

    end_examples.each_key do |str|
      line = Orgmode::Line.new str
      expect(line.end_block?).to be_truthy
      expect(line.block_type).to eql(end_examples[str])
    end
  end

  context "when a block has header arguments" do
    cases = {
      "#+begin_src :hello world" => {
        ':hello' => 'world'
      },
      "#+begin_src ruby -n -r -l \"asdf\" asdf asdf :asdf asdf" => {
        ':asdf' => 'asdf'
      },
      "#+begin_src ruby :results \"he:llo\" :results :hello :tangle somewhere.rb :exports code :shebang #!/bin/bash" => {
        ':results' => '"he:llo"', ':tangle' => 'somewhere.rb', ':exports' => 'code', ':shebang' => '#!/bin/bash'
      },
      "#+begin_src clojure :results :hello :tangle somewhere.rb :exports code" => {
        ':tangle' => 'somewhere.rb', ':exports' => 'code'
      },
      "#+begin_src js :results output :hello :tangle somewhere.rb :exports code" => {
        ':results' => 'output', ':tangle' => 'somewhere.rb', ':exports' => 'code'
      }
    }

    cases.each_pair do |example, expected|
      it "should recognize #{example}" do 
        line = Orgmode::Line.new example
        expect(line.block_header_arguments).to eq(expected)
      end
    end
  end

  context "when initializing" do
    cases = {
      "# this looks like a comment" => :comment,
      "  1. This looks like an ordered list" => :ordered_list,
      "       - this looks like an # unordered list" => :unordered_list,
      " | one | two | table! |  \n" => :table_row,
      "\n" => :blank,
      " |-----+-----+--------|  \n" => :table_separator
    }

    cases.each_pair do |example, expected|
      it "should accept '#{example}' with assigned type #{expected}" do 
        line = Orgmode::Line.new(example, nil, expected)
        expect(line.assigned_paragraph_type).to eq(expected)
      end
    end
  end

  it "should parse in-buffer settings" do
    cases = {
      "#+ARCHIVE: %s_done" => { :key => "ARCHIVE", :value => "%s_done" },
      "#+CATEGORY: foo" => { :key => "CATEGORY", :value => "foo"},
      "#+BEGIN_EXAMPLE:" => { :key => "BEGIN_EXAMPLE", :value => "" },
      "#+A:" => { :key => "A", :value => "" } # Boundary: Smallest keyword is one letter
    }
    cases.each_pair do |key, value|
      l = Orgmode::Line.new key
      expect(l.in_buffer_setting?).to be_truthy
      called = nil
      l.in_buffer_setting? do |k, v|
        expect(k).to eql(value[:key])
        expect(v).to eql(value[:value])
        called = true
      end
      expect(called).to be true
    end
  end

  it "should reject ill-formed settings" do
    cases = [
             "##+ARCHIVE: blah",
             "#CATEGORY: foo",
             "",
             "\n",
             "   #+BEGIN_EXAMPLE:\n"
            ]

    cases.each do |c|
      l = Orgmode::Line.new c
      expect(l.in_buffer_setting?).to be_nil
    end
  end

  context "at the start of a results block" do
    cases = [
             "#+RESULTS: hello-world",
             "#+RESULTS: ",
             "#+RESULTS:",
             "#+results: HELLO-WORLD",
             "#+results: ",
             "#+results:"
            ]
    cases.each do |c|
      it "should recognize #{c}" do 
        l = Orgmode::Line.new(c).start_of_results_code_block?
        expect(l).to be_truthy
      end
    end
  end

  it "should recognize an included file" do
    expect(Orgmode::Line.new("#+INCLUDE: \"~/somefile.org\"").include_file?).to be_truthy
  end

  it "should recognize an included file with specific lines" do
    expect(Orgmode::Line.new("#+INCLUDE: \"~/somefile.org\" :lines \"4-18\"").include_file?).to be_truthy
  end

  it "should recognize an included code file" do
    expect(Orgmode::Line.new("#+INCLUDE: \"~/somefile.org\" src ruby").include_file?).to be_truthy
  end
end