Skip to content

Commit

Permalink
Simplify utils for writing tests
Browse files Browse the repository at this point in the history
The current utils for writing buffer operation tests are quite wordy and
not easy for a human to interpret at a glance.

In an effort to make it easier to write detailed, language specific
tests I have added a way to describe test prepare and check content as a
string.

This works by looking for `|` characters in test content and using this
to infer the cursor position.
  • Loading branch information
julienvincent committed Oct 16, 2024
1 parent 2f0e7fc commit 49b0acc
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 168 deletions.
222 changes: 82 additions & 140 deletions tests/nvim-paredit/barf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,87 @@ describe("barfing ::", function()
expect_all(paredit.barf_forwards, {
{
"list",
before_content = "(a)",
before_cursor = { 1, 1 },
after_content = "()a",
after_cursor = { 1, 1 },
{ "(|a)" },
{ "(|)a" },
},
{
"vector",
before_content = "[a]",
before_cursor = { 1, 1 },
after_content = "[]a",
after_cursor = { 1, 1 },
{ "[|a]" },
{ "[|]a" },
},
{
"quoted list",
before_content = "`(a)",
before_cursor = { 1, 2 },
after_content = "`()a",
after_cursor = { 1, 2 },
"`(|a)",
{ "`(|)a" },
},
{
"quoted list",
before_content = "'(a)",
before_cursor = { 1, 2 },
after_content = "'()a",
after_cursor = { 1, 2 },
"'(|a)",
"'(|)a",
},
{
"anon fn",
before_content = "#(a)",
before_cursor = { 1, 2 },
after_content = "#()a",
after_cursor = { 1, 2 },
{ "#(|a)" },
{ "#(|)a" },
},
{
"set",
before_content = "#{a}",
before_cursor = { 1, 2 },
after_content = "#{}a",
after_cursor = { 1, 2 },
"#{|a}",
"#{|}a",
},
{
"reader conditional",
before_content = "#?(:cljs a :clj b)",
before_cursor = { 1, 3 },
after_content = "#?(:cljs a :clj) b",
after_cursor = { 1, 3 },
{ "#?(|:cljs a :clj b)" },
"#?(|:cljs a :clj) b",
},
})
end)

it("should skip comments", function()
prepare_buffer({
content = { "(", ";; comment", "a)" },
cursor = { 1, 1 },
"(|",
";; comment",
"a)",
})
paredit.barf_forwards()
expect({
content = { "()", ";; comment", "a" },
cursor = { 1, 0 },
"|()",
";; comment",
"a",
})

prepare_buffer({
content = { "(a ;; comment", ")" },
cursor = { 1, 1 },
"(|a ;; comment",
")",
})
paredit.barf_forwards()
expect({
content = "()a ;; comment",
cursor = { 1, 1 },
"(|)a ;; comment",
"",
})
end)

it("should do nothing in an empty form", function()
prepare_buffer({
content = "()",
cursor = { 1, 1 },
"(|)",
})
paredit.barf_forwards()
expect({
content = "()",
cursor = { 1, 1 },
"(|)",
})
end)

it("should do nothing in the document root", function()
expect_all(paredit.barf_forwards, {
{
"from root",
before_content = { "(a)", "" },
before_cursor = { 2, 0 },
after_content = { "(a)", "" },
after_cursor = { 2, 0 },
{ "(a)", "|" },
{ "(a)", "|" },
},
{
"from another list",
before_content = { "(a)", "()" },
before_cursor = { 2, 1 },
after_content = { "(a)", "()" },
after_cursor = { 2, 1 },
{ "(a)", "(|)" },
{ "(a)", "(|)" },
},
})
end)
Expand All @@ -121,17 +103,13 @@ describe("barfing ::", function()
expect_all(paredit.barf_forwards, {
{
"double nested list",
before_content = "(() a)",
before_cursor = { 1, 2 },
after_content = "(()) a",
after_cursor = { 1, 2 },
"((|) a)",
"((|)) a",
},
{
"list with quoted list",
before_content = "('())",
before_cursor = { 1, 3 },
after_content = "()'()",
after_cursor = { 1, 1 },
{ "('(|))" },
{ "(|)'()" },
},
})
end)
Expand All @@ -144,17 +122,13 @@ describe("barfing ::", function()
expect_all(barf_with_behaviour, {
{
"single line",
before_content = "(aa bb)",
before_cursor = { 1, 5 },
after_content = "(aa) bb",
after_cursor = { 1, 3 },
{ "(aa b|b)" },
"(aa|) bb",
},
{
"multi line",
before_content = { "(aa", "bb)" },
before_cursor = { 2, 1 },
after_content = { "(aa)", "bb" },
after_cursor = { 1, 3 },
{ "(aa", "b|b)" },
{ "(aa|)", "bb" },
},
})
end)
Expand All @@ -167,17 +141,13 @@ describe("barfing ::", function()
expect_all(barf_with_behaviour, {
{
"single line",
before_content = "(aa bb cc)",
before_cursor = { 1, 4 },
after_content = "(aa bb) cc",
after_cursor = { 1, 6 },
{ "(aa |bb cc)" },
"(aa bb|) cc",
},
{
"multi line",
before_content = { "(aa", "bb", "cc)" },
before_cursor = { 1, 1 },
after_content = { "(aa", "bb)", "cc" },
after_cursor = { 2, 2 },
{ "(|aa", "bb", "cc)" },
{ "(aa", "bb|)", "cc" },
},
})
end)
Expand All @@ -188,98 +158,82 @@ describe("barfing ::", function()
expect_all(paredit.barf_backwards, {
{
"list",
before_content = "(a)",
before_cursor = { 1, 1 },
after_content = "a()",
after_cursor = { 1, 2 },
{ "(|a)" },
{ "a(|)" },
},
{
"vector",
before_content = "[a]",
before_cursor = { 1, 1 },
after_content = "a[]",
after_cursor = { 1, 2 },
{ "[|a]" },
{ "a[|]" },
},
{
"quoted list",
before_content = "`(a)",
before_cursor = { 1, 2 },
after_content = "a`()",
after_cursor = { 1, 3 },
{ "`(|a)" },
{ "a`(|)" },
},
{
"quoted list",
before_content = "'(a)",
before_cursor = { 1, 2 },
after_content = "a'()",
after_cursor = { 1, 3 },
{ "'(|a)" },
{ "a'(|)" },
},
{
"anon fn",
before_content = "#(a)",
before_cursor = { 1, 2 },
after_content = "a#()",
after_cursor = { 1, 3 },
{ "#(|a)" },
{ "a#(|)" },
},
{
"set",
before_content = "#{a}",
before_cursor = { 1, 2 },
after_content = "a#{}",
after_cursor = { 1, 3 },
{ "#{|a}" },
{ "a#{|}" },
},
})
end)

it("should skip comments", function()
prepare_buffer({
content = { "(", ";; comment", "a)" },
cursor = { 1, 1 },
"(|",
";; comment",
"a)",
})
paredit.barf_backwards()
expect({
content = { "", ";; comment", "a()" },
cursor = { 3, 1 },
"",
";; comment",
"a|()",
})

prepare_buffer({
content = { "(a ;; comment", ")" },
cursor = { 1, 1 },
"(|a ;; comment",
")",
})
paredit.barf_backwards()
expect({
content = { "a ;; comment", "()" },
cursor = { 2, 0 },
"a ;; comment",
"|()",
})
end)

it("should do nothing in an empty form", function()
prepare_buffer({
content = "()",
cursor = { 1, 1 },
"(|)",
})
paredit.barf_backwards()
expect({
content = "()",
cursor = { 1, 1 },
"(|)",
})
end)

it("should do nothing in the document root", function()
expect_all(paredit.barf_backwards, {
{
"from root",
before_content = { "(a)", "" },
before_cursor = { 2, 0 },
after_content = { "(a)", "" },
after_cursor = { 2, 0 },
{ "(a)", "|" },
{ "(a)", "|" },
},
{
"from another list",
before_content = { "(a)", "()" },
before_cursor = { 2, 1 },
after_content = { "(a)", "()" },
after_cursor = { 2, 1 },
{ "(a)", "(|)" },
{ "(a)", "(|)" },
},
})
end)
Expand All @@ -288,17 +242,13 @@ describe("barfing ::", function()
expect_all(paredit.barf_backwards, {
{
"double nested list",
before_content = "(a ())",
before_cursor = { 1, 4 },
after_content = "a (())",
after_cursor = { 1, 4 },
{ "(a (|))" },
{ "a ((|))" },
},
{
"list with quoted list",
before_content = "('())",
before_cursor = { 1, 3 },
after_content = "'()()",
after_cursor = { 1, 4 },
{ "('(|))" },
"'()(|)",
},
})
end)
Expand All @@ -311,17 +261,13 @@ describe("barfing ::", function()
expect_all(barf_with_behaviour, {
{
"single line",
before_content = "(aa bb)",
before_cursor = { 1, 1 },
after_content = "aa (bb)",
after_cursor = { 1, 4 },
{ "(|aa bb)" },
{ "aa (|bb)" },
},
{
"multi line",
before_content = { "(aa", "bb)" },
before_cursor = { 1, 1 },
after_content = { "aa", "(bb)" },
after_cursor = { 2, 0 },
{ "(|aa", "bb)" },
{ "aa", "|(bb)" },
},
})
end)
Expand All @@ -334,17 +280,13 @@ describe("barfing ::", function()
expect_all(barf_with_behaviour, {
{
"single line",
before_content = "(aa bb cc)",
before_cursor = { 1, 1 },
after_content = "aa (bb cc)",
after_cursor = { 1, 4 },
{ "(|aa bb cc)" },
{ "aa (|bb cc)" },
},
{
"multi line",
before_content = { "(aa", "bb", "cc)" },
before_cursor = { 1, 1 },
after_content = { "aa", "(bb", "cc)" },
after_cursor = { 2, 0 },
{ "(|aa", "bb", "cc)" },
{ "aa", "|(bb", "cc)" },
},
})
end)
Expand Down
Loading

0 comments on commit 49b0acc

Please sign in to comment.