Back to Repositories

Testing Asciidoctor Template Processing in Sinatra Framework

This test suite validates the integration and functionality of Asciidoctor processing within the Sinatra framework. It ensures proper rendering of AsciiDoc content, template handling, and layout integration for web applications built with Sinatra.

Test Coverage Overview

The test suite provides comprehensive coverage of Asciidoctor functionality in Sinatra applications.

Key areas tested include:
  • Inline AsciiDoc string rendering
  • Template engine registration and file associations
  • View path resolution and template loading
  • Error handling for missing templates
  • Layout integration with both inline and file-based approaches
  • Nested template rendering capabilities

Implementation Analysis

The testing approach utilizes Minitest framework with a mock application setup pattern. Each test case isolates specific Asciidoctor functionality while maintaining integration with Sinatra’s routing and rendering pipeline.

Notable patterns include:
  • Mock app configuration for isolated testing
  • Template engine verification
  • Layout engine compatibility testing
  • Nested rendering validation

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • Asciidoctor gem for document processing
  • Tilt templating system integration
  • ERB for layout processing
  • Custom view path configuration
  • Mock application helpers for test setup

Best Practices Demonstrated

The test suite exemplifies several testing best practices for template processing in Sinatra.

Notable practices include:
  • Isolated test environments for each scenario
  • Comprehensive error case handling
  • Multiple rendering pathway validation
  • Clear test case organization
  • Proper setup and teardown patterns
  • Graceful dependency handling with rescue block

sinatra/sinatra

test/asciidoctor_test.rb

            
require_relative 'test_helper'

begin
  require 'asciidoctor'

  class AsciidoctorTest < Minitest::Test
    def asciidoc_app(&block)
      mock_app do
        set :views, __dir__ + '/views'
        get('/', &block)
      end
      get '/'
    end

    it 'renders inline AsciiDoc strings' do
      asciidoc_app { asciidoc '== Hiya' }
      assert ok?
      assert_match %r{<h2.*?>Hiya</h2>}, body
    end

    it 'uses the correct engine' do
      engine = Tilt::AsciidoctorTemplate
      assert_equal engine, Tilt[:ad]
      assert_equal engine, Tilt[:adoc]
      assert_equal engine, Tilt[:asciidoc]
    end

    it 'renders .asciidoc files in views path' do
      asciidoc_app { asciidoc :hello }
      assert ok?
      assert_match %r{<h2.*?>Hello from AsciiDoc</h2>}, body
    end

    it 'raises error if template not found' do
      mock_app { get('/') { asciidoc :no_such_template } }
      assert_raises(Errno::ENOENT) { get('/') }
    end

    it 'renders with inline layouts' do
      mock_app do
        layout { 'THIS. IS. #{yield.upcase}!' }
        get('/') { asciidoc 'Sparta', :layout_engine => :str }
      end
      get '/'
      assert ok?
      assert_include body, 'THIS. IS.'
      assert_include body, '<P>SPARTA</P>'
    end

    it 'renders with file layouts' do
      asciidoc_app do
        asciidoc 'Hello World', :layout => :layout2, :layout_engine => :erb
      end
      assert ok?
      assert_include body, 'ERB Layout!'
      assert_include body, '<p>Hello World</p>'
    end

    it 'can be used in a nested fashion for partials and whatnot' do
      mock_app do
        template(:inner) { 'hi' }
        template(:outer) { '<outer><%= asciidoc :inner %></outer>' }
        get('/') { erb :outer }
      end
      get '/'
      assert ok?
      assert_match %r{<outer>.*<p.*?>hi</p>.*</outer>}m, body
    end
  end
rescue LoadError
  warn "#{$!}: skipping asciidoc tests"
end