Mercurial > prosody-hg
diff spec/util_ringbuffer_spec.lua @ 14186:d975d280baec
Merge 13.0->trunk
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Mon, 25 May 2026 19:01:23 +0100 |
| parents | 67b68147ed1d |
| children |
line wrap: on
line diff
--- a/spec/util_ringbuffer_spec.lua Tue May 19 19:27:43 2026 +0100 +++ b/spec/util_ringbuffer_spec.lua Mon May 25 19:01:23 2026 +0100 @@ -35,6 +35,51 @@ end); end); + describe(":find", function () + local function test_find(b, str, expected) + if expected ~= nil then + assert.equal(expected, b:find(str)); + else + assert.is_nil(b:find(str)); + end + end + + it("works", function () + local b = rb.new(); + assert.truthy(b:write("hello world")); + test_find(b, "world", 7); + end); + + it("works across wraps", function () + local b = rb.new(5); + assert.truthy(b:write("12345")); + assert.equal("123", b:read(3)); + assert.truthy(b:write("678")); + test_find(b, "45678", 1); + test_find(b, "567", 2); + test_find(b, "56", 2); + test_find(b, "5", 2); + test_find(b, "6", 3); + test_find(b, "7", 4); + test_find(b, "8", 5); + test_find(b, "9", nil); + end); + + it("no match when the needle is larger than the buffer", function () + for i = 5, 11 do + local b = rb.new(i); + assert.truthy(b:write("hello")); + test_find(b, "hello world", nil); + end + end); + + it("no match when the needle is an empty string", function () + local b = rb.new(5); + assert.truthy(b:write("hello")); + test_find(b, "", nil); + end); + end); + describe(":sub", function () -- Helper function to compare buffer:sub() with string:sub() local function test_sub(b, x, y)
