summaryrefslogtreecommitdiff
path: root/spec/ruby/core/regexp/encoding_spec.rb
blob: 6ce6f8b3caa8ea778f58b90f48beb83ad1e79ea4 (plain)
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
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'

describe "Regexp#encoding" do
  it "returns an Encoding object" do
    /glar/.encoding.should be_an_instance_of(Encoding)
  end

  it "defaults to US-ASCII if the Regexp contains only US-ASCII character" do
    /ASCII/.encoding.should == Encoding::US_ASCII
  end

  it "returns US_ASCII if the 'n' modifier is supplied and only US-ASCII characters are present" do
    /ASCII/n.encoding.should == Encoding::US_ASCII
  end

  it "returns BINARY if the 'n' modifier is supplied and non-US-ASCII characters are present" do
    /\xc2\xa1/n.encoding.should == Encoding::BINARY
  end

  it "defaults to UTF-8 if \\u escapes appear" do
    /\u{9879}/.encoding.should == Encoding::UTF_8
  end

  it "defaults to UTF-8 if a literal UTF-8 character appears" do
    /¥/.encoding.should == Encoding::UTF_8
  end

  it "returns UTF-8 if the 'u' modifier is supplied" do
    /ASCII/u.encoding.should == Encoding::UTF_8
  end

  it "returns Windows-31J if the 's' modifier is supplied" do
    /ASCII/s.encoding.should == Encoding::Windows_31J
  end

  it "returns EUC_JP if the 'e' modifier is supplied" do
    /ASCII/e.encoding.should == Encoding::EUC_JP
  end

  it "upgrades the encoding to that of an embedded String" do
    str = "文字化け".encode('euc-jp')
    /#{str}/.encoding.should == Encoding::EUC_JP
  end

  it "ignores the encoding and uses US-ASCII if the string has only ASCII characters" do
    str = "abc".encode('euc-jp')
    str.encoding.should == Encoding::EUC_JP
    /#{str}/.encoding.should == Encoding::US_ASCII
  end

  it "ignores the default_internal encoding" do
    old_internal = Encoding.default_internal
    Encoding.default_internal = Encoding::EUC_JP
    /foo/.encoding.should_not == Encoding::EUC_JP
    Encoding.default_internal = old_internal
  end
end