File: codepoints.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 (44 lines) | stat: -rw-r--r-- 1,142 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
# -*- encoding: utf-8 -*-
require File.expand_path('../../fixtures/classes', __FILE__)

describe :io_codepoints, :shared => true do
  before(:each) do
    @io = IOSpecs.io_fixture "lines.txt"
    @enum = @io.send(@method)
  end

  after(:each) do
    @io.close
  end

  it "returns an Enumerator when passed no block" do
    @enum.should be_an_instance_of(enumerator_class)
  end

  it "yields each codepoint" do
    @enum.first(25).should == [
      86, 111, 105, 99, 105, 32, 108, 97, 32, 108, 105, 103, 110,
      101, 32, 117, 110, 101, 46, 10, 81, 117, 105, 32, 232
    ]
  end

  it "yields each codepoint starting from the current position" do
    @io.pos = 130
    @enum.to_a.should == [101, 32, 115, 105, 120, 46, 10]
  end

  it "raises an error if reading invalid sequence" do
    @io.pos = 60  # inside of a multibyte sequence
    lambda { @enum.first }.should raise_error(ArgumentError)
  end

  it "does not change $_" do
    $_ = "test"
    @enum.to_a
    $_.should == "test"
  end

  it "raises an IOError when self is not readable" do
    lambda { IOSpecs.closed_io.send(@method).to_a }.should raise_error(IOError)
  end
end