Annotate

spec/util_array_spec.lua @ 13652:a08065207ef0

net.server_epoll: Call :shutdown() on TLS sockets when supported Comment from Matthew: This fixes a potential issue where the Prosody process gets blocked on sockets waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends layer 7 data, and this can cause problems for sockets which are in the process of being cleaned up. This depends on LuaSec changes which are not yet upstream. From Martijn's original email: So first my analysis of luasec. in ssl.c the socket is put into blocking mode right before calling SSL_shutdown() inside meth_destroy(). My best guess to why this is is because meth_destroy is linked to the __close and __gc methods, which can't exactly be called multiple times and luasec does want to make sure that a tls session is shutdown as clean as possible. I can't say I disagree with this reasoning and don't want to change this behaviour. My solution to this without changing the current behaviour is to introduce a shutdown() method. I am aware that this overlaps in a conflicting way with tcp's shutdown method, but it stays close to the OpenSSL name. This method calls SSL_shutdown() in the current (non)blocking mode of the underlying socket and returns a boolean whether or not the shutdown is completed (matching SSL_shutdown()'s 0 or 1 return values), and returns the familiar ssl_ioerror() strings on error with a false for completion. This error can then be used to determine if we have wantread/wantwrite to finalize things. Once meth_shutdown() has been called once a shutdown flag will be set, which indicates to meth_destroy() that the SSL_shutdown() has been handled by the application and it shouldn't be needed to set the socket to blocking mode. I've left the SSL_shutdown() call in the LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a timeout for the shutdown code, which might allow SSL_shutdown() to clean up anyway at the last possible moment. Another thing I've changed to luasec is the call to socket_setblocking() right before calling close(2) in socket_destroy() in usocket.c. According to the latest POSIX[0]: Note that the requirement for close() on a socket to block for up to the current linger interval is not conditional on the O_NONBLOCK setting. Which I read to mean that removing O_NONBLOCK on the socket before close doesn't impact the behaviour and only causes noise in system call tracers. I didn't touch the windows bits of this, since I don't do windows. For the prosody side of things I've made the TLS shutdown bits resemble interface:onwritable(), and put it under a combined guard of self._tls and self.conn.shutdown. The self._tls bit is there to prevent getting stuck on this condition, and self.conn.shutdown is there to prevent the code being called by instances where the patched luasec isn't deployed. The destroy() method can be called from various places and is read by me as the "we give up" error path. To accommodate for these unexpected entrypoints I've added a single call to self.conn:shutdown() to prevent the socket being put into blocking mode. I have no expectations that there is any other use here. Same as previous, the self.conn.shutdown check is there to make sure it's not called on unpatched luasec deployments and self._tls is there to make sure we don't call shutdown() on tcp sockets. I wouldn't recommend logging of the conn:shutdown() error inside close(), since a lot of clients simply close the connection before SSL_shutdown() is done.
author Martijn van Duren <martijn@openbsd.org>
date Thu, 06 Feb 2025 15:04:38 +0000
parent 13245:ffe4adbd2af9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local array = require "util.array";
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 describe("util.array", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 describe("creation", function ()
13245
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
4 describe("new", function ()
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
5 it("works", function ()
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
6 local a = array.new({"a", "b", "c"});
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
7 assert.same({"a", "b", "c"}, a);
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
8 end);
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
9 end);
ffe4adbd2af9 util.array: Fix new() library function
Kim Alvefur <zash@zash.se>
parents: 11787
diff changeset
10
10397
921e8b00778e util.array: Fix typo in test
Kim Alvefur <zash@zash.se>
parents: 10100
diff changeset
11 describe("from table", function ()
10100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local a = array({"a", "b", "c"});
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 assert.same({"a", "b", "c"}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 describe("from iterator", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 -- collects the first value, ie the keys
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local a = array(ipairs({true, true, true}));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 assert.same({1, 2, 3}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 describe("collect", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 -- collects the first value, ie the keys
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 local a = array.collect(ipairs({true, true, true}));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 assert.same({1, 2, 3}, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 describe("metatable", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 describe("operator", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 describe("addition", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local a = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 local b = array({ "c", "d" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 assert.same({"a", "b", "c", "d"}, a + b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 describe("equality", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local a1 = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 local a2 = array({ "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 local b = array({ "c", "d" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 assert.truthy(a1 == a2);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 assert.falsy(a1 == b);
10590
257dc26e8e65 util.array: Add a test case for a behavior change in Lua 5.3
Kim Alvefur <zash@zash.se>
parents: 10397
diff changeset
53 assert.falsy(a1 == { "a", "b" }, "Behavior of metatables changed in Lua 5.3");
10100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 describe("division", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 local b = a / function (i) if i ~= "b" then return i .. "x" end end;
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 assert.same({ "ax", "cx" }, b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 describe("methods", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 describe("map", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 local b = a:map(string.upper);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 assert.same({ "A", "B", "C" }, b);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 describe("filter", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 a:filter(function (i) return i ~= "b" end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 assert.same({ "a", "c" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 describe("sort", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local a = array({ 5, 4, 3, 1, 2, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 a:sort();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 assert.same({ 1, 2, 3, 4, 5, }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 describe("unique", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 local a = array({ "a", "b", "c", "c", "a", "b" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 a:unique();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 assert.same({ "a", "b", "c" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
99 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 describe("pluck", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 local a = array({ { a = 1, b = -1 }, { a = 2, b = -2 }, });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104 a:pluck("a");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 assert.same({ 1, 2 }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 describe("reverse", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 a:reverse();
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 assert.same({ "c", "b", "a" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 -- TODO :shuffle
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120 describe("append", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 a:append(array({ "d", "e", }));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 assert.same({ "a", "b", "c", "d", "e" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
128 describe("push", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
131 a:push("d"):push("e");
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 assert.same({ "a", "b", "c", "d", "e" }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
133 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 describe("pop", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
138 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 assert.equal("c", a:pop());
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
140 assert.same({ "a", "b", }, a);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
142 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 describe("concat", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 assert.equal("a,b,c", a:concat(","));
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
149 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
151 describe("length", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
152 it("works", function ()
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
153 local a = array({ "a", "b", "c" });
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154 assert.equal(3, a:length());
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157
11787
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
158 describe("slice", function ()
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
159 it("works", function ()
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
160 local a = array({ "a", "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
161 assert.equal(array.slice(a, 1, 2), array{ "a", "b" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
162 assert.equal(array.slice(a, 1, 3), array{ "a", "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
163 assert.equal(array.slice(a, 2, 3), array{ "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
164 assert.equal(array.slice(a, 2), array{ "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
165 assert.equal(array.slice(a, -4), array{ "a", "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
166 assert.equal(array.slice(a, -3), array{ "a", "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
167 assert.equal(array.slice(a, -2), array{ "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
168 assert.equal(array.slice(a, -1), array{ "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
169 end);
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
170
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
171 it("can mutate", function ()
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
172 local a = array({ "a", "b", "c" });
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
173 assert.equal(a:slice(-1), array{"c"});
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
174 assert.equal(a, array{"c"});
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
175 end);
3ae6fa901a8b util.array: Add :slice() method + tests
Matthew Wild <mwild1@gmail.com>
parents: 10590
diff changeset
176 end);
10100
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 -- TODO The various array.foo(array ina, array outa) functions
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180 end);
5ca9c4917885 util.array: Add tests
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181