summaryrefslogtreecommitdiff
path: root/spec/ruby/library/stringscanner/scan_byte_spec.rb
blob: c60e22be4f508ca04071aa04c86d581dbc968bd1 (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
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
101
102
require_relative '../../spec_helper'
require 'strscan'

version_is StringScanner::Version, "3.1.1" do # ruby_version_is "3.4"
  describe "StringScanner#scan_byte" do
    it "scans one byte and returns it as on Integer" do
      s = StringScanner.new('abc') # "abc".bytes => [97, 98, 99]
      s.scan_byte.should == 97
      s.scan_byte.should == 98
      s.scan_byte.should == 99
    end

    it "is not multi-byte character sensitive" do
      s = StringScanner.new("あ") # "あ".bytes => [227, 129, 130]
      s.scan_byte.should == 227
      s.scan_byte.should == 129
      s.scan_byte.should == 130
    end

    it "returns nil at the end of the string" do
      s = StringScanner.new('a')
      s.scan_byte # skip one
      s.scan_byte.should == nil
      s.pos.should == 1
    end

    it "changes current position" do
      s = StringScanner.new('abc')
      s.pos.should == 0
      s.scan_byte
      s.pos.should == 1
    end

    it "sets the last match result" do
      s = StringScanner.new('abc')
      s.pos = 1
      s.scan_byte

      s.pre_match.should == "a"
      s.matched.should == "b"
      s.post_match.should == "c"
    end

    describe "#[] successive call with a capture group name" do
      # https://github.com/ruby/strscan/issues/139
      ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes
      version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
        it "returns nil" do
          s = StringScanner.new("abc")
          s.scan_byte
          s.should.matched?
          s[:a].should be_nil
        end
      end
      end
      version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.3"
        it "raises IndexError" do
          s = StringScanner.new("abc")
          s.scan_byte
          s.should.matched?
          -> { s[:a] }.should raise_error(IndexError)
        end
      end

      it "returns a matching character when given Integer index" do
        s = StringScanner.new("This is a test")
        s.scan_byte
        s[0].should == "T"
      end

      # https://github.com/ruby/strscan/issues/135
      ruby_version_is ""..."3.5" do # Don't run on 3.5.0dev that already contains not released fixes
      version_is StringScanner::Version, "3.1.1"..."3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
        it "ignores the previous matching with Regexp" do
          s = StringScanner.new("abc")

          s.exist?(/(?<a>a)/)
          s.should.matched?
          s[:a].should == "a"

          s.scan_byte
          s.should.matched?
          s[:a].should == nil
        end
      end
      end
      version_is StringScanner::Version, "3.1.3" do # ruby_version_is "3.4.0"..."3.4.3"
        it "ignores the previous matching with Regexp" do
          s = StringScanner.new("abc")

          s.exist?(/(?<a>a)/)
          s.should.matched?
          s[:a].should == "a"

          s.scan_byte
          s.should.matched?
          -> { s[:a] }.should raise_error(IndexError)
        end
      end
    end
  end
end