File: test_encoding.rb

package info (click to toggle)
ruby-whitequark-parser 3.3.4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,828 kB
  • sloc: yacc: 40,699; ruby: 20,395; makefile: 12; sh: 8
file content (100 lines) | stat: -rw-r--r-- 2,526 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# encoding: binary
# frozen_string_literal: true

require 'helper'

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

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

  require 'parser/all'

  def test_default
    assert_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")
    assert_nil recognize("#!/bin/foo\n")
  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_nil recognize('')
  end

  def test_no_comment
    assert_nil recognize(%{require 'cane/encoding_aware_iterator'})
  end

  def test_adjacent
    assert_nil recognize('# codingkoi8-r')
    assert_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')

    e = assert_raises(ArgumentError) do
      assert_nil recognize('# coding: utf-8-dicks')
    end
    assert(e.is_a?(Parser::UnknownEncodingInMagicComment))
  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

  def test_ending_comment
    assert_nil recognize('foo # coding: koi8-r')
  end

  def test_wrong_prefix
    assert_nil recognize('# decoding: koi8-r')
  end

  def test_no_spaces
    assert_equal Encoding::KOI8_R, recognize('#encoding:koi8-r')
    assert_equal Encoding::KOI8_R, recognize('#coding:koi8-r')
  end

  def test_underscore_and_star_characters
    assert_equal Encoding::KOI8_R, recognize('# -*- encoding: koi8-r -*-')
  end

  def test_garbage_around_encoding_comment
    assert_equal Encoding::KOI8_R, recognize('# 1$# -*- &)* encoding: koi8-r 1$# -*- &)*')
  end
end