File: argf_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (123 lines) | stat: -rw-r--r-- 3,086 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require "../spec_helper"

describe IO::ARGF do
  it "reads from STDIN if ARGV isn't specified" do
    argv = [] of String
    stdin = IO::Memory.new("hello")

    argf = IO::ARGF.new argv, stdin
    argf.path.should eq("-")
    argf.gets_to_end.should eq("hello")
    argf.read_byte.should be_nil
  end

  it "reads from ARGV if specified" do
    path1 = datapath("argf_test_file_1.txt")
    path2 = datapath("argf_test_file_2.txt")
    stdin = IO::Memory.new("")
    argv = [path1, path2]

    argf = IO::ARGF.new argv, stdin
    argf.path.should eq(path1)
    argv.should eq([path1, path2])

    str = argf.gets(5)
    str.should eq("12345")

    argv.should eq([path2])

    str = argf.gets_to_end
    str.should eq("\n67890\n")

    argv.should be_empty

    argf.read_byte.should be_nil

    argv << path1
    str = argf.gets(5)
    str.should eq("12345")
  end

  it "reads when is more data left to read" do
    argf = IO::ARGF.new [datapath("argf_test_file_3.xml")], IO::Memory.new
    argf.read(Bytes.new(4))
    buf = Bytes.new(4096)
    z = argf.read(buf)
    z = argf.read(buf)
    z = argf.read(buf)
    z = argf.read(buf)
    z.should_not eq 0
    String.new(buf[0...z]).should_not be_empty
  end

  describe "gets" do
    it "reads from STDIN if ARGV isn't specified" do
      argv = [] of String
      stdin = IO::Memory.new("hello\nworld\n")

      argf = IO::ARGF.new argv, stdin
      argf.gets.should eq("hello")
      argf.gets.should eq("world")
      argf.gets.should be_nil
    end

    it "reads from STDIN if ARGV isn't specified, chomp = false" do
      argv = [] of String
      stdin = IO::Memory.new("hello\nworld\n")

      argf = IO::ARGF.new argv, stdin
      argf.gets(chomp: false).should eq("hello\n")
      argf.gets(chomp: false).should eq("world\n")
      argf.gets(chomp: false).should be_nil
    end

    it "reads from ARGV if specified" do
      path1 = datapath("argf_test_file_1.txt")
      path2 = datapath("argf_test_file_2.txt")
      stdin = IO::Memory.new("")
      argv = [path1, path2]

      argf = IO::ARGF.new argv, stdin
      argv.should eq([path1, path2])

      argf.gets(chomp: false).should eq("12345\n")
      argv.should eq([path2])

      argf.gets(chomp: false).should eq("67890\n")
      argv.should be_empty

      argf.gets(chomp: false).should be_nil

      argv << path1
      str = argf.gets(chomp: false)
      str.should eq("12345\n")
    end
  end

  describe "peek" do
    it "peeks from STDIN if ARGV isn't specified" do
      argv = [] of String
      stdin = IO::Memory.new("1234")

      argf = IO::ARGF.new argv, stdin
      argf.peek.should eq("1234".to_slice)

      argf.gets_to_end.should eq("1234")
    end

    it "peeks from ARGV if specified" do
      path1 = datapath("argf_test_file_1.txt")
      path2 = datapath("argf_test_file_2.txt")
      stdin = IO::Memory.new("")
      argv = [path1, path2]

      argf = IO::ARGF.new argv, stdin
      argf.peek.should eq("12345\n".to_slice)

      argf.read_string(6)
      argf.read_byte

      argf.peek.should eq("7890\n".to_slice)
    end
  end
end