File: force_encoding_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (48 lines) | stat: -rw-r--r-- 1,499 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
require File.expand_path('../../../spec_helper', __FILE__)

with_feature :encoding do
  describe "String#force_encoding" do
    it "accepts a String as the name of an Encoding" do
      "abc".force_encoding('shift_jis').encoding.should == Encoding::Shift_JIS
    end

    it "accepts an Encoding instance" do
      "abc".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::Shift_JIS
    end

    it "calls #to_str to convert an object to an encoding name" do
      obj = mock("force_encoding")
      obj.should_receive(:to_str).and_return("utf-8")

      "abc".force_encoding(obj).encoding.should == Encoding::UTF_8
    end

    it "raises a TypeError if #to_str does not return a String" do
      obj = mock("force_encoding")
      obj.should_receive(:to_str).and_return(1)

      lambda { "abc".force_encoding(obj) }.should raise_error(TypeError)
    end

    it "raises a TypeError if passed nil" do
      lambda { "abc".force_encoding(nil) }.should raise_error(TypeError)
    end

    it "returns self" do
      str = "abc"
      str.force_encoding('utf-8').should equal(str)
    end

    it "sets the encoding even if the String contents are invalid in that encoding" do
      str = "\u{9765}"
      str.force_encoding('euc-jp')
      str.encoding.should == Encoding::EUC_JP
      str.valid_encoding?.should be_false
    end

    it "does not transcode self" do
      str = "\u{8612}"
      str.dup.force_encoding('utf-16le').should_not == str.encode('utf-16le')
    end
  end
end