-
-
Notifications
You must be signed in to change notification settings - Fork 50
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 #70 from gzuidhof/add-test-cases
Add support for direct string translation, add conversion tests
- Loading branch information
Showing
14 changed files
with
650 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Test Tygo | ||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: | ||
- 1.22.x | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v4 | ||
- name: install | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: '${{ matrix.go-version }}' | ||
- name: vet | ||
run: go vet ./... | ||
- name: test | ||
run: go test -v -race ./... |
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
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 @@ | ||
package tygo | ||
|
||
import ( | ||
"fmt" | ||
"go/parser" | ||
"go/token" | ||
"strings" | ||
) | ||
|
||
// ConvertGoToTypescript converts Go code string to Typescript. | ||
// | ||
// This is mostly useful for testing purposes inside tygo itself. | ||
func ConvertGoToTypescript(goCode string, pkgConfig PackageConfig) (string, error) { | ||
src := fmt.Sprintf(`package tygoconvert | ||
%s`, goCode) | ||
|
||
fset := token.NewFileSet() | ||
|
||
f, err := parser.ParseFile(fset, "", src, parser.AllErrors|parser.ParseComments) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse source: %w", err) | ||
} | ||
|
||
pkgConfig, err = pkgConfig.Normalize() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to normalize package config: %w", err) | ||
} | ||
|
||
pkgGen := &PackageGenerator{ | ||
conf: &pkgConfig, | ||
pkg: nil, | ||
} | ||
|
||
s := new(strings.Builder) | ||
|
||
pkgGen.generateFile(s, f, "") | ||
code := s.String() | ||
|
||
return code, nil | ||
} |
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,113 @@ | ||
package tygo | ||
|
||
import ( | ||
"embed" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Embed markdown test fixtures | ||
// | ||
//go:embed testdata/fixtures/*.md | ||
var mdfs embed.FS | ||
|
||
type MarkdownFixture struct { | ||
PackageConfig PackageConfig | ||
GoCode string | ||
TsCode string | ||
} | ||
|
||
func TestConvertGoToTypescriptSmoketest(t *testing.T) { | ||
t.Parallel() | ||
|
||
goCode := "type MyType uint8" | ||
tsCode, err := ConvertGoToTypescript(goCode, PackageConfig{}) | ||
require.NoError(t, err) | ||
|
||
expected := `export type MyType = number /* uint8 */; | ||
` | ||
assert.Equal(t, expected, tsCode) | ||
} | ||
|
||
func parseMarkdownFixtures(fileContents []byte) ([]MarkdownFixture, error) { | ||
fixtures := make([]MarkdownFixture, 0) | ||
currentFixture := MarkdownFixture{} | ||
|
||
currentBlockContents := "" | ||
currentBlockLanguage := "" | ||
inCodeBlock := false | ||
for _, line := range strings.Split(string(fileContents), "\n") { | ||
if strings.HasPrefix(line, "```") { | ||
if inCodeBlock { | ||
// End of code block | ||
if currentBlockLanguage == "ts" || currentBlockLanguage == "typescript" { | ||
// Every fixture ends with a typescript block | ||
currentFixture.TsCode = currentBlockContents | ||
fixtures = append(fixtures, currentFixture) | ||
currentFixture = MarkdownFixture{} | ||
} else if currentBlockLanguage == "go" { | ||
currentFixture.GoCode = currentBlockContents | ||
} else if currentBlockLanguage == "yml" || currentBlockLanguage == "yaml" { | ||
// Parse package config | ||
pc := PackageConfig{} | ||
err := yaml.Unmarshal([]byte(currentBlockContents), &pc) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal package config: %w", err) | ||
} | ||
currentFixture.PackageConfig = pc | ||
} | ||
currentBlockContents = "" | ||
currentBlockLanguage = "" | ||
} else { // Start of code block | ||
language := strings.TrimPrefix(line, "```") | ||
language = strings.TrimSpace(language) | ||
currentBlockLanguage = language | ||
} | ||
inCodeBlock = !inCodeBlock | ||
continue | ||
} | ||
|
||
if inCodeBlock { | ||
currentBlockContents += line + "\n" | ||
} | ||
} | ||
|
||
return fixtures, nil | ||
|
||
} | ||
|
||
// Tests all markdown files in `testdata/fixtures/` directory. | ||
func TestMarkdownFixtures(t *testing.T) { | ||
t.Parallel() | ||
|
||
fixtures, err := mdfs.ReadDir("testdata/fixtures") | ||
require.NoError(t, err) | ||
|
||
for _, fixture := range fixtures { | ||
fixture := fixture | ||
|
||
// Read markdown file | ||
md, err := mdfs.ReadFile("testdata/fixtures/" + fixture.Name()) | ||
require.NoError(t, err) | ||
|
||
testCases, err := parseMarkdownFixtures(md) | ||
require.NoError(t, err) | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(fixture.Name(), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tsCode, err := ConvertGoToTypescript(tc.GoCode, tc.PackageConfig) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, tc.TsCode, tsCode) | ||
}) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.