Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add formatter tests #130

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (e *formatEnvironment) indentLevelUp() {

func (e *formatEnvironment) indentLevelDown() {
e.indentLevel--
if e.indentLevel < 0 {
e.indentLevel = 0
}
}

func (e *formatEnvironment) genIndent() []ast.Node {
Expand Down Expand Up @@ -242,6 +245,7 @@ func formatItem(node ast.Node, env *formatEnvironment) ast.Node {
}
if commentAfterMatcher.IsMatch(node) {
results = append(results, linebreakNode)
env.indentLevelDown()
}

breakStatementAfterMatcher := astutil.NodeMatcher{
Expand All @@ -251,7 +255,7 @@ func formatItem(node ast.Node, env *formatEnvironment) ast.Node {
}
if breakStatementAfterMatcher.IsMatch(node) {
results = append(results, linebreakNode)
env.indentLevelDown()
env.indentLevelReset()
}

return &ast.ItemWith{Toks: results}
Expand Down
55 changes: 54 additions & 1 deletion internal/formatter/formatter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package formatter

import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

"github.com/sqls-server/sqls/ast"
Expand All @@ -20,7 +25,7 @@ func TestEval(t *testing.T) {
{
name: "InsertIntoFormat",
input: "INSERT INTO users (NAME, email) VALUES ('john doe', '[email protected]')",
expected: "INSERT INTO users(\n\tNAME,\n\temail\n)\nVALUES(\n'john doe',\n'[email protected]'\n)",
expected: "INSERT INTO users(\n\tNAME,\n\temail\n)\nVALUES(\n\t'john doe',\n\t'[email protected]'\n)",
params: lsp.DocumentFormattingParams{},
config: &config.Config{
LowercaseKeywords: false,
Expand Down Expand Up @@ -117,3 +122,51 @@ func parseInit(t *testing.T, input string) []*ast.Statement {
}
return stmts
}

func TestFormat(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
dir := filepath.Dir(filename)

files, err := filepath.Glob(filepath.Join(dir, "testdata", "*.sql"))
if err != nil {
t.Fatal(err)
}
slices.Sort(files)

opts := &ast.RenderOptions{
LowerCase: false,
IdentifierQuoted: false,
}
env := &formatEnvironment{}
for _, fname := range files {
b, err := os.ReadFile(fname)
if err != nil {
t.Fatal(err)
}
parsed, err := parser.Parse(string(b))
if err != nil {
t.Fatal(err)
}
formatted := Eval(parsed, env)
got := strings.TrimRight(formatted.Render(opts), "\n") + "\n"

b, err = os.ReadFile(fname[:len(fname)-4] + ".golden")
if err != nil {
t.Fatal(err)
}
want := string(b)
if got != want {
if _, err := os.Stat(fname[:len(fname)-4] + ".ignore"); err != nil {
t.Logf("%s:\n"+
" want: %q\n"+
" got: %q\n",
fname, want, got)
} else {
t.Errorf("%s:\n"+
" want: %q\n"+
" got: %q\n",
fname, want, got)
}
}
}
}
4 changes: 4 additions & 0 deletions internal/formatter/testdata/001.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
*
FROM
TABLE
1 change: 1 addition & 0 deletions internal/formatter/testdata/001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * From table
13 changes: 13 additions & 0 deletions internal/formatter/testdata/002.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- hoge --
SELECT
x/*x*/,
/*x*/y
FROM
zzz;
-- zzzz
SELECT
*
FROM
yyy;
-- yyyy
-- hage --
4 changes: 4 additions & 0 deletions internal/formatter/testdata/002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- hoge --
SELECT x/*x*/, /*x*/y FROM zzz; -- zzzz
SELECT * FROM yyy; -- yyyy
-- hage --
28 changes: 28 additions & 0 deletions internal/formatter/testdata/003.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- hoge --
SELECT
x/*x*/,
/*x*/y
FROM
zzz;
-- zzzz
SELECT
*
FROM
yyy;
-- yyyy
-- hage --
SELECT
ap.autograph_purchase_id AS "id",
ap.order_number AS "order",
ap.product_price AS "productPrice",
i.name AS "influencerName",
p.name AS "productName",
u.email AS "email"
FROM
autograph_purchaseASap innser
JOIN influencer AS i
ON autograph_purchase.influencer_id = influencer.influencer_id
LEFT JOIN product AS p
ON product.product_id = autograph_purchase.product_id
LEFT JOIN USER AS u
ON USER.user_id = autograph_purchase.user_id
Empty file.
15 changes: 15 additions & 0 deletions internal/formatter/testdata/003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SELECT
ap.autograph_purchase_id AS "id",
ap.order_number AS "order",
ap.product_price AS "productPrice",
i.name AS "influencerName",
p.name AS "productName",
u.email AS "email"
FROM
autograph_purchaseASap innser
JOIN influencer AS i
ON autograph_purchase.influencer_id = influencer.influencer_id
LEFT JOIN product AS p
ON product.product_id = autograph_purchase.product_id
LEFT JOIN USER1 AS u
ON USER1.user_id = autograph_purchase.user_id