Commit 1b5d7a02 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add tests for JsDuck::Logger.

Make the Logger testable by defining a LoggerCls and instantiating
it as global Logger - allowing us to instantiate the LoggerCls
separately in tests.

Test the enabling/disabling of warnings.
parent c1f0bf81
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
require 'jsduck/util/singleton'
require 'jsduck/util/os'

module JsDuck

  # Central logging of JsDuck
  class Logger
    include Util::Singleton

  class LoggerCls
    # Set to true to enable verbose logging
    attr_accessor :verbose

@@ -215,4 +212,7 @@ module JsDuck
    end
  end

  # The actual global Logger instance
  Logger = LoggerCls.new

end

spec/logger_spec.rb

0 → 100644
+80 −0
Original line number Diff line number Diff line
require "jsduck/logger"

describe JsDuck::Logger do
  let(:logger) do
    JsDuck::LoggerCls.new
  end

  let(:usual_warnings) do
    [:tag, :global, :link]
  end

  describe "by default" do
    it "has the nil warning enabled" do
      logger.warning_enabled?(nil, "").should == true
    end

    it "has the usual warnings disabled" do
      usual_warnings.each do |type|
        logger.warning_enabled?(type, "").should == false
      end
    end
  end

  describe "after enabling all warnings" do
    before do
      logger.set_warning(:all, true)
    end

    it "has the usual warnings disabled" do
      usual_warnings.each do |type|
        logger.warning_enabled?(type, "").should == true
      end
    end
  end

  shared_examples_for "limited to a path" do
    it "has the :tag warning disabled for /other/path/file.js" do
      logger.warning_enabled?(:tag, "/other/path/file.js").should == false
    end

    it "has the :tag warning enabled for /some/path/file.js" do
      logger.warning_enabled?(:tag, "/some/path/file.js").should == true
    end

    it "has the :tag warning enabled for /within/some/path/file.js" do
      logger.warning_enabled?(:tag, "/within/some/path/file.js").should == true
    end
  end

  describe "after enabling all warnings in /some/path" do
    before do
      logger.set_warning(:all, true, "/some/path")
    end

    it_should_behave_like "limited to a path"
  end

  describe "after enabling :tag warning in /some/path" do
    before do
      logger.set_warning(:tag, true, "/some/path")
    end

    it_should_behave_like "limited to a path"

    describe "and also enabling it in /other/path" do
      before do
        logger.set_warning(:tag, true, "/other/path")
      end

      it "has the :tag warning enabled for /some/path/file.js" do
        logger.warning_enabled?(:tag, "/some/path/file.js").should == true
      end

      it "has the :tag warning enabled for /other/path/file.js" do
        logger.warning_enabled?(:tag, "/other/path/file.js").should == true
      end
    end
  end

end