diff options
-rw-r--r-- | src/ltn12.lua | 13 | ||||
-rw-r--r-- | test/ltn12test.lua | 25 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index 5b10f56..1014de2 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -139,7 +139,9 @@ function source.rewind(src) | |||
139 | end | 139 | end |
140 | end | 140 | end |
141 | 141 | ||
142 | function source.chain(src, f) | 142 | -- chains a source with one or several filter(s) |
143 | function source.chain(src, f, ...) | ||
144 | if ... then f=filter.chain(f, ...) end | ||
143 | base.assert(src and f) | 145 | base.assert(src and f) |
144 | local last_in, last_out = "", "" | 146 | local last_in, last_out = "", "" |
145 | local state = "feeding" | 147 | local state = "feeding" |
@@ -254,8 +256,13 @@ function sink.error(err) | |||
254 | end | 256 | end |
255 | end | 257 | end |
256 | 258 | ||
257 | -- chains a sink with a filter | 259 | -- chains a sink with one or several filter(s) |
258 | function sink.chain(f, snk) | 260 | function sink.chain(f, snk, ...) |
261 | if ... then | ||
262 | local args = { f, snk, ... } | ||
263 | snk = table.remove(args, #args) | ||
264 | f = filter.chain(unpack(args)) | ||
265 | end | ||
259 | base.assert(f and snk) | 266 | base.assert(f and snk) |
260 | return function(chunk, err) | 267 | return function(chunk, err) |
261 | if chunk ~= "" then | 268 | if chunk ~= "" then |
diff --git a/test/ltn12test.lua b/test/ltn12test.lua index 74a45e8..e3f85fb 100644 --- a/test/ltn12test.lua +++ b/test/ltn12test.lua | |||
@@ -192,6 +192,21 @@ assert(filter(nil, 1), "filter not empty") | |||
192 | print("ok") | 192 | print("ok") |
193 | 193 | ||
194 | -------------------------------- | 194 | -------------------------------- |
195 | io.write("testing source.chain (with several filters): ") | ||
196 | local function double(x) -- filter turning "ABC" into "AABBCC" | ||
197 | if not x then return end | ||
198 | local b={} | ||
199 | for k in x:gmatch'.' do table.insert(b, k..k) end | ||
200 | return table.concat(b) | ||
201 | end | ||
202 | source = ltn12.source.string(s) | ||
203 | source = ltn12.source.chain(source, double, double, double) | ||
204 | sink, t = ltn12.sink.table() | ||
205 | assert(ltn12.pump.all(source, sink), "returned error") | ||
206 | assert(table.concat(t) == double(double(double(s))), "mismatch") | ||
207 | print("ok") | ||
208 | |||
209 | -------------------------------- | ||
195 | io.write("testing source.chain (with split) and sink.chain (with merge): ") | 210 | io.write("testing source.chain (with split) and sink.chain (with merge): ") |
196 | source = ltn12.source.string(s) | 211 | source = ltn12.source.string(s) |
197 | filter = split(5) | 212 | filter = split(5) |
@@ -206,6 +221,15 @@ assert(filter2(nil, 1), "filter2 not empty") | |||
206 | print("ok") | 221 | print("ok") |
207 | 222 | ||
208 | -------------------------------- | 223 | -------------------------------- |
224 | io.write("testing sink.chain (with several filters): ") | ||
225 | source = ltn12.source.string(s) | ||
226 | sink, t = ltn12.sink.table() | ||
227 | sink = ltn12.sink.chain(double, double, double, sink) | ||
228 | assert(ltn12.pump.all(source, sink), "returned error") | ||
229 | assert(table.concat(t) == double(double(double(s))), "mismatch") | ||
230 | print("ok") | ||
231 | |||
232 | -------------------------------- | ||
209 | io.write("testing filter.chain (and sink.chain, with split, merge): ") | 233 | io.write("testing filter.chain (and sink.chain, with split, merge): ") |
210 | source = ltn12.source.string(s) | 234 | source = ltn12.source.string(s) |
211 | filter = split(5) | 235 | filter = split(5) |
@@ -272,3 +296,4 @@ assert(filter3(nil, 1), "filter3 not empty") | |||
272 | assert(filter4(nil, 1), "filter4 not empty") | 296 | assert(filter4(nil, 1), "filter4 not empty") |
273 | assert(filter5(nil, 1), "filter5 not empty") | 297 | assert(filter5(nil, 1), "filter5 not empty") |
274 | print("ok") | 298 | print("ok") |
299 | |||