diff options
| author | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:46:05 +0930 |
|---|---|---|
| committer | Mark Pulford <mark@kyne.com.au> | 2011-05-16 19:46:05 +0930 |
| commit | 6ba7499ac5992c8cc849ffcbd6d2837d305f5b63 (patch) | |
| tree | f7765d77bb51d3401f4fe0a9acc7279596928918 /tests/common.lua | |
| parent | 22550d0ab3328554922822ca0207996b3d1bb73e (diff) | |
| download | lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.tar.gz lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.tar.bz2 lua-cjson-6ba7499ac5992c8cc849ffcbd6d2837d305f5b63.zip | |
Add support for stdin to encode.lua/decode.lua
Diffstat (limited to 'tests/common.lua')
| -rw-r--r-- | tests/common.lua | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/tests/common.lua b/tests/common.lua index e08b6c2..0b231bd 100644 --- a/tests/common.lua +++ b/tests/common.lua | |||
| @@ -96,12 +96,21 @@ function dump_value(value) | |||
| 96 | end | 96 | end |
| 97 | 97 | ||
| 98 | function file_load(filename) | 98 | function file_load(filename) |
| 99 | local file, err = io.open(filename) | 99 | local file |
| 100 | if file == nil then | 100 | if filename == nil then |
| 101 | error("Unable to read " .. filename) | 101 | file = io.stdin |
| 102 | else | ||
| 103 | local err | ||
| 104 | file, err = io.open(filename) | ||
| 105 | if file == nil then | ||
| 106 | error(string.format("Unable to read '%s': %s", filename, err)) | ||
| 107 | end | ||
| 102 | end | 108 | end |
| 103 | local data = file:read("*a") | 109 | local data = file:read("*a") |
| 104 | file:close() | 110 | |
| 111 | if filename ~= nil then | ||
| 112 | file:close() | ||
| 113 | end | ||
| 105 | 114 | ||
| 106 | if data == nil then | 115 | if data == nil then |
| 107 | error("Failed to read " .. filename) | 116 | error("Failed to read " .. filename) |
| @@ -111,12 +120,20 @@ function file_load(filename) | |||
| 111 | end | 120 | end |
| 112 | 121 | ||
| 113 | function file_save(filename, data) | 122 | function file_save(filename, data) |
| 114 | local file, err = io.open(filename, "w") | 123 | local file |
| 115 | if file == nil then | 124 | if filename == nil then |
| 116 | error("Unable to write " .. filename) | 125 | file = io.stdout |
| 126 | else | ||
| 127 | local err | ||
| 128 | file, err = io.open(filename, "w") | ||
| 129 | if file == nil then | ||
| 130 | error(string.format("Unable to write '%s': %s", filename, err)) | ||
| 131 | end | ||
| 117 | end | 132 | end |
| 118 | file:write(data) | 133 | file:write(data) |
| 119 | file:close() | 134 | if filename ~= nil then |
| 135 | file:close() | ||
| 136 | end | ||
| 120 | end | 137 | end |
| 121 | 138 | ||
| 122 | function compare_values(val1, val2) | 139 | function compare_values(val1, val2) |
