"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/logger_test.rb" (1 Jun 2019, 7229 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require_relative 'test_helper'
    3 
    4 context 'Logger' do
    5   MyLogger = Class.new Logger
    6 
    7   context 'LoggerManager' do
    8     test 'provides access to logger via static logger method' do
    9       logger = Asciidoctor::LoggerManager.logger
   10       refute_nil logger
   11       assert_kind_of Logger, logger
   12     end
   13 
   14     test 'allows logger instance to be changed' do
   15       old_logger = Asciidoctor::LoggerManager.logger
   16       new_logger = MyLogger.new $stdout
   17       begin
   18         Asciidoctor::LoggerManager.logger = new_logger
   19         assert_same new_logger, Asciidoctor::LoggerManager.logger
   20       ensure
   21         Asciidoctor::LoggerManager.logger = old_logger
   22       end
   23     end
   24 
   25     test 'setting logger instance to falsy value resets instance to default logger' do
   26       old_logger = Asciidoctor::LoggerManager.logger
   27       begin
   28         Asciidoctor::LoggerManager.logger = MyLogger.new $stdout
   29         Asciidoctor::LoggerManager.logger = nil
   30         refute_nil Asciidoctor::LoggerManager.logger
   31         assert_kind_of Logger, Asciidoctor::LoggerManager.logger
   32       ensure
   33         Asciidoctor::LoggerManager.logger = old_logger
   34       end
   35     end
   36 
   37     test 'creates logger instance from static logger_class property' do
   38       old_logger_class = Asciidoctor::LoggerManager.logger_class
   39       old_logger = Asciidoctor::LoggerManager.logger
   40       begin
   41         Asciidoctor::LoggerManager.logger_class = MyLogger
   42         Asciidoctor::LoggerManager.logger = nil
   43         refute_nil Asciidoctor::LoggerManager.logger
   44         assert_kind_of MyLogger, Asciidoctor::LoggerManager.logger
   45       ensure
   46         Asciidoctor::LoggerManager.logger_class = old_logger_class
   47         Asciidoctor::LoggerManager.logger = old_logger
   48       end
   49     end
   50   end
   51 
   52   context 'Logger' do
   53     test 'configures default logger with progname set to asciidoctor' do
   54       assert_equal 'asciidoctor', Asciidoctor::LoggerManager.logger.progname
   55     end
   56 
   57     test 'configures default logger with level set to WARN' do
   58       assert_equal Logger::Severity::WARN, Asciidoctor::LoggerManager.logger.level
   59     end
   60 
   61     test 'configures default logger to write messages to $stderr' do
   62       out_string, err_string = redirect_streams do |out, err|
   63         Asciidoctor::LoggerManager.logger.warn 'this is a call'
   64         [out.string, err.string]
   65       end
   66       assert_empty out_string
   67       refute_empty err_string
   68       assert_includes err_string, 'this is a call'
   69     end
   70 
   71     test 'configures default logger to use a formatter that matches traditional format' do
   72       err_string = redirect_streams do |_, err|
   73         Asciidoctor::LoggerManager.logger.warn 'this is a call'
   74         Asciidoctor::LoggerManager.logger.fatal 'it cannot be done'
   75         err.string
   76       end
   77       assert_includes err_string, %(asciidoctor: WARNING: this is a call)
   78       assert_includes err_string, %(asciidoctor: FAILED: it cannot be done)
   79     end
   80 
   81     test 'NullLogger level is not nil' do
   82       logger = Asciidoctor::NullLogger.new
   83       refute_nil logger.level
   84       assert_equal Logger::WARN, logger.level
   85     end
   86   end
   87 
   88   context ':logger API option' do
   89     test 'should be able to set logger when invoking load API' do
   90       old_logger = Asciidoctor::LoggerManager.logger
   91       new_logger = MyLogger.new $stdout
   92       begin
   93         Asciidoctor.load 'contents', logger: new_logger
   94         assert_same new_logger, Asciidoctor::LoggerManager.logger
   95       ensure
   96         Asciidoctor::LoggerManager.logger = old_logger
   97       end
   98     end
   99 
  100     test 'should be able to set logger when invoking load_file API' do
  101       old_logger = Asciidoctor::LoggerManager.logger
  102       new_logger = MyLogger.new $stdout
  103       begin
  104         Asciidoctor.load_file fixture_path('basic.adoc'), logger: new_logger
  105         assert_same new_logger, Asciidoctor::LoggerManager.logger
  106       ensure
  107         Asciidoctor::LoggerManager.logger = old_logger
  108       end
  109     end
  110 
  111     test 'should be able to set logger when invoking convert API' do
  112       old_logger = Asciidoctor::LoggerManager.logger
  113       new_logger = MyLogger.new $stdout
  114       begin
  115         Asciidoctor.convert 'contents', logger: new_logger
  116         assert_same new_logger, Asciidoctor::LoggerManager.logger
  117       ensure
  118         Asciidoctor::LoggerManager.logger = old_logger
  119       end
  120     end
  121 
  122     test 'should be able to set logger when invoking convert_file API' do
  123       old_logger = Asciidoctor::LoggerManager.logger
  124       new_logger = MyLogger.new $stdout
  125       begin
  126         Asciidoctor.convert_file fixture_path('basic.adoc'), to_file: false, logger: new_logger
  127         assert_same new_logger, Asciidoctor::LoggerManager.logger
  128       ensure
  129         Asciidoctor::LoggerManager.logger = old_logger
  130       end
  131     end
  132   end
  133 
  134   context 'Logging' do
  135     test 'including Logging gives instance methods on module access to logging infrastructure' do
  136       module SampleModuleA
  137         include Asciidoctor::Logging
  138         def get_logger
  139           logger
  140         end
  141       end
  142 
  143       class SampleClassA
  144         include SampleModuleA
  145       end
  146       assert_same Asciidoctor::LoggerManager.logger, SampleClassA.new.get_logger
  147       assert SampleClassA.public_method_defined? :logger
  148     end
  149 
  150     test 'including Logging gives static methods on module access to logging infrastructure' do
  151       module SampleModuleB
  152         include Asciidoctor::Logging
  153         def self.get_logger
  154           logger
  155         end
  156       end
  157 
  158       assert_same Asciidoctor::LoggerManager.logger, SampleModuleB.get_logger
  159     end
  160 
  161     test 'including Logging gives instance methods on class access to logging infrastructure' do
  162       class SampleClassC
  163         include Asciidoctor::Logging
  164         def get_logger
  165           logger
  166         end
  167       end
  168 
  169       assert_same Asciidoctor::LoggerManager.logger, SampleClassC.new.get_logger
  170       assert SampleClassC.public_method_defined? :logger
  171     end
  172 
  173     test 'including Logging gives static methods on class access to logging infrastructure' do
  174       class SampleClassD
  175         include Asciidoctor::Logging
  176         def self.get_logger
  177           logger
  178         end
  179       end
  180 
  181       assert_same Asciidoctor::LoggerManager.logger, SampleClassD.get_logger
  182     end
  183 
  184     test 'can create an auto-formatting message with context' do
  185       class SampleClassE
  186         include Asciidoctor::Logging
  187         def create_message cursor
  188           message_with_context 'Asciidoctor was here', source_location: cursor
  189         end
  190       end
  191 
  192       cursor = Asciidoctor::Reader::Cursor.new 'file.adoc', fixturedir, 'file.adoc', 5
  193       message = SampleClassE.new.create_message cursor
  194       assert_equal 'Asciidoctor was here', message[:text]
  195       assert_same cursor, message[:source_location]
  196       assert_equal 'file.adoc: line 5: Asciidoctor was here', message.inspect
  197     end
  198 
  199     test 'writes message prefixed with program name and source location to stderr' do
  200       input = <<~'EOS'
  201       [#first]
  202       first paragraph
  203 
  204       [#first]
  205       another first paragraph
  206       EOS
  207       messages = redirect_streams do |_, err|
  208         convert_string_to_embedded input
  209         err.string.chomp
  210       end
  211       assert_equal 'asciidoctor: WARNING: <stdin>: line 5: id assigned to block already in use: first', messages
  212     end
  213   end
  214 end