Always return status and result; add tests

This commit is contained in:
Alex Kotov 2023-09-29 22:46:13 +04:00
parent 37a9f56d10
commit c97f60619f
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 212 additions and 34 deletions

View file

@ -17,6 +17,14 @@ def parse_command(line)
[command, rest].freeze [command, rest].freeze
end end
def success(data = nil)
puts "OK #{data.to_json}"
end
def failure(msg)
puts "ERR #{String(msg).lines.join(' ')}"
end
workdir = ARGV.first workdir = ARGV.first
workdir = Dir.pwd if String(workdir).strip.empty? workdir = Dir.pwd if String(workdir).strip.empty?
workdir = File.expand_path(workdir).freeze workdir = File.expand_path(workdir).freeze
@ -25,39 +33,49 @@ config = Referator::Config.new workdir
footnotes = Referator::Footnotes.new config footnotes = Referator::Footnotes.new config
while (line = $stdin.gets) while (line = $stdin.gets)
begin
command, rest = parse_command line command, rest = parse_command line
case command case command
when 'EXIT'
success
exit
when 'REGISTER_FORMAT' when 'REGISTER_FORMAT'
data = JSON.parse rest data = JSON.parse rest
config.formats.register( config.formats.register(
String(data['name']).to_sym, String(data['name']).to_sym,
**data.except('name').transform_keys(&:to_sym), **data.except('name').transform_keys(&:to_sym),
) )
success
when 'REGISTER_KIND' when 'REGISTER_KIND'
data = JSON.parse rest data = JSON.parse rest
config.kinds.register String(data).to_sym config.kinds.register String(data).to_sym
success
when 'ADD_REF' when 'ADD_REF'
data = JSON.parse rest data = JSON.parse rest
config.repo.add_ref(**data.transform_keys(&:to_sym)) config.repo.add_ref(**data.transform_keys(&:to_sym))
success
when 'ADD_NOTE' when 'ADD_NOTE'
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
puts footnotes.add_note( success footnotes.add_note(
String(data['kind']).to_sym, String(data['kind']).to_sym,
String(data['id']), String(data['id']),
).to_h.to_json ).to_h
when 'REF' when 'REF'
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
puts config.repo[ success config.repo[
String(data['kind']).to_sym, String(data['kind']).to_sym,
String(data['id']), String(data['id']),
].to_h.to_json ].to_h
when 'RENDER_FOOTNOTES' when 'RENDER_FOOTNOTES'
config.freeze config.freeze
data = JSON.parse rest data = JSON.parse rest
puts footnotes.render(String(data).to_sym).to_json success String footnotes.render String(data).to_sym
else else
raise 'Invalid command' raise 'Invalid command'
end end
rescue => e
failure e.detailed_message
end
end end

119
test.rb Executable file
View file

@ -0,0 +1,119 @@
#!/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
cmd :REGISTER_KIND, :self
cmd :REGISTER_KIND, :link
cmd :REGISTER_FORMAT, { name: :html,
args: ['test/footnotes.rb', { format: nil }],
stdin: { notes: nil } }
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' }
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
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
cmd :ADD_NOTE, { kind: :self, id: '/blog/foo' }
cmd :ADD_NOTE, { kind: :link, id: :example_com }
cmd :ADD_NOTE, { kind: :self, id: '/blog/bar' }
cmd :ADD_NOTE, { kind: :link, id: :causa_arcana_com }
cmd(:RENDER_FOOTNOTES, :html).tap do |result|
expected = <<~HTML.freeze
<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

24
test/footnotes.html.erb Normal file
View file

@ -0,0 +1,24 @@
<% if notes.any? { |note| note['kind'] == 'self' } -%>
<h2>Self references</h2>
<ol>
<% notes.select { |note| note['kind'] == 'self' }.each do |note| -%>
<li id="<%= note['anchor'] %>" value="<%= note['index'] %>">
<a href="<%= note['id'] %>.html"><%= note['text'] %></a>
</li>
<% end -%>
</ol>
<% end -%>
<% if notes.any? { |note| note['kind'] == 'link' } -%>
<h2>Link references</h2>
<ol>
<% notes.select { |note| note['kind'] == 'link' }.each do |note| -%>
<li id="<%= note['anchor'] %>" value="<%= note['index'] %>">
<a href="<%= note['url'] %>"><%= note['text'] %></a>
</li>
<% end -%>
</ol>
<% end -%>

17
test/footnotes.rb Executable file
View file

@ -0,0 +1,17 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'erb'
require 'json'
require 'pathname'
def template(filename)
pathname = Pathname.new(__dir__).join(filename).expand_path.freeze
ERB.new(pathname.read, trim_mode: '-').tap do |erb|
erb.filename = pathname.to_s.freeze
end.freeze
end
puts template("footnotes.#{ARGV.first}.erb").result_with_hash(
notes: JSON.parse($stdin.read),
).strip