From 73a8e15de98ba1dc206db9246437eabecbd70016 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 30 Sep 2023 01:02:57 +0400 Subject: [PATCH] Test multiple formats --- test.rb | 57 +++++++++++++++++++++++++++++++++++++++++ test/footnotes.html.erb | 12 ++++++--- test/footnotes.json.erb | 33 ++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 test/footnotes.json.erb diff --git a/test.rb b/test.rb index eeb1ec7..6b208dd 100755 --- a/test.rb +++ b/test.rb @@ -37,6 +37,11 @@ cmd :REGISTER_FORMAT, { name: :html, args: ['test/render.rb', { template: nil }, { format: nil }] } +cmd :REGISTER_FORMAT, { name: :json, + stdin: { notes: nil }, + args: ['test/render.rb', + { template: nil }, + { format: nil }] } cmd :ADD_REF, { kind: :self, @@ -119,3 +124,55 @@ cmd(:RENDER_FOOTNOTES, :html).tap do |result| raise unless result == expected end +cmd(:RENDER_FOOTNOTES, :json).tap do |result| + expected = <<~JSON.freeze + { + "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 diff --git a/test/footnotes.html.erb b/test/footnotes.html.erb index 0fe7170..c95e307 100644 --- a/test/footnotes.html.erb +++ b/test/footnotes.html.erb @@ -1,21 +1,25 @@ -<% if notes.any? { |note| note['kind'] == 'self' } -%> +<% self_notes = notes.select { |note| note['kind'] == 'self' } -%> +<% link_notes = notes.select { |note| note['kind'] == 'link' } -%> +<%# -%> +<% unless self_notes.empty? -%>

Self references

    -<% notes.select { |note| note['kind'] == 'self' }.each do |note| -%> +<% self_notes.each do |note| -%>
  1. <%= note['text'] %>
  2. <% end -%>
<% end -%> -<% if notes.any? { |note| note['kind'] == 'link' } -%> +<%# -%> +<% unless link_notes.empty? -%>

Link references

    -<% notes.select { |note| note['kind'] == 'link' }.each do |note| -%> +<% link_notes.each do |note| -%>
  1. <%= note['text'] %>
  2. diff --git a/test/footnotes.json.erb b/test/footnotes.json.erb new file mode 100644 index 0000000..f2e058e --- /dev/null +++ b/test/footnotes.json.erb @@ -0,0 +1,33 @@ +<% self_notes = notes.select { |note| note['kind'] == 'self' } -%> +<% link_notes = notes.select { |note| note['kind'] == 'link' } -%> +<%# -%> +{ + "self": [ + <%- self_notes.each_with_index do |note, index| -%> + { + "index": <%= Integer(note['index']).to_json %>, + "kind": "self", + "id": <%= String(note['id']).to_json %>, + "slug": <%= String(note['slug']).to_json %>, + "anchor": <%= String(note['anchor']).to_json %>, + "fragment": <%= String(note['fragment']).to_json %>, + "url": <%= "#{note['id']}.html".to_json %>, + "text": <%= String(note['text']).to_json %> + }<%= ',' unless index == self_notes.count - 1 %> + <%- end -%> + ], + "link": [ + <%- link_notes.each_with_index do |note, index| -%> + { + "index": <%= Integer(note['index']).to_json %>, + "kind": "link", + "id": <%= String(note['id']).to_json %>, + "slug": <%= String(note['slug']).to_json %>, + "anchor": <%= String(note['anchor']).to_json %>, + "fragment": <%= String(note['fragment']).to_json %>, + "url": <%= String(note['url']).to_json %>, + "text": <%= String(note['text']).to_json %> + }<%= ',' unless index == link_notes.count - 1 %> + <%- end -%> + ] +}