File: test_helper.rb

package info (click to toggle)
ruby-commonmarker 0.23.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,456 kB
  • sloc: ansic: 10,575; ruby: 1,741; sh: 36; makefile: 22
file content (71 lines) | stat: -rw-r--r-- 1,796 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
# frozen_string_literal: true

require "commonmarker"
require "minitest/autorun"
require "minitest/pride"
require "minitest/focus"

include CommonMarker

FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")

def fixtures_file(file)
  File.read(File.join(FIXTURES_DIR, file), encoding: "utf-8")
end

def make_bin(file, args = "")
  %x(ruby#{RUBY_VERSION[/\d+\.\d+/]} bin/commonmarker #{File.join(FIXTURES_DIR, file)} #{args}).chomp
end

def open_spec_file(filename)
  line_number = 0
  start_line = 0
  end_line = 0
  example_number = 0
  markdown_lines = []
  html_lines = []
  state = 0 # 0 regular text, 1 markdown example, 2 html output
  headertext = ""
  tests = []
  extensions = []

  header_re = Regexp.new("#+ ")
  filepath = File.join("ext", "commonmarker", "cmark-upstream", "test", filename)

  File.readlines(filepath, encoding: "utf-8").each do |line|
    line_number += 1

    l = line.strip
    if l =~ /^`{32} example(.*)$/
      state = 1
      extensions = Regexp.last_match(1).split
    elsif l == "`" * 32
      state = 0
      example_number += 1
      end_line = line_number
      tests << {
        markdown: markdown_lines.join.tr("→", "\t"),
        html: html_lines.join.tr("→", "\t").rstrip,
        example: example_number,
        start_line: start_line,
        end_line: end_line,
        section: headertext,
        extensions: extensions.map(&:to_sym),
      }
      start_line = 0
      markdown_lines = []
      html_lines = []
    elsif l == "."
      state = 2
    elsif state == 1
      start_line = line_number - 1 if start_line.zero?
      markdown_lines << line.to_s
    elsif state == 2
      html_lines << line.to_s
    elsif state.zero? && header_re.match(line)
      headertext = line.sub(header_re, "").strip
    end
  end

  tests
end