Skip to content

Commit

Permalink
Merge pull request #130 from mattn/add-tests
Browse files Browse the repository at this point in the history
add formatter tests
  • Loading branch information
mattn committed Dec 30, 2023
2 parents 8aa922d + 04efc44 commit 7bc8eb6
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 2 deletions.
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', 'example@host.com')",
expected: "INSERT INTO users(\n\tNAME,\n\temail\n)\nVALUES(\n'john doe',\n'example@host.com'\n)",
expected: "INSERT INTO users(\n\tNAME,\n\temail\n)\nVALUES(\n\t'john doe',\n\t'example@host.com'\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

0 comments on commit 7bc8eb6

Please sign in to comment.