|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
| 3 | +require_relative 'dsl/convert_all' |
| 4 | +require_relative 'dsl/convert_single' |
| 5 | +require_relative 'dsl/file_contexts' |
| 6 | + |
3 | 7 | ##
|
4 |
| -# Defines methods to create contexts for converting asciidoc files to html. |
| 8 | +# Defines methods to create contexts and shared examples used in the tests. |
5 | 9 | module Dsl
|
6 |
| - ## |
7 |
| - # Create a context to assert things about an html page. By default it just |
8 |
| - # asserts that the page was created but if you pass a block you can add |
9 |
| - # assertions on `body` and `title`. |
10 |
| - def page_context(file_name, &block) |
11 |
| - context "for #{file_name}" do |
12 |
| - include_context 'page', file_name |
13 |
| - |
14 |
| - # Yield to the block to add more tests. |
15 |
| - class_exec(&block) |
16 |
| - end |
17 |
| - end |
18 |
| - shared_context 'page' do |file_name| |
19 |
| - let(:file) do |
20 |
| - dest_file(file_name) |
21 |
| - end |
22 |
| - let(:body) do |
23 |
| - return unless File.exist? file |
24 |
| - |
25 |
| - File.open(dest_file(file), 'r:UTF-8') do |f| |
26 |
| - f.read |
27 |
| - .sub(/.+<!-- start body -->/m, '') |
28 |
| - .sub(/<!-- end body -->.+/m, '') |
29 |
| - end |
30 |
| - end |
31 |
| - let(:title) do |
32 |
| - return unless body |
33 |
| - |
34 |
| - m = body.match %r{<h1 class="title"><a id=".+"></a>([^<]+)(<a.+?)?</h1>} |
35 |
| - raise "Can't find title in #{body}" unless m |
36 |
| - |
37 |
| - m[1] |
38 |
| - end |
39 |
| - |
40 |
| - it 'is created' do |
41 |
| - expect(file).to file_exist |
42 |
| - end |
43 |
| - end |
44 |
| - |
45 |
| - ## |
46 |
| - # Include a context into the current context that converts asciidoc files |
47 |
| - # into html and adds some basic assertions about the conversion process. Pass |
48 |
| - # a block that takes a `Source` object and returns the "root" asciidoc file |
49 |
| - # to convert. It does the conversion with both with `--asciidoctor` and |
50 |
| - # without `--asciidoctor` and asserts that the files are the same. |
51 |
| - def convert_single_before_context |
52 |
| - include_context 'tmp dirs' |
53 |
| - before(:context) do |
54 |
| - from = yield(Source.new @src) |
55 |
| - @asciidoctor_out = convert_single from, @dest, |
56 |
| - asciidoctor: true |
57 |
| - # Convert a second time with the legacy `AsciiDoc` tool and stick the |
58 |
| - # result into the `asciidoc` directory. We will compare the results of |
59 |
| - # this conversion with the results of the `Asciidoctor` conversion. |
60 |
| - @asciidoc_out = convert_single from, "#{@dest}/asciidoc", |
61 |
| - asciidoctor: false |
62 |
| - end |
63 |
| - include_examples 'convert single' |
64 |
| - end |
65 |
| - shared_context 'convert single' do |
66 |
| - let(:out) { @asciidoctor_out } |
67 |
| - let(:asciidoctor_files) do |
68 |
| - files_in(dest_file('.')).reject { |f| f.start_with? 'asciidoc/' } |
69 |
| - end |
70 |
| - let(:asciidoc_files) do |
71 |
| - files_in(dest_file('asciidoc')) |
72 |
| - end |
73 |
| - it 'prints the path to the html index' do |
74 |
| - expect(out).to include(dest_file('index.html')) |
75 |
| - end |
76 |
| - it 'creates the template hash' do |
77 |
| - expect(dest_file('template.md5')).to file_exist |
78 |
| - end |
79 |
| - it 'creates the css' do |
80 |
| - expect(dest_file('styles.css')).to file_exist |
81 |
| - end |
82 |
| - it 'creates the js' do |
83 |
| - expect(dest_file('docs.js')).to file_exist |
84 |
| - end |
85 |
| - it 'logs the same lines with asciidoc' do |
86 |
| - # The only difference should be that the output path includes `asciidoc/` |
87 |
| - expect(@asciidoc_out.gsub('asciidoc/', '')).to eq(@asciidoctor_out) |
88 |
| - end |
89 |
| - it 'makes the same files with asciidoc' do |
90 |
| - expect(asciidoc_files).to eq(asciidoctor_files) |
91 |
| - end |
92 |
| - it 'makes exactly the same html files with asciidoc' do |
93 |
| - # This does *all* the files in the same example which doesn't feel very |
94 |
| - # rspec but it gets the job done and we don't know the file list at this |
95 |
| - # point so there isn't much we can do about it. |
96 |
| - asciidoctor_files.each do |file| |
97 |
| - next unless File.extname(file) == '.html' |
98 |
| - |
99 |
| - # We shell out to the html_diff tool that we wrote for this when the |
100 |
| - # integration tests were all defined in a Makefile. It isn't great to |
101 |
| - # shell out here but we've already customized html_diff. |
102 |
| - asciidoctor_file = dest_file(file) |
103 |
| - asciidoc_file = dest_file("asciidoc/#{file}") |
104 |
| - html_diff = File.expand_path '../../html_diff', __dir__ |
105 |
| - sh "#{html_diff} #{asciidoctor_file} #{asciidoc_file}" |
106 |
| - end |
107 |
| - end |
108 |
| - end |
109 |
| - |
110 |
| - class Source |
111 |
| - def initialize(root) |
112 |
| - @root = root |
113 |
| - end |
114 |
| - |
115 |
| - ## |
116 |
| - # Write a source file and return the absolute path to that file. |
117 |
| - def write(path, text) |
118 |
| - path = "#{@root}/#{path}" |
119 |
| - File.open(path, 'w:UTF-8') do |f| |
120 |
| - f.write text |
121 |
| - end |
122 |
| - path |
123 |
| - end |
124 |
| - end |
| 10 | + include ConvertAll |
| 11 | + include ConvertSingle |
| 12 | + include FileContexts |
125 | 13 | end
|
0 commit comments