Back to Repositories

Testing URL Path Processing and Filter Operations in DevDocs

This test suite validates the core filtering functionality in DevDocs, focusing on URL handling, path manipulation, and page type detection. The tests ensure robust URL processing and path management for documentation pages.

Test Coverage Overview

The test suite provides comprehensive coverage of the Docs::Filter class functionality:

  • URL path manipulation and subpath extraction
  • Slug generation from various URL patterns
  • Page type detection (root, initial pages)
  • URL string validation and classification

Implementation Analysis

Tests utilize RSpec-style assertions with Minitest, implementing stub methods for isolation. The approach follows behavior-driven development patterns, with focused test cases for each method. Extensive use of context setup and helper methods demonstrates clean test organization.

Technical Details

  • Testing Framework: Minitest
  • Mocking Library: Built-in stub functionality
  • Test Helper Integration: FilterTestHelper module
  • Context Management: Pre-test setup using before blocks

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with proper setup and teardown
  • Comprehensive edge case coverage
  • Clear test naming conventions
  • Efficient test organization with nested describes
  • DRY principles through helper methods

freecodecamp/devdocs

test/lib/docs/core/filter_test.rb

            
require_relative '../../../test_helper'
require_relative '../../../../lib/docs'

class DocsFilterTest < Minitest::Spec
  include FilterTestHelper
  self.filter_class = Docs::Filter

  before do
    context[:base_url] = 'http://example.com/path'
    context[:url] = 'http://example.com/path/file'
  end

  describe "#subpath" do
    it "returns the #subpath_to the #current_url" do
      stub(filter).subpath_to(filter.current_url) { 'subpath' }
      assert_equal 'subpath', filter.subpath
    end
  end

  describe "#subpath_to" do
    it "returns the subpath from the #base_url to the url, ignoring case" do
      stub(filter.base_url).subpath_to('url', ignore_case: true) { 'subpath' }
      assert_equal 'subpath', filter.subpath_to('url')
    end
  end

  describe "#slug" do
    def slug(subpath)
      stub(filter).subpath { subpath }
      filter.slug
    end

    it "returns '' when #subpath is blank" do
      assert_equal '', slug('')
    end

    it "returns '' when #subpath is '/'" do
      assert_equal '', slug('/')
    end

    it "returns 'path' when #subpath is '/path'" do
      assert_equal 'path', slug('/path')
    end

    it "returns 'path' when #subpath is '/path.html'" do
      assert_equal 'path', slug('/path.html')
    end

    it "returns 'if..else' when #subpath is '/if..else'" do
      assert_equal 'if..else', slug('/if..else')
    end

    it "returns 'dir/path' when #subpath is '/dir/path'" do
      assert_equal 'dir/path', slug('/dir/path')
    end
  end

  describe "#root_page?" do
    it "returns true when #subpath is blank" do
      stub(filter).subpath { '' }
      assert filter.root_page?
    end

    it "returns true when #subpath is '/'" do
      stub(filter).subpath { '/' }
      assert filter.root_page?
    end

    it "returns true when #subpath is the root path" do
      context[:root_path] = '/path'
      stub(filter).subpath { '/path' }
      assert filter.root_page?
    end

    it "returns false when #subpath isn't the root path" do
      stub(filter).subpath { '/path' }
      refute filter.root_page?
    end
  end

  describe "#initial_page?" do
    before do
      context[:initial_paths] = ['initial']
      stub(filter).root_page? { false }
    end

    it "returns true when #root_page? is true" do
      stub(filter).root_page? { true }
      assert filter.initial_page?
    end

    it "returns true when #subpath is included in :initial_paths" do
      stub(filter).subpath { 'initial' }
      assert filter.initial_page?
    end

    it "returns false otherwise" do
      refute filter.initial_page?
    end
  end

  describe "#fragment_url_string?" do
    it "returns false with ''" do
      refute filter.fragment_url_string?('')
    end

    it "returns true with '#'" do
      assert filter.fragment_url_string?('#')
    end

    it "returns false with '/#'" do
      refute filter.fragment_url_string?('/#')
    end

    it "returns false with 'http://example.com/#'" do
      refute filter.fragment_url_string?('http://example.com/#')
    end
  end

  describe "#relative_url_string?" do
    it "returns true with ''" do
      assert filter.relative_url_string?('')
    end

    it "returns true with 'http'" do
      assert filter.relative_url_string?('http')
    end

    it "returns true with '/file'" do
      assert filter.relative_url_string?('/file')
    end

    it "returns true with '?file'" do
      assert filter.relative_url_string?('?file')
    end

    it "returns false with '#file'" do
      refute filter.relative_url_string?('#file')
    end

    it "returns false with 'http://example.com'" do
      refute filter.relative_url_string?('http://example.com')
    end

    it "returns false with 'ftp://example.com'" do
      refute filter.relative_url_string?('ftp://example.com')
    end

    it "returns false with 'mailto:[email protected]'" do
      refute filter.relative_url_string?('mailto:[email protected]')
    end

    it "returns false with 'data:image/gif;base64,foo'" do
      refute filter.relative_url_string?('data:image/gif;base64,foo')
    end
  end

  describe "#absolute_url_string?" do
    it "returns true with 'http://example.com'" do
      assert filter.absolute_url_string?('http://example.com')
    end

    it "returns true with 'ftp://example.com'" do
      assert filter.absolute_url_string?('ftp://example.com')
    end

    it "returns true with 'mailto:[email protected]'" do
      assert filter.absolute_url_string?('mailto:[email protected]')
    end

    it "returns false with ''" do
      refute filter.absolute_url_string?('')
    end

    it "returns false with 'http'" do
      refute filter.absolute_url_string?('http')
    end
  end
end