Skip to content

Commit db2581c

Browse files
test: add unit tests for class, orderedmap, stringbuffer (#1372)
* test: add unit tests for class, orderedmap, stringbuffer Add new unit tests for CopilotChat.utils.class, orderedmap, and stringbuffer modules. These tests cover class creation, inheritance, ordered map behavior, and string buffer operations to improve code reliability and coverage. Signed-off-by: Tomas Slusny <slusnucky@gmail.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Tomas Slusny <slusnucky@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a5ac084 commit db2581c

3 files changed

Lines changed: 84 additions & 0 deletions

File tree

tests/class_spec.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
local class = require('CopilotChat.utils.class')
2+
3+
describe('CopilotChat.utils.class', function()
4+
it('creates a simple class', function()
5+
local Foo = class(function(self, x)
6+
self.x = x
7+
end)
8+
local obj = Foo(42)
9+
assert.equals(42, obj.x)
10+
end)
11+
12+
it('supports init method', function()
13+
local Bar = class(function(self, y)
14+
self.y = y
15+
end)
16+
local obj = Bar.new(7)
17+
assert.equals(7, obj.y)
18+
obj:init(8)
19+
assert.equals(8, obj.y)
20+
end)
21+
22+
it('supports inheritance', function()
23+
local Parent = class(function(self)
24+
self.val = 1
25+
end)
26+
local Child = class(function(self)
27+
self.val = 2
28+
end, Parent)
29+
local obj = Child()
30+
assert.equals(2, obj.val)
31+
assert.equals(Parent, getmetatable(Child).__index)
32+
end)
33+
end)

tests/orderedmap_spec.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local orderedmap = require('CopilotChat.utils.orderedmap')
2+
3+
describe('CopilotChat.utils.orderedmap', function()
4+
it('sets and gets values', function()
5+
local map = orderedmap()
6+
map:set('a', 1)
7+
map:set('b', 2)
8+
assert.equals(1, map:get('a'))
9+
assert.equals(2, map:get('b'))
10+
end)
11+
12+
it('preserves insertion order', function()
13+
local map = orderedmap()
14+
map:set('x', 10)
15+
map:set('y', 20)
16+
map:set('z', 30)
17+
assert.are.same({ 'x', 'y', 'z' }, map:keys())
18+
assert.are.same({ 10, 20, 30 }, map:values())
19+
end)
20+
21+
it('overwrites value but not order', function()
22+
local map = orderedmap()
23+
map:set('a', 1)
24+
map:set('a', 2)
25+
assert.are.same({ 'a' }, map:keys())
26+
assert.are.same({ 2 }, map:values())
27+
end)
28+
end)

tests/stringbuffer_spec.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
local stringbuffer = require('CopilotChat.utils.stringbuffer')
2+
3+
describe('CopilotChat.utils.stringbuffer', function()
4+
it('concatenates strings with put', function()
5+
local buf = stringbuffer()
6+
buf:put('hello')
7+
buf:put(' ')
8+
buf:put('world')
9+
assert.equals('hello world', buf:tostring())
10+
end)
11+
12+
it('sets buffer with set', function()
13+
local buf = stringbuffer()
14+
buf:put('foo')
15+
buf:set('bar')
16+
assert.equals('bar', buf:tostring())
17+
end)
18+
19+
it('handles empty buffer', function()
20+
local buf = stringbuffer()
21+
assert.equals('', buf:tostring())
22+
end)
23+
end)

0 commit comments

Comments
 (0)