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
parser-2.2.0 test/test_encoding.rb
parser-2.2.0.pre.8 test/test_encoding.rb
parser-2.2.0.pre.7 test/test_encoding.rb
parser-2.2.0.pre.6 test/test_encoding.rb
parser-2.2.0.pre.5 test/test_encoding.rb
parser-2.2.0.pre.4 test/test_encoding.rb
parser-2.2.0.pre.3 test/test_encoding.rb
parser-2.2.0.pre.2 test/test_encoding.rb
parser-2.2.0.pre.1 test/test_encoding.rb
parser-2.1.9 test/test_encoding.rb
parser-2.1.8 test/test_encoding.rb
parser-2.1.7 test/test_encoding.rb
parser-2.1.6 test/test_encoding.rb
parser-2.1.5 test/test_encoding.rb
parser-2.1.4 test/test_encoding.rb
parser-2.1.3 test/test_encoding.rb
parser-2.1.2 test/test_encoding.rb
parser-2.1.1 test/test_encoding.rb
parser-2.1.0 test/test_encoding.rb
parser-2.1.0.pre2 test/test_encoding.rb