Sha256: 553cbc7bd9a043c63ec56f52222c157b41ca09200fc12157ef5bf0be740b9884

Contents?: true

Size: 1.95 KB

Versions: 68

Compression:

Stored size: 1.95 KB

Contents

# encoding: binary

require 'helper'

class TestEncoding < Minitest::Test
  include AST::Sexp

  def recognize(string)
    Parser::Source::Buffer.recognize_encoding(string)
  end

  if defined?(Encoding)
    require 'parser/all'

    def test_default
      assert_equal nil, recognize('foobar')
    end

    def test_bom
      assert_equal Encoding::UTF_8, recognize("\xef\xbb\xbf\nfoobar")
      assert_equal Encoding::UTF_8, recognize("\xef\xbb\xbf# coding:koi8-r\nfoobar")
    end

    def test_magic_comment
      assert_equal Encoding::KOI8_R, recognize("# coding:koi8-r\nfoobar")
    end

    def test_shebang
      assert_equal Encoding::KOI8_R, recognize("#!/bin/foo\n# coding:koi8-r\nfoobar")
    end

    def test_case
      assert_equal Encoding::KOI8_R, recognize("# coding:KoI8-r\nfoobar")
    end

    def test_space
      assert_equal Encoding::KOI8_R, recognize("# coding : koi8-r\nfoobar")
    end

    def test_empty
      assert_equal nil, recognize('')
    end

    def test_no_comment
      assert_equal nil, recognize(%{require 'cane/encoding_aware_iterator'})
    end

    def test_adjacent
      assert_equal nil, recognize('# codingkoi8-r')
      assert_equal nil, recognize('# coding koi8-r')
    end

    def test_utf8_mac
      assert_equal Encoding::UTF8_MAC, recognize('# coding: utf8-mac')
    end

    def test_suffix
      assert_equal Encoding::UTF_8, recognize('# coding: utf-8-dos')
      assert_equal Encoding::UTF_8, recognize('# coding: utf-8-unix')
      assert_equal Encoding::UTF_8, recognize('# coding: utf-8-mac')

      assert_raises(ArgumentError) do
        assert_equal nil,           recognize('# coding: utf-8-dicks')
      end
    end

    def test_parse_18_invalid_enc
      ast = Parser::Ruby18.parse("# encoding:feynman-diagram\n1")
      assert_equal ast, s(:int, 1)
    end

    def test_parse_19_invalid_enc
      assert_raises(ArgumentError) do
        Parser::Ruby19.parse("# encoding:feynman-diagram\n1")
      end
    end
  end
end

Version data entries

68 entries across 68 versions & 5 rubygems

Version Path
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/parser-2.4.0.0/test/test_encoding.rb
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/test/test_encoding.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/parser-2.4.0.0/test/test_encoding.rb
parser-2.4.0.0 test/test_encoding.rb
parser-2.3.3.1 test/test_encoding.rb
parser-2.3.3.0 test/test_encoding.rb
parser-2.3.2.0 test/test_encoding.rb
parser-2.3.1.4 test/test_encoding.rb
parser-2.3.1.3 test/test_encoding.rb
erruby_parser-2.3.1.2 test/test_encoding.rb
parser-2.3.1.2 test/test_encoding.rb
parser-2.3.1.1 test/test_encoding.rb
parser-2.3.1.0 test/test_encoding.rb
parser-2.3.0.7 test/test_encoding.rb
parser-2.3.0.6 test/test_encoding.rb
parser-2.3.0.5 test/test_encoding.rb
parser-2.3.0.4 test/test_encoding.rb
parser-2.3.0.3 test/test_encoding.rb
parser-2.3.0.2 test/test_encoding.rb
parser-2.3.0.1 test/test_encoding.rb