Back to Repositories

Testing CSS Padding Rule Generation in Bourbon

This test suite validates the padding functionality in the Bourbon Sass library. It ensures proper CSS padding rule generation for various input combinations and edge cases, verifying the core padding mixins behave correctly.

Test Coverage Overview

The test suite provides comprehensive coverage of Bourbon’s padding mixin functionality.

Key areas tested include:
  • Single value padding application
  • Two-value alternating padding rules
  • Three-value padding with implied sides
  • Four-value explicit padding definitions
  • Handling of null padding values

Implementation Analysis

The tests utilize RSpec’s behavior-driven development approach with context-based organization. Each padding scenario is isolated in its own context block, using expect() assertions to verify CSS rule generation. The implementation leverages custom matchers (have_rule and have_ruleset) for CSS rule validation.

Technical patterns include:
  • Before block setup for parser initialization
  • Contextual test organization
  • Custom matcher usage for CSS validation

Technical Details

Testing infrastructure includes:
  • RSpec testing framework
  • Custom ParserSupport utility
  • CSS rule parsing and validation helpers
  • Bourbon library integration
  • Specialized matchers for CSS rule verification

Best Practices Demonstrated

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

Notable practices include:
  • Descriptive context and example naming
  • Isolated test cases for each padding scenario
  • Proper setup and initialization handling
  • Clear separation of concerns in test organization
  • Effective use of custom matchers for readable assertions

thoughtbot/bourbon

spec/bourbon/library/padding_spec.rb

            
require "spec_helper"

describe "padding" do
  before(:all) do
    ParserSupport.parse_file("library/padding")
  end

  context "called with one size" do
    it "applies same width to all sides" do
      rule = "padding: 1px"

      expect(".padding-all").to have_rule(rule)
    end
  end

  context "called with two sizes" do
    it "applies to alternating sides" do
      rule = "padding: 2px 3px"

      expect(".padding-alternate").to have_rule(rule)
    end
  end

  context "called with three sizes" do
    it "applies second width to left and right" do
      rule = "padding: 4px 5px 6px"

      expect(".padding-implied-left").to have_rule(rule)
    end
  end

  context "called with four sizes" do
    it "applies different widths to all sides" do
      rule = "padding: 7px 8px 9px 10px"

      expect(".padding-explicit").to have_rule(rule)
    end
  end

  context "called with null values" do
    it "writes rules for other three" do
      ruleset = "padding-top: 11px; " +
                "padding-right: 12px; " +
                "padding-left: 13px;"
      bad_rule = "padding-bottom: null;"

      expect(".padding-false-third").to have_ruleset(ruleset)
      expect(".padding-false-third").to_not have_rule(bad_rule)
    end
  end
end