-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
fix: Support for multiple structs in one file (#2)
Showing
15 changed files
with
388 additions
and
111 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
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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//nolint:testpackage | ||
package arcgengo | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"testing" | ||
|
||
"github.com/kunitsucom/util.go/testing/assert" | ||
"github.com/kunitsucom/util.go/testing/require" | ||
|
||
"github.com/kunitsucom/arcgen/internal/config" | ||
"github.com/kunitsucom/arcgen/internal/contexts" | ||
) | ||
|
||
//nolint:paralleltest | ||
func TestGenerate(t *testing.T) { | ||
t.Run("success,tests", func(t *testing.T) { | ||
ctx := contexts.WithArgs(context.Background(), []string{ | ||
"ddlgen", | ||
"--column-tag-go=dbtest", | ||
"--method-prefix-global=Get", | ||
// "--src=tests/common.source", | ||
"--src=tests", | ||
}) | ||
|
||
backup := fileSuffix | ||
t.Cleanup(func() { fileSuffix = backup }) | ||
|
||
_, err := config.Load(ctx) | ||
require.NoError(t, err) | ||
|
||
fileSuffix = ".source" | ||
require.NoError(t, Generate(ctx, config.Source())) | ||
|
||
{ | ||
expectedFile, err := os.Open("tests/common.golden") | ||
require.NoError(t, err) | ||
expectedBytes, err := io.ReadAll(expectedFile) | ||
require.NoError(t, err) | ||
expected := string(expectedBytes) | ||
|
||
actualFile, err := os.Open("tests/common.dbtest.gen.source") | ||
require.NoError(t, err) | ||
actualBytes, err := io.ReadAll(actualFile) | ||
require.NoError(t, err) | ||
actual := string(actualBytes) | ||
|
||
assert.Equal(t, expected, actual) | ||
} | ||
}) | ||
|
||
t.Run("failure,no.errsource", func(t *testing.T) { | ||
ctx := contexts.WithArgs(context.Background(), []string{ | ||
"ddlgen", | ||
"--column-tag-go=dbtest", | ||
"--method-prefix-global=Get", | ||
"--src=tests/no.errsource", | ||
}) | ||
|
||
backup := fileSuffix | ||
t.Cleanup(func() { fileSuffix = backup }) | ||
|
||
_, err := config.Load(ctx) | ||
require.NoError(t, err) | ||
|
||
fileSuffix = ".source" | ||
require.ErrorsContains(t, Generate(ctx, config.Source()), "expected 'package', found 'EOF'") | ||
}) | ||
|
||
t.Run("failure,no.errsource", func(t *testing.T) { | ||
ctx := contexts.WithArgs(context.Background(), []string{ | ||
"ddlgen", | ||
"--column-tag-go=dbtest", | ||
"--method-prefix-global=Get", | ||
"--src=tests", | ||
}) | ||
|
||
backup := fileSuffix | ||
t.Cleanup(func() { fileSuffix = backup }) | ||
|
||
_, err := config.Load(ctx) | ||
require.NoError(t, err) | ||
|
||
fileSuffix = ".errsource" | ||
require.ErrorsContains(t, Generate(ctx, config.Source()), "expected 'package', found 'EOF'") | ||
}) | ||
|
||
t.Run("failure,no-such-file-or-directory", func(t *testing.T) { | ||
ctx := contexts.WithArgs(context.Background(), []string{ | ||
"ddlgen", | ||
"--column-tag-go=dbtest", | ||
"--method-prefix-global=Get", | ||
"--src=tests/no-such-file-or-directory", | ||
}) | ||
|
||
backup := fileSuffix | ||
t.Cleanup(func() { fileSuffix = backup }) | ||
|
||
_, err := config.Load(ctx) | ||
require.NoError(t, err) | ||
|
||
fileSuffix = ".source" | ||
require.ErrorsContains(t, Generate(ctx, config.Source()), "no such file or directory") | ||
}) | ||
|
||
t.Run("failure,directory.dir", func(t *testing.T) { | ||
ctx := contexts.WithArgs(context.Background(), []string{ | ||
"ddlgen", | ||
"--column-tag-go=dbtest", | ||
"--method-prefix-global=Get", | ||
"--src=tests/directory.dir", | ||
}) | ||
|
||
backup := fileSuffix | ||
t.Cleanup(func() { fileSuffix = backup }) | ||
|
||
_, err := config.Load(ctx) | ||
require.NoError(t, err) | ||
|
||
fileSuffix = ".dir" | ||
require.ErrorsContains(t, Generate(ctx, config.Source()), "is a directory") | ||
}) | ||
} |
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
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 @@ | ||
*.gen.source |
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,3 @@ | ||
package tests | ||
|
||
// dbtest: table: `Tables` |
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,41 @@ | ||
// Code generated by arcgen. DO NOT EDIT. | ||
// | ||
// source: tests/common.source | ||
|
||
package main | ||
|
||
func (s *User) GetTableName() string { | ||
return "`Users`" | ||
} | ||
|
||
func (s *User) GetColumnNames() []string { | ||
return []string{"Id", "Name", "Email", "Age"} | ||
} | ||
|
||
func (s *User) GetColumnName_Id() string { | ||
return "Id" | ||
} | ||
|
||
func (s *User) GetColumnName_Name() string { | ||
return "Name" | ||
} | ||
|
||
func (s *User) GetColumnName_Email() string { | ||
return "Email" | ||
} | ||
|
||
func (s *User) GetColumnName_Age() string { | ||
return "Age" | ||
} | ||
|
||
func (s *Group) GetColumnNames() []string { | ||
return []string{"Id", "Name"} | ||
} | ||
|
||
func (s *Group) GetColumnName_Id() string { | ||
return "Id" | ||
} | ||
|
||
func (s *Group) GetColumnName_Name() string { | ||
return "Name" | ||
} |
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,36 @@ | ||
package main | ||
|
||
type ( | ||
// User is a user. | ||
// | ||
// dbtest: table: `Users` | ||
User struct { | ||
// ID is a user ID. | ||
ID string `dbtest:"Id"` | ||
// Name is a user name. | ||
Name string `dbtest:"Name"` | ||
// Email is a user email. | ||
Email string `dbtest:"Email"` | ||
// Age is a user age. | ||
Age int `dbtest:"Age"` | ||
// Ignore is a ignore field. | ||
Ignore string `dbtest:"-"` | ||
} | ||
|
||
// Users is a slice of User. | ||
// | ||
// dbtest: table: `Users` | ||
Users []*User | ||
|
||
// dbtest: table: `InvalidUsers` | ||
InvalidUser struct { | ||
ID string | ||
} | ||
|
||
// Group is a group. | ||
// | ||
Group struct { | ||
ID string `dbtest:"Id"` | ||
Name string `dbtest:"Name"` | ||
} | ||
) |
1 change: 1 addition & 0 deletions
1
internal/arcgen/lang/go/tests/directory.dbtest.gen.dir/.gitignore
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 @@ | ||
* |
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,10 @@ | ||
package main | ||
|
||
type ( | ||
// ReadOnly is a struct. | ||
// | ||
// dbtest: table: ReadOnly | ||
ReadOnly struct { | ||
Name string `dbtest:"ReadOnly"` | ||
} | ||
) |
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 @@ | ||
package tests |
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