zombiezen.com/go/lua@v0.0.0-20231013005828-290725fb9140/testdata/iolib.lua (about)

     1  -- Copyright 2023 Ross Light
     2  --
     3  -- Permission is hereby granted, free of charge, to any person obtaining a copy of
     4  -- this software and associated documentation files (the “Software”), to deal in
     5  -- the Software without restriction, including without limitation the rights to
     6  -- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
     7  -- the Software, and to permit persons to whom the Software is furnished to do so,
     8  -- subject to the following conditions:
     9  --
    10  -- The above copyright notice and this permission notice shall be included in all
    11  -- copies or substantial portions of the Software.
    12  --
    13  -- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    15  -- FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    16  -- COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    17  -- IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    18  -- CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    19  --
    20  -- SPDX-License-Identifier: MIT
    21  
    22  -- Writing
    23  do
    24    local f = assert(io.open("foo.txt", "w"))
    25    local wresult = assert(f:write("Hello, ", 42, "!\n"))
    26    assert(wresult == f, "write result is "..tostring(wresult))
    27    wresult = assert(f:write("second line\n"))
    28    assert(wresult == f, "write result is "..tostring(wresult))
    29    assert(f:close())
    30  end
    31  
    32  -- Reading
    33  local lines = {
    34    "Hello, 42!",
    35    "second line",
    36  }
    37  do
    38    local f = assert(io.open("foo.txt"))
    39    local line1 = assert(f:read())
    40    assert(line1 == lines[1])
    41    local rest = assert(f:read("a"))
    42    assert(rest == lines[2].."\n")
    43    assert(f:close())
    44  end
    45  
    46  -- Lines
    47  do
    48    local i = 1
    49    for line in io.lines("foo.txt") do
    50      assert(i <= #lines, "Too many lines")
    51      assert(lines[i] == line, "line "..i..": "..line)
    52      i = i + 1
    53    end
    54  end
    55  
    56  -- Seeking
    57  do
    58    local f = assert(io.open("foo.txt", "r+"))
    59    local firstBytes = assert(f:read(2))
    60    assert(firstBytes == "He")
    61    local pos = assert(f:seek("cur", #lines[1] + 1 - #firstBytes))
    62    assert(pos == (#lines[1] + 1))
    63    lines[2] = "this is a very long line"
    64    assert(f:write(lines[2].."\n"))
    65    pos = assert(f:seek("set"))
    66    assert(pos == 0)
    67    local result = assert(f:read("a"))
    68    local want = lines[1].."\n"..lines[2].."\n"
    69    assert(result == want)
    70    pos = assert(f:seek("cur"))
    71    assert(pos == #want)
    72  end