Back to Repositories

Testing Tabber Indentation Management Implementation in synx

This test suite validates the Synx::Tabber functionality, focusing on indentation management and output formatting capabilities. The tests verify tabbing behavior, output handling, and color formatting options in the Synx library.

Test Coverage Overview

The test suite provides comprehensive coverage of the Synx::Tabber class functionality.

Key areas tested include:
  • Incremental and decremental tabbing operations
  • Custom indentation level management
  • Output formatting with proper indentation
  • Color output handling
  • Quiet mode functionality

Implementation Analysis

The testing approach utilizes RSpec’s behavior-driven development patterns with isolated unit tests. Each method is tested independently using before hooks and let statements for setup.

Technical implementation includes:
  • StringIO for output capture and verification
  • Mock expectations for stdout testing
  • Reset functionality between test runs
  • Nested describe blocks for logical grouping

Technical Details

Testing infrastructure includes:
  • RSpec as the testing framework
  • StringIO for output manipulation
  • Color string manipulation for terminal output
  • Mock objects for stdout verification
  • Spec helper integration for common setup

Best Practices Demonstrated

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

Notable practices include:
  • Proper test isolation using before hooks
  • Descriptive context naming
  • Edge case coverage
  • Consistent assertion patterns
  • Clean setup and teardown management

venmo/synx

spec/synx/tabber_spec.rb

            
require 'spec_helper'

describe Synx::Tabber do

  before(:each) do
    Synx::Tabber.reset
    Synx::Tabber.options[:output] = output
  end

  let(:output) { StringIO.new }

  describe "::increase" do
    it "should default to increasing tabbing by 1" do
      Synx::Tabber.increase
      expect(Synx::Tabber.current).to eq 1
      Synx::Tabber.increase
      expect(Synx::Tabber.current).to eq 2
    end

    it "should indent by the amount passed in" do
      Synx::Tabber.increase(3)
      expect(Synx::Tabber.current).to eq 3
      Synx::Tabber.increase(5)
      expect(Synx::Tabber.current).to eq 8
    end

  end

  describe "::decrease" do
    it "should default to decreasing tabbing by 1" do
      Synx::Tabber.increase
      Synx::Tabber.increase
      expect(Synx::Tabber.current).to eq 2
      Synx::Tabber.decrease
      expect(Synx::Tabber.current).to eq 1
    end

    it "should not do any more decreasing past 0 tabbing" do
      Synx::Tabber.increase
      Synx::Tabber.increase
      expect(Synx::Tabber.current).to eq 2
      Synx::Tabber.decrease
      expect(Synx::Tabber.current).to eq 1
      Synx::Tabber.decrease
      expect(Synx::Tabber.current).to eq 0
      Synx::Tabber.decrease
      expect(Synx::Tabber.current).to eq 0
      Synx::Tabber.increase
      expect(Synx::Tabber.current).to eq 1
    end
  end

  describe "::puts" do
    it "should print to the output io with the appropriate indentation" do
      Synx::Tabber.increase(3)
      Synx::Tabber.puts("Hello, world.")
      expect(output.string).to eq("      Hello, world.
")
    end

    it "should not print anything if quiet is true" do
      Synx::Tabber.options[:quiet] = true
      Synx::Tabber.puts("Hello, world.")
      expect(output.string).to eq("")
    end

    it "should print colors if no_color is false or not present" do
      Synx::Tabber.puts("Hello, world.".red)
      expect(output.string).to eq("\e[0;31;49mHello, world.\e[0m
")
    end

    it "should not print colors if no_color is true" do
      Synx::Tabber.options[:no_color] = true
      Synx::Tabber.puts("Hello, world.".red)
      expect(output.string).to eq("Hello, world.
")
    end

    it "prints to stdout if no output is specified" do
      expect($stdout).to receive(:puts).with("  Hello, world.")
      Synx::Tabber.reset
      Synx::Tabber.increase
      Synx::Tabber.puts("Hello, world.")
    end
  end

  describe "::a_single_tab" do
    it "should be two spaces" do
      expect(Synx::Tabber.send(:a_single_tab)).to eq "  "
    end
  end

end