File: string.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (51 lines) | stat: -rw-r--r-- 1,771 bytes parent folder | download | duplicates (9)
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
describe :string_unpack_string, shared: true do
  it "returns an empty string if the input is empty" do
    "".unpack(unpack_format).should == [""]
  end

  it "returns empty strings for repeated formats if the input is empty" do
    "".unpack(unpack_format(nil, 3)).should == ["", "", ""]
  end

  it "returns an empty string and does not decode any bytes when the count modifier is zero" do
    "abc".unpack(unpack_format(0)+unpack_format).should == ["", "a"]
  end

  it "implicitly has a count of one when no count is specified" do
    "abc".unpack(unpack_format).should == ["a"]
  end

  it "decodes the number of bytes specified by the count modifier" do
    "abc".unpack(unpack_format(3)).should == ["abc"]
  end

  it "decodes the number of bytes specified by the count modifier including whitespace bytes" do
    [ ["a bc",  ["a b", "c"]],
      ["a\fbc", ["a\fb", "c"]],
      ["a\nbc", ["a\nb", "c"]],
      ["a\rbc", ["a\rb", "c"]],
      ["a\tbc", ["a\tb", "c"]],
      ["a\vbc", ["a\vb", "c"]]
    ].should be_computed_by(:unpack, unpack_format(3)+unpack_format)
  end

  it "decodes past whitespace bytes when passed the '*' modifier" do
    [ ["a b c",    ["a b c"]],
      ["a\fb c",   ["a\fb c"]],
      ["a\nb c",   ["a\nb c"]],
      ["a\rb c",   ["a\rb c"]],
      ["a\tb c",   ["a\tb c"]],
      ["a\vb c",   ["a\vb c"]],
    ].should be_computed_by(:unpack, unpack_format("*"))
  end
end

describe :string_unpack_Aa, shared: true do
  it "decodes the number of bytes specified by the count modifier including NULL bytes" do
    "a\x00bc".unpack(unpack_format(3)+unpack_format).should == ["a\x00b", "c"]
  end

  it "decodes past NULL bytes when passed the '*' modifier" do
    "a\x00b c".unpack(unpack_format("*")).should == ["a\x00b c"]
  end
end