summaryrefslogtreecommitdiff
path: root/spec/scripts/lib/glfm/parse_examples_spec.rb
blob: a1ee6b3f440ebec2385859ba4af0b4e161f64ab3 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# frozen_string_literal: true
require 'fast_spec_helper'
require_relative '../../../../scripts/lib/glfm/parse_examples'

RSpec.describe Glfm::ParseExamples, '#parse_examples' do
  subject do
    described_module = described_class
    Class.new { include described_module }.new
  end

  let(:spec_txt_contents) do
    <<~MARKDOWN
      ---
      title: Spec
      ...

      # Introduction

      intro

      # 1.0.0 H1

      ## 1.1.0 H2

      no extension

      ```````````````````````````````` example
      example 1 md
      .
      html
      ````````````````````````````````

      one extension

      ```````````````````````````````` example extension_1.1.0-1
      example 2 md
      .
      html
      ````````````````````````````````

      ### 1.1.1 H3 with no examples

      text

      ### 1.1.2 Consecutive H3 with example

      text

      ```````````````````````````````` example disabled
      example 3 md
      .
      html
      ````````````````````````````````

      ## 1.2.0 H2 with all disabled examples


      ```````````````````````````````` example disabled
      example 4 md
      .
      html
      ````````````````````````````````

      ## 1.2.0 New H2


      ```````````````````````````````` example extension_1.2.0-1
      example 5 md
      .
      html
      ````````````````````````````````

      # 2.0.0 New H1

      ## 2.1.0 H2

      ```````````````````````````````` example gitlab
      example 6 md
      .
      html
      ````````````````````````````````

      ## 2.2.0 H2 which contains an H3

      No example here, just text

      ### 2.2.1 H3

      The CommonMark and GHFM specifications don't have any examples inside an H3, but it is
      supported for the GLFM specification.

      ```````````````````````````````` example extension_2.2.1-1
      example 7 md
      .
      html
      ````````````````````````````````

      # 3.0.0 New H1

      ## 3.1.0 H2

      ```````````````````````````````` example
      example 8 md
      .
      html
      ````````````````````````````````

      ### 3.1.1 H3

      ```````````````````````````````` example
      example 9 md
      .
      html
      ````````````````````````````````

      ### 3.1.1 Consecutive H3

      ```````````````````````````````` example
      example 10 md
      .
      html
      ````````````````````````````````

      ## 3.2.0 Another H2

      ```````````````````````````````` example
      example 11 md
      .
      html
      ````````````````````````````````

      <!-- END TESTS -->

      # Appendix

      Appendix text.
    MARKDOWN
  end

  let(:spec_txt_lines) { spec_txt_contents.split("\n") }

  describe "parsing" do
    it 'correctly parses' do
      examples = subject.parse_examples(spec_txt_lines)

      expected =
        [
          {
            disabled: false,
            end_line: 19,
            example: 1,
            extensions: [],
            headers: [
              '1.0.0 H1',
              '1.1.0 H2'
            ],
            html: 'html',
            markdown: 'example 1 md',
            section: '1.1.0 H2',
            start_line: 15
          },
          {
            disabled: false,
            end_line: 27,
            example: 2,
            extensions: %w[extension_1.1.0-1],
            headers: [
              '1.0.0 H1',
              '1.1.0 H2'
            ],
            html: 'html',
            markdown: 'example 2 md',
            section: '1.1.0 H2',
            start_line: 23
          },
          {
            disabled: true,
            end_line: 41,
            example: 3,
            extensions: %w[disabled],
            headers: [
              '1.0.0 H1',
              '1.1.0 H2',
              '1.1.2 Consecutive H3 with example'
            ],
            html: 'html',
            markdown: 'example 3 md',
            section: '1.1.2 Consecutive H3 with example',
            start_line: 37
          },
          {
            disabled: true,
            end_line: 50,
            example: 4,
            extensions: %w[disabled],
            headers: [
              '1.0.0 H1',
              '1.2.0 H2 with all disabled examples'
            ],
            html: 'html',
            markdown: 'example 4 md',
            section: '1.2.0 H2 with all disabled examples',
            start_line: 46
          },
          {
            disabled: false,
            end_line: 59,
            example: 5,
            extensions: %w[extension_1.2.0-1],
            headers: [
              '1.0.0 H1',
              '1.2.0 New H2'
            ],
            html: 'html',
            markdown: 'example 5 md',
            section: '1.2.0 New H2',
            start_line: 55
          },
          {
            disabled: false,
            end_line: 69,
            example: 6,
            extensions: %w[gitlab],
            headers: [
              '2.0.0 New H1',
              '2.1.0 H2'
            ],
            html: 'html',
            markdown: 'example 6 md',
            section: '2.1.0 H2',
            start_line: 65
          },
          {
            disabled: false,
            end_line: 84,
            example: 7,
            extensions: %w[extension_2.2.1-1],
            headers: [
              '2.0.0 New H1',
              '2.2.0 H2 which contains an H3',
              '2.2.1 H3'
            ],
            html: 'html',
            markdown: 'example 7 md',
            section: '2.2.1 H3',
            start_line: 80
          },
          {
            disabled: false,
            end_line: 94,
            example: 8,
            extensions: [],
            headers: [
              '3.0.0 New H1',
              '3.1.0 H2'
            ],
            html: 'html',
            markdown: 'example 8 md',
            section: '3.1.0 H2',
            start_line: 90
          },
          {
            disabled: false,
            end_line: 102,
            example: 9,
            extensions: [],
            headers: [
              '3.0.0 New H1',
              '3.1.0 H2',
              '3.1.1 H3'
            ],
            html: 'html',
            markdown: 'example 9 md',
            section: '3.1.1 H3',
            start_line: 98
          },
          {
            disabled: false,
            end_line: 110,
            example: 10,
            extensions: [],
            headers: [
              '3.0.0 New H1',
              '3.1.0 H2',
              '3.1.1 Consecutive H3'
            ],
            html: 'html',
            markdown: 'example 10 md',
            section: '3.1.1 Consecutive H3',
            start_line: 106
          },
          {
            disabled: false,
            end_line: 118,
            example: 11,
            extensions: [],
            headers: [
              '3.0.0 New H1',
              '3.2.0 Another H2'
            ],
            html: 'html',
            markdown: 'example 11 md',
            section: '3.2.0 Another H2',
            start_line: 114
          }
        ]

      expect(examples).to eq(expected)
    end
  end

  describe "with incorrect header nesting" do
    let(:spec_txt_contents) do
      <<~MARKDOWN
        ---
        title: Spec
        ...

        # H1

        ### H3

      MARKDOWN
    end

    it "raises if H3 is nested directly in H1" do
      expect { subject.parse_examples(spec_txt_lines) }
        .to raise_error(/The H3 'H3' may not be nested directly within the H1 'H1'/)
    end
  end
end