Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m00qek committed Jan 4, 2024
1 parent 3dd21c3 commit 2c82797
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 428 deletions.
7 changes: 5 additions & 2 deletions lua/baleia/locations/offsets.lua
Expand Up @@ -11,8 +11,11 @@ local M = {}
---@param offset Offset
---@param position StrictPosition|LoosePosition
local function update_position(position, offset)
position.line = position.line + offset.line
position.column = position.column and position.column + offset.column
local line_offset = offset.line or 0
local column_offset = offset.column or 0

position.line = position.line + line_offset
position.column = position.column and position.column + column_offset
end

---@param offset OffsetConfig
Expand Down
6 changes: 3 additions & 3 deletions lua/baleia/styles.lua
Expand Up @@ -11,7 +11,7 @@ M.ANSI_CODES_PATTERN = ansi.PATTERN
---@field foreground ColorAttribute
---@field special ColorAttribute
---@field modes table<string, ModeAttribute>
---@field offset integer
---@field offset? integer

---@class HighlightAttributes
---@field foreground? string
Expand Down Expand Up @@ -67,14 +67,14 @@ function M.none()
foreground = colors.none(),
background = colors.none(),
special = colors.none(),
offset = 0,
offset = nil,
modes = {},
}

---TODO: WTF
for _, mode in pairs(ansi.modes) do
for name, attr in pairs(mode.definition) do
if style.modes[name] == nil or style.modes[name].value.tag > attr.value.tag then
if style.modes[name] == nil then
style.modes[name] = modes.ignore(attr.value.tag)
end
end
Expand Down
1 change: 1 addition & 0 deletions test/selene.toml
@@ -0,0 +1 @@
std="vim"
35 changes: 19 additions & 16 deletions test/spec/baleia/highlight_spec.lua
@@ -1,20 +1,23 @@
local highlight = require("baleia.highlight")
local text = require("baleia.text")

describe("[all]", function()
it("", function()
local lines = { "; first line\x1b[32m ", "; \x1b[31msecond line" }
it("", function()
local lines = { "; first line\x1b[32m ", "; \x1b[31msecond line" }

assert.combinators.match({
highlights = {
{ line = 1, firstcolumn = 18, name = "B_0_2_none_none" },
{ line = 2, firstcolumn = 8, name = "B_0_1_none_none" } },
definitions = {
B_0_1_none_none = { foreground = "#800000", ctermfg = 1 },
B_0_2_none_none = { foreground = "#008000", ctermfg = 2 },
},
},
highlight.all({ name = 'B', line_starts_at = 3, colors = { cterm = { }, gui = { } } },
{ global = { column = 0, line = 0 } },
lines))
end)
local marks, highlights = text.colors(
{ strip_ansi_codes = true, name = "B", line_starts_at = 3, colors = { cterm = {}, gui = {} } },
lines,
{ global = { column = 1, line = 0 } }
)

assert.combinators.match({
{ line = 1, firstcolumn = 14, highlight = "B_0_2_none_none" },
{ line = 2, firstcolumn = 4, highlight = "B_0_1_none_none" },
}, marks)

assert.combinators.match({
B_0_1_none_none = { foreground = "#800000", ctermfg = 1 },
B_0_2_none_none = { foreground = "#008000", ctermfg = 2 },
}, highlights)
end)
end)
281 changes: 125 additions & 156 deletions test/spec/baleia/locations_spec.lua
@@ -1,166 +1,135 @@
local locations = require("baleia.locations")

describe("[merge_neighbours]", function()
it("Two codes,in the **same** line, without text between them", function()
local locs = locations.extract({ "\x1b[0m\x1b[31mfirst line" })
assert.combinators.match({
{
style = { offset = 9 },
from = { line = 1, column = 1 },
to = { line = 1 }
}
}, locations.merge_neighbours(locs))
end)

it("Two codes,in **different** line, without text between them", function()
local locs = locations.extract({ "first line\x1b[1m", "\x1b[31msecond line" })
assert.combinators.match({
{
style = { offset = 5 },
from = { line = 1, column = 11 },
to = { line = 2 }
},
}, locations.merge_neighbours(locs))
end)

it("Two codes,in the **same** line, without text between them", function()
local locs = locations.extract({ strip_ansi_codes = true }, {}, { "\x1b[0m\x1b[31mfirst line" })
assert.combinators.match({
{
style = { offset = 9 },
from = { line = 1, column = 1 },
to = { line = 1 },
},
}, locs)
end)

it("Two codes,in **different** line, without text between them", function()
local locs = locations.extract({ strip_ansi_codes = true }, {}, { "first line\x1b[1m", "\x1b[31msecond line" })
assert.combinators.match({
{
style = { offset = 5 },
from = { line = 1, column = 11 },
to = { line = 2 },
},
}, locs)
end)
end)

describe("[extract]", function()
it("with no sequences", function()
assert.combinators.match({}, locations.extract({ "no sequences" }))
end)

it("with sequence at the beginning of the line", function()
local lines = { "\x1b[0mfirst line" }
assert.combinators.match({
{ from = { line = 1, column = 1 }, to = { line = 1 } }
}, locations.extract(lines))
end)

it("with sequence at the end of the line", function()
local lines = { "first line\x1b[0m" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 1 } }
}, locations.extract(lines))
end)

it("with sequence at the beginning and end of the line", function()
local lines = { "first line\x1b[0m", "\x1b[0msecond line" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 1 } },
{ from = { line = 2, column = 1 }, to = { line = 2 } },
}, locations.extract(lines))
end)

it("with blank line between", function()
local lines = { "first line\x1b[0m", "second line", "third \x1b[0mline" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 3 } },
{ from = { line = 3, column = 7 }, to = { line = 3 } },
}, locations.extract(lines))
end)

it("with blank lines at the end", function()
local lines = { "first \x1b[31mline", "second \x1b[32mline\x1b[0m", "third line" }

assert.combinators.match({
{ from = { line = 1, column = 7 }, to = { line = 2, column = 7 } },
{ from = { line = 2, column = 8 }, to = { line = 2, column = 16 } },
{ from = { line = 2, column = 17 }, to = { line = 3 } },
}, locations.extract(lines))
end)
it("with no sequences", function()
assert.combinators.match({}, locations.extract({ strip_ansi_codes = true }, {}, { "no sequences" }))
end)

it("with sequence at the beginning of the line", function()
local lines = { "\x1b[0mfirst line" }
assert.combinators.match({
{ from = { line = 1, column = 1 }, to = { line = 1 } },
}, locations.extract({ strip_ansi_codes = true }, {}, lines))
end)

it("with sequence at the end of the line", function()
local lines = { "first line\x1b[0m" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 1 } },
}, locations.extract({ strip_ansi_codes = true }, {}, lines))
end)

it("with sequence at the beginning and end of the line", function()
local lines = { "first line\x1b[0m", "\x1b[0msecond line" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 2 } },
}, locations.extract({ strip_ansi_codes = true }, {}, lines))
end)

it("with blank line between", function()
local lines = { "first line\x1b[0m", "second line", "third \x1b[0mline" }
assert.combinators.match({
{ from = { line = 1, column = 11 }, to = { line = 3 } },
{ from = { line = 3, column = 7 }, to = { line = 3 } },
}, locations.extract({ strip_ansi_codes = true }, {}, lines))
end)

it("with blank lines at the end", function()
local lines = { "first \x1b[31mline", "second \x1b[32mline\x1b[0m", "third line" }

assert.combinators.match({
{ from = { line = 1, column = 7 }, to = { line = 2, column = 7 } },
{ from = { line = 2, column = 8 }, to = { line = 2, column = 11 } },
{ from = { line = 2, column = 12 }, to = { line = 3 } },
}, locations.extract({ strip_ansi_codes = true }, {}, lines))
end)
end)

local offsets = require("baleia.locations.offsets")
describe("[with_offset]", function()
it("considers global offsets", function()
local offset = { global = { line = 100, column = 10 } }
local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 101, column = 11 },
to = { line = 102, column = 11 },
},
}, locations.with_offset(offset, location))
end)

it("considers line offsets", function()
local offset = {
global = { line = 0, column = 0 },
line = { [1] = { column = 5 } }
}

local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 1, column = 6 },
to = { line = 2, column = 1 },
},
}, locations.with_offset(offset, location))
end)

it("considers both global and line offsets", function()
local offset = {
global = { line = 100, column = 10 },
line = { [1] = { column = 5 } }
}

local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 101, column = 16 },
to = { line = 102, column = 11 },
},
}, locations.with_offset(offset, location))
end)
end)

describe("[strip_ansi_codes]", function()
it("", function()
local locs = {
{ style = { offset = 5 }, from = { line = 1, column = 7 }, to = { line = 2, column = 7 } },
{ style = { offset = 5 }, from = { line = 2, column = 8 }, to = { line = 2, column = 16 } },
{ style = { offset = 4 }, from = { line = 2, column = 17 }, to = { line = 3 } },
}
assert.combinators.match({
{ from = { column = 7 }, to = { column = 7 } },
{ from = { column = 8 }, to = { column = 11 } },
{ from = { column = 12 } },
}, locations.strip_ansi_codes(locs))
end)
end)

describe("[ignore_ansi_codes]", function()
it("", function()
local locs = {
{ style = { offset = 5 }, from = { line = 1, column = 7 }, to = { line = 2, column = 7 } },
{ style = { offset = 5 }, from = { line = 2, column = 8 }, to = { line = 2, column = 16 } },
{ style = { offset = 4 }, from = { line = 2, column = 17 }, to = { line = 3 } },
}
assert.combinators.match({
{ from = { column = 12 }, to = { column = 7 } },
{ from = { column = 13 }, to = { column = 16 } },
{ from = { column = 21 } },
}, locations.ignore_ansi_codes(locs))
end)
it("considers global offsets", function()
local offset = { global = { line = 100, column = 10 } }
local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 101, column = 11 },
to = { line = 102, column = 11 },
},
}, offsets.apply(offset, location))
end)

it("considers line offsets", function()
local offset = {
global = { line = 0, column = 0 },
lines = { [1] = { column = 5 } },
}

local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 1, column = 6 },
to = { line = 2, column = 1 },
},
}, offsets.apply(offset, location))
end)

it("considers both global and line offsets", function()
local offset = {
global = { line = 100, column = 10 },
lines = { [1] = { column = 5 } },
}

local location = {
{
style = { offset = 0 },
from = { line = 1, column = 1 },
to = { line = 2, column = 1 },
},
}

assert.combinators.match({
{
from = { line = 1, column = 6 },
to = { line = 102, column = 11 },
},
}, offsets.apply(offset, location))
end)
end)

0 comments on commit 2c82797

Please sign in to comment.