referator/test.rb

258 lines
5.8 KiB
Ruby
Raw Normal View History

#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'open3'
EXE = File.expand_path('exe/referator', __dir__).freeze
@stdin, @stdout, @wait_thr = Open3.popen2 EXE, chdir: __dir__
@stdin.sync = true
@stdout.sync = true
at_exit do
@stdin.close
@stdout.close
raise 'Script error' unless @wait_thr.value.success?
end
def cmd(name, data)
@stdin.puts "#{name} #{data.to_json}"
result = @stdout.gets.chomp
if result.start_with? 'OK '
JSON.parse(result[3..]).freeze
elsif result.start_with? 'ERR '
raise "Error response: #{result[4..]}"
else
raise 'Invalid response'
end
end
2023-09-29 19:40:39 -04:00
#################
# REGISTER_KIND #
#################
cmd :REGISTER_KIND, :self
cmd :REGISTER_KIND, :link
2023-09-29 19:40:39 -04:00
###################
# REGISTER_FORMAT #
###################
cmd :REGISTER_FORMAT, { name: :html,
2023-09-29 16:38:39 -04:00
stdin: { notes: nil },
args: ['test/render.rb',
{ template: nil },
{ format: nil }] }
2023-09-29 17:02:57 -04:00
cmd :REGISTER_FORMAT, { name: :json,
stdin: { notes: nil },
args: ['test/render.rb',
{ template: nil },
{ format: nil }] }
2023-09-29 19:40:39 -04:00
###########
# ADD_REF #
###########
cmd :ADD_REF,
{ kind: :self,
id: '/blog/foo',
slug: 'blog-foo',
text: 'Foo' }
cmd :ADD_REF,
{ kind: :self,
id: '/blog/bar',
slug: 'blog-bar',
text: 'Bar' }
cmd :ADD_REF,
{ kind: :link,
id: :example_com,
slug: 'example-com',
url: 'https://example.com',
text: 'Example Domain' }
cmd :ADD_REF,
{ kind: :link,
id: :causa_arcana_com,
slug: 'causa-arcana-com',
url: 'https://causa-arcana.com',
text: 'Causa Arcana' }
2023-09-29 19:40:39 -04:00
#######
# REF #
#######
cmd(:REF, { kind: :self, id: '/blog/foo' }).tap do |result|
expected = {
'kind' => 'self',
'id' => '/blog/foo',
'slug' => 'blog-foo',
'anchor' => 'self-blog-foo',
'fragment' => '#self-blog-foo',
'text' => 'Foo',
}
raise unless result == expected
end
2023-09-29 19:40:39 -04:00
cmd(:REF, { kind: :link, id: :example_com }).tap do |result|
expected = {
'kind' => 'link',
'id' => 'example_com',
'slug' => 'example-com',
'anchor' => 'link-example-com',
'fragment' => '#link-example-com',
'url' => 'https://example.com',
'text' => 'Example Domain',
}
raise unless result == expected
end
2023-09-29 19:40:39 -04:00
############
# ADD_NOTE #
############
cmd(:ADD_NOTE, { kind: :self, id: '/blog/foo' }).tap do |result|
expected = {
'kind' => 'self',
'id' => '/blog/foo',
'slug' => 'blog-foo',
'anchor' => 'self-blog-foo',
'fragment' => '#self-blog-foo',
'text' => 'Foo',
'index' => 1,
}.freeze
raise unless result == expected
end
cmd(:ADD_NOTE, { kind: :link, id: :example_com }).tap do |result|
expected = {
'kind' => 'link',
'id' => 'example_com',
'slug' => 'example-com',
'anchor' => 'link-example-com',
'fragment' => '#link-example-com',
'url' => 'https://example.com',
'text' => 'Example Domain',
'index' => 2,
}.freeze
raise unless result == expected
end
cmd(:ADD_NOTE, { kind: :self, id: '/blog/bar' }).tap do |result|
expected = {
'kind' => 'self',
'id' => '/blog/bar',
'slug' => 'blog-bar',
'anchor' => 'self-blog-bar',
'fragment' => '#self-blog-bar',
'text' => 'Bar',
'index' => 3,
}.freeze
raise unless result == expected
end
cmd(:ADD_NOTE, { kind: :link, id: :causa_arcana_com }).tap do |result|
expected = {
'kind' => 'link',
'id' => 'causa_arcana_com',
'slug' => 'causa-arcana-com',
'anchor' => 'link-causa-arcana-com',
'fragment' => '#link-causa-arcana-com',
'url' => 'https://causa-arcana.com',
'text' => 'Causa Arcana',
'index' => 4,
}.freeze
raise unless result == expected
end
####################
# RENDER_FOOTNOTES #
####################
cmd(:RENDER_FOOTNOTES, :html).tap do |result|
2023-09-29 17:19:05 -04:00
expected = <<~HTML
<h2>Self references</h2>
<ol>
<li id="self-blog-foo" value="1">
<a href="/blog/foo.html">Foo</a>
</li>
<li id="self-blog-bar" value="3">
<a href="/blog/bar.html">Bar</a>
</li>
</ol>
<h2>Link references</h2>
<ol>
<li id="link-example-com" value="2">
<a href="https://example.com">Example Domain</a>
</li>
<li id="link-causa-arcana-com" value="4">
<a href="https://causa-arcana.com">Causa Arcana</a>
</li>
</ol>
HTML
raise unless result == expected
end
2023-09-29 19:40:39 -04:00
2023-09-29 17:02:57 -04:00
cmd(:RENDER_FOOTNOTES, :json).tap do |result|
2023-09-29 17:19:05 -04:00
expected = <<~JSON
2023-09-29 17:02:57 -04:00
{
"self": [
{
"index": 1,
"kind": "self",
"id": "/blog/foo",
"slug": "blog-foo",
"anchor": "self-blog-foo",
"fragment": "#self-blog-foo",
"url": "/blog/foo.html",
"text": "Foo"
},
{
"index": 3,
"kind": "self",
"id": "/blog/bar",
"slug": "blog-bar",
"anchor": "self-blog-bar",
"fragment": "#self-blog-bar",
"url": "/blog/bar.html",
"text": "Bar"
}
],
"link": [
{
"index": 2,
"kind": "link",
"id": "example_com",
"slug": "example-com",
"anchor": "link-example-com",
"fragment": "#link-example-com",
"url": "https://example.com",
"text": "Example Domain"
},
{
"index": 4,
"kind": "link",
"id": "causa_arcana_com",
"slug": "causa-arcana-com",
"anchor": "link-causa-arcana-com",
"fragment": "#link-causa-arcana-com",
"url": "https://causa-arcana.com",
"text": "Causa Arcana"
}
]
}
JSON
raise unless result == expected
end