-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from mattn/add-tests
add formatter tests
- Loading branch information
Showing
9 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
@@ -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, | ||
|
@@ -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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
SELECT | ||
* | ||
FROM | ||
TABLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select * From table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |