Back to Repositories

Testing HTML Code Formatter Implementation in BetterErrors

This test suite validates the HTML code formatting functionality in BetterErrors, focusing on line highlighting and context display for error reporting. It ensures proper handling of source code display and error line highlighting in the Better Errors interface.

Test Coverage Overview

The test suite provides comprehensive coverage of the HTML code formatter’s core functionality:

  • Context line display verification (5 lines above/below error)
  • Edge case handling for end-of-file scenarios
  • Error line highlighting functionality
  • Invalid file and line number handling

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development patterns with context-specific test cases. It employs let blocks for setup and leverages RSpec’s expectation syntax for assertions. The tests validate both successful formatting scenarios and error handling cases.

Technical Details

Testing tools and configuration:

  • RSpec as the testing framework
  • File-based test fixtures for source code
  • Dynamic file path resolution using File.expand_path
  • Described_class pattern for test subject isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear, single-responsibility assertions
  • Proper setup using let blocks for shared resources
  • Comprehensive edge case coverage
  • Clear test descriptions that document functionality
  • Consistent error handling validation

bettererrors/better_errors

spec/better_errors/code_formatter/html_spec.rb

            
require "spec_helper"

RSpec.describe BetterErrors::CodeFormatter::HTML do
  let(:filename) { File.expand_path("../../support/my_source.rb", __FILE__) }
  let(:line) { 8 }
  let(:formatter) { described_class.new(filename, line) }

  it "shows 5 lines of context above and below the line" do
    expect(formatter.line_range).to eq(3..13)

    expect(formatter.context_lines).to eq([
        "three\n",
        "four\n",
        "five\n",
        "six\n",
        "seven\n",
        "eight\n",
        "nine\n",
        "ten\n",
        "eleven\n",
        "twelve\n",
        "thirteen\n"
      ])
  end

  context 'when the line is right at the end of the file' do
    let(:line) { 20 }
    it "ends on the line" do
      expect(formatter.line_range).to eq(15..20)
    end
  end

  it "highlights the erroring line" do
    formatter = described_class.new(filename, 8)
    expect(formatter.output).to match(/highlight.*eight/)
  end

  it "works when the line is right on the edge" do
    formatter = described_class.new(filename, 20)
    expect(formatter.output).not_to eq(formatter.source_unavailable)
  end

  it "doesn't barf when the lines don't make any sense" do
    formatter = described_class.new(filename, 999)
    expect(formatter.output).to eq(formatter.source_unavailable)
  end

  it "doesn't barf when the file doesn't exist" do
    formatter = described_class.new("fkdguhskd7e l", 1)
    expect(formatter.output).to eq(formatter.source_unavailable)
  end
end