Back to Repositories

Testing Image Geometry Parsing Functionality in Paperclip

This test suite validates the GeometryParser component of the Paperclip gem, focusing on its ability to parse image dimensions, orientations, and modifiers. The tests ensure accurate extraction and handling of various image geometry string formats commonly used in image processing workflows.

Test Coverage Overview

The test suite provides comprehensive coverage of the GeometryParser functionality.

Key areas tested include:
  • Basic dimension parsing (width x height)
  • Handling of empty orientation values
  • Processing of orientation metadata
  • Parsing of image modifiers
  • Combined parsing of dimensions, orientations, and modifiers

Implementation Analysis

The tests utilize RSpec’s stubbing capabilities to isolate the GeometryParser component. Each test case follows a consistent pattern of arranging input data, creating a parser instance, and verifying the parsed output against expected values. The implementation leverages Paperclip::Geometry.stubs for dependency isolation.

Technical patterns include:
  • Factory pattern implementation
  • Stub-based dependency injection
  • Consistent test structure using arrange-act-assert pattern

Technical Details

Testing tools and configuration:
  • RSpec testing framework
  • Stub mocking functionality
  • Paperclip::Geometry dependency
  • Custom geometry string format parsing
  • Unit test isolation techniques

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby and RSpec.

Notable practices include:
  • Single responsibility principle in test cases
  • Clear test naming conventions
  • Consistent assertion patterns
  • Proper test isolation through stubbing
  • Comprehensive edge case coverage

thoughtbot/paperclip

spec/paperclip/geometry_parser_spec.rb

            
require 'spec_helper'

describe Paperclip::GeometryParser do
  it 'identifies an image and extract its dimensions with no orientation' do
    Paperclip::Geometry.stubs(:new).with(
      height: '73',
      width: '434',
      modifier: nil,
      orientation: nil
    ).returns(:correct)
    factory = Paperclip::GeometryParser.new("434x73")

    output = factory.make

    assert_equal :correct, output
  end

  it 'identifies an image and extract its dimensions with an empty orientation' do
    Paperclip::Geometry.stubs(:new).with(
      height: '73',
      width: '434',
      modifier: nil,
      orientation: ''
    ).returns(:correct)
    factory = Paperclip::GeometryParser.new("434x73,")

    output = factory.make

    assert_equal :correct, output
  end

  it 'identifies an image and extract its dimensions and orientation' do
    Paperclip::Geometry.stubs(:new).with(
      height: '200',
      width: '300',
      modifier: nil,
      orientation: '6'
    ).returns(:correct)
    factory = Paperclip::GeometryParser.new("300x200,6")

    output = factory.make

    assert_equal :correct, output
  end

  it 'identifies an image and extract its dimensions and modifier' do
    Paperclip::Geometry.stubs(:new).with(
      height: '64',
      width: '64',
      modifier: '#',
      orientation: nil
    ).returns(:correct)
    factory = Paperclip::GeometryParser.new("64x64#")

    output = factory.make

    assert_equal :correct, output
  end

  it 'identifies an image and extract its dimensions, orientation, and modifier' do
    Paperclip::Geometry.stubs(:new).with(
      height: '50',
      width: '100',
      modifier: '>',
      orientation: '7'
    ).returns(:correct)
    factory = Paperclip::GeometryParser.new("100x50,7>")

    output = factory.make

    assert_equal :correct, output
  end
end