From a48c3d0ba8a86ea62308c7c556582c380b0f1228 Mon Sep 17 00:00:00 2001 From: SquidDev Date: Wed, 1 Jan 2020 09:01:00 +0000 Subject: [PATCH] A couple of io fixes - Use expect within io.write before calling the handle's write function. Closes #338 - Coerce to string in write before doing any writing. Fixes #339 --- .../resources/assets/computercraft/lua/bios.lua | 1 + .../assets/computercraft/lua/rom/apis/io.lua | 6 +++++- src/test/resources/test-rom/spec/base_spec.lua | 12 ++++++++++++ .../resources/test-rom/spec/test_helpers.lua | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/bios.lua b/src/main/resources/assets/computercraft/lua/bios.lua index 0cceccf99..2913da090 100644 --- a/src/main/resources/assets/computercraft/lua/bios.lua +++ b/src/main/resources/assets/computercraft/lua/bios.lua @@ -207,6 +207,7 @@ function write( sText ) end -- Print the line with proper word wrapping + sText = tostring(sText) while #sText > 0 do local whitespace = string.match( sText, "^[ \t]+" ) if whitespace then diff --git a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua index 779d7e26d..0011ab73c 100644 --- a/src/main/resources/assets/computercraft/lua/rom/apis/io.lua +++ b/src/main/resources/assets/computercraft/lua/rom/apis/io.lua @@ -122,7 +122,11 @@ handleMetatable = { if not handle.write then return nil, "file is not writable" end local n = select("#", ...) - for i = 1, n do handle.write(select(i, ...)) end + for i = 1, n do + local arg = select(i, ...) + expect(1, arg, "string", "number") + handle.write(arg) + end return self end, }, diff --git a/src/test/resources/test-rom/spec/base_spec.lua b/src/test/resources/test-rom/spec/base_spec.lua index 8ce2213bf..2e557e132 100644 --- a/src/test/resources/test-rom/spec/base_spec.lua +++ b/src/test/resources/test-rom/spec/base_spec.lua @@ -1,3 +1,5 @@ +local with_window = require "test_helpers".with_window + describe("The Lua base library", function() describe("sleep", function() it("validates arguments", function() @@ -13,6 +15,16 @@ describe("The Lua base library", function() write("") expect.error(write, nil):eq("bad argument #1 (expected string or number, got nil)") end) + + it("writes numbers", function() + local w = with_window(5, 5, function() write(123) end) + expect(w.getLine(1)):eq("123 ") + end) + + it("writes strings", function() + local w = with_window(5, 5, function() write("abc") end) + expect(w.getLine(1)):eq("abc ") + end) end) describe("loadfile", function() diff --git a/src/test/resources/test-rom/spec/test_helpers.lua b/src/test/resources/test-rom/spec/test_helpers.lua index 74b94b7e2..5d1ba9f1c 100644 --- a/src/test/resources/test-rom/spec/test_helpers.lua +++ b/src/test/resources/test-rom/spec/test_helpers.lua @@ -41,6 +41,22 @@ local function capture_program(stub, program, ...) } end +--- Run a function redirecting to a new window with the given dimensions +-- +-- @tparam number width The window's width +-- @tparam number height The window's height +-- @tparam function() fn The action to run +-- @treturn window.Window The window, whose content can be queried. +local function with_window(width, height, fn) + local current = term.current() + local redirect = window.create(current, 1, 1, width, height, false) + term.redirect(redirect) + fn() + term.redirect(current) + return redirect +end + return { capture_program = capture_program, + with_window = with_window, }