"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/test/helpers_test.rb" (1 Jun 2019, 4884 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. See also the last Fossies "Diffs" side-by-side code changes report for "helpers_test.rb": 2.0.5_vs_2.0.6.

    1 # frozen_string_literal: true
    2 require_relative 'test_helper'
    3 
    4 context 'Helpers' do
    5   context 'URI Encoding' do
    6     test 'should URI encode non-word characters generally' do
    7       given = ' !*/%&?\\='
    8       expect = '+%21%2A%2F%25%26%3F%5C%3D'
    9       assert_equal expect, (Asciidoctor::Helpers.encode_uri_component given)
   10     end
   11 
   12     test 'should not URI encode select non-word characters' do
   13       # NOTE Ruby 2.5 and up stopped encoding ~
   14       given = '-.'
   15       expect = given
   16       assert_equal expect, (Asciidoctor::Helpers.encode_uri_component given)
   17     end
   18   end
   19 
   20   context 'URIs and Paths' do
   21     test 'rootname should return file name without extension' do
   22       assert_equal 'master', Asciidoctor::Helpers.rootname('master.adoc')
   23       assert_equal 'docs/master', Asciidoctor::Helpers.rootname('docs/master.adoc')
   24     end
   25 
   26     test 'rootname should file name if it has no extension' do
   27       assert_equal 'master', Asciidoctor::Helpers.rootname('master')
   28       assert_equal 'docs/master', Asciidoctor::Helpers.rootname('docs/master')
   29     end
   30 
   31     test 'rootname should ignore dot not in last segment' do
   32       assert_equal 'include.d/master', Asciidoctor::Helpers.rootname('include.d/master')
   33       assert_equal 'include.d/master', Asciidoctor::Helpers.rootname('include.d/master.adoc')
   34     end
   35 
   36     test 'extname? should return whether path contains an extname' do
   37       assert Asciidoctor::Helpers.extname?('document.adoc')
   38       assert Asciidoctor::Helpers.extname?('path/to/document.adoc')
   39       assert_nil Asciidoctor::Helpers.extname?('basename')
   40       refute Asciidoctor::Helpers.extname?('include.d/basename')
   41     end
   42 
   43     test 'UriSniffRx should detect URIs' do
   44       assert Asciidoctor::UriSniffRx =~ 'http://example.com'
   45       assert Asciidoctor::UriSniffRx =~ 'https://example.com'
   46       assert Asciidoctor::UriSniffRx =~ 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs='
   47     end
   48 
   49     test 'UriSniffRx should not detect an absolute Windows path as a URI' do
   50       assert Asciidoctor::UriSniffRx !~ 'c:/sample.adoc'
   51       assert Asciidoctor::UriSniffRx !~ 'c:\\sample.adoc'
   52     end
   53   end
   54 
   55   context 'Type Resolution' do
   56     test 'should get class for top-level class name' do
   57       clazz = Asciidoctor::Helpers.class_for_name 'String'
   58       refute_nil clazz
   59       assert_equal String, clazz
   60     end
   61 
   62     test 'should get class for class name in module' do
   63       clazz = Asciidoctor::Helpers.class_for_name 'Asciidoctor::Document'
   64       refute_nil clazz
   65       assert_equal Asciidoctor::Document, clazz
   66     end
   67 
   68     test 'should get class for class name resolved from root' do
   69       clazz = Asciidoctor::Helpers.class_for_name '::Asciidoctor::Document'
   70       refute_nil clazz
   71       assert_equal Asciidoctor::Document, clazz
   72     end
   73 
   74     test 'should raise exception if cannot find class for name' do
   75       begin
   76         Asciidoctor::Helpers.class_for_name 'InvalidModule::InvalidClass'
   77         flunk 'Expecting RuntimeError to be raised'
   78       rescue NameError => e
   79         assert_equal 'Could not resolve class for name: InvalidModule::InvalidClass', e.message
   80       end
   81     end
   82 
   83     test 'should raise exception if constant name is invalid' do
   84       begin
   85         Asciidoctor::Helpers.class_for_name 'foobar'
   86         flunk 'Expecting RuntimeError to be raised'
   87       rescue NameError => e
   88         assert_equal 'Could not resolve class for name: foobar', e.message
   89       end
   90     end
   91 
   92     test 'should raise exception if class not found in scope' do
   93       begin
   94         Asciidoctor::Helpers.class_for_name 'Asciidoctor::Extensions::String'
   95         flunk 'Expecting RuntimeError to be raised'
   96       rescue NameError => e
   97         assert_equal 'Could not resolve class for name: Asciidoctor::Extensions::String', e.message
   98       end
   99     end
  100 
  101     test 'should raise exception if name resolves to module' do
  102       begin
  103         Asciidoctor::Helpers.class_for_name 'Asciidoctor::Extensions'
  104         flunk 'Expecting RuntimeError to be raised'
  105       rescue NameError => e
  106         assert_equal 'Could not resolve class for name: Asciidoctor::Extensions', e.message
  107       end
  108     end
  109 
  110     test 'should resolve class if class is given' do
  111       clazz = Asciidoctor::Helpers.resolve_class Asciidoctor::Document
  112       refute_nil clazz
  113       assert_equal Asciidoctor::Document, clazz
  114     end
  115 
  116     test 'should resolve class if class from string' do
  117       clazz = Asciidoctor::Helpers.resolve_class 'Asciidoctor::Document'
  118       refute_nil clazz
  119       assert_equal Asciidoctor::Document, clazz
  120     end
  121 
  122     test 'should not resolve class if not in scope' do
  123       begin
  124         Asciidoctor::Helpers.resolve_class 'Asciidoctor::Extensions::String'
  125         flunk 'Expecting RuntimeError to be raised'
  126       rescue NameError => e
  127         assert_equal 'Could not resolve class for name: Asciidoctor::Extensions::String', e.message
  128       end
  129     end
  130   end
  131 end