summaryrefslogtreecommitdiff
path: root/lib/elixir/test/elixir/io_test.exs
blob: 7d46dbbf5638a0dff3470c0f5a10222641caea35 (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
103
104
105
106
107
108
109
Code.require_file "test_helper.exs", __DIR__

defmodule IOTest do
  use ExUnit.Case, async: true
  import ExUnit.CaptureIO

  test :read_with_count do
    {:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__), [:char_list])
    assert 'FOO' == IO.read(file, 3)
    assert File.close(file) == :ok
  end

  test :read_with_utf8_and_binary do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__), [:utf8])
    assert "Русский" == IO.read(file, 7)
    assert File.close(file) == :ok
  end

  test :binread do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__))
    assert "Русский" == IO.binread(file, 14)
    assert File.close(file) == :ok
  end

  test :getn do
    {:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__))
    assert "F" == IO.getn(file, "")
    assert "O" == IO.getn(file, "")
    assert "O" == IO.getn(file, "")
    assert "\n" == IO.getn(file, "")
    assert :eof == IO.getn(file, "")
    assert File.close(file) == :ok
  end

  test :getn_with_count do
    {:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__), [:char_list])
    assert 'FOO' == IO.getn(file, "", 3)
    assert File.close(file) == :ok
  end

  test :getn_with_utf8_and_binary do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__), [:utf8])
    assert "Русский" == IO.getn(file, "", 7)
    assert File.close(file) == :ok
  end

  test :gets do
    {:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__), [:char_list])
    assert 'FOO\n' == IO.gets(file, "")
    assert :eof == IO.gets(file, "")
    assert File.close(file) == :ok
  end

  test :gets_with_utf8_and_binary do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__), [:utf8])
    assert "Русский\n" == IO.gets(file, "")
    assert "日\n" == IO.gets(file, "")
    assert File.close(file) == :ok
  end

  test :readline do
    {:ok, file} = File.open(Path.expand('fixtures/file.txt', __DIR__))
    assert "FOO\n" == IO.read(file, :line)
    assert :eof == IO.read(file, :line)
    assert File.close(file) == :ok
  end

  test :readline_with_utf8_and_binary do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__), [:utf8])
    assert "Русский\n" == IO.read(file, :line)
    assert "日\n" == IO.read(file, :line)
    assert File.close(file) == :ok
  end

  test :binreadline do
    {:ok, file} = File.open(Path.expand('fixtures/utf8.txt', __DIR__))
    assert "Русский\n" == IO.binread(file, :line)
    assert "日\n" == IO.binread(file, :line)
    assert File.close(file) == :ok
  end

  test :puts_with_chardata do
    assert capture_io(fn -> IO.puts("hello") end) == "hello\n"
    assert capture_io(fn -> IO.puts('hello') end) == "hello\n"
    assert capture_io(fn -> IO.puts(:hello) end) == "hello\n"
    assert capture_io(fn -> IO.puts(13) end) == "13\n"
  end

  test :write_with_chardata do
    assert capture_io(fn -> IO.write("hello") end) == "hello"
    assert capture_io(fn -> IO.write('hello') end) == "hello"
    assert capture_io(fn -> IO.write(:hello) end) == "hello"
    assert capture_io(fn -> IO.write(13) end) == "13"
  end

  test :gets_with_chardata do
    assert capture_io("foo\n", fn -> IO.gets("hello") end) == "hello"
    assert capture_io("foo\n", fn -> IO.gets('hello') end) == "hello"
    assert capture_io("foo\n", fn -> IO.gets(:hello) end) == "hello"
    assert capture_io("foo\n", fn -> IO.gets(13) end) == "13"
  end

  test :getn_with_chardata do
    assert capture_io("foo\n", fn -> IO.getn("hello", 3) end) == "hello"
    assert capture_io("foo\n", fn -> IO.getn('hello', 3) end) == "hello"
    assert capture_io("foo\n", fn -> IO.getn(:hello, 3) end) == "hello"
    assert capture_io("foo\n", fn -> IO.getn(13, 3) end) == "13"
  end
end