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

cltest: spx #1887

Merged
merged 1 commit into from
May 27, 2024
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
106 changes: 106 additions & 0 deletions cl/cltest/spx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cltest

import (
"bytes"
"os"
"testing"

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
"github.com/goplus/mod/modfile"
)

func spxParserConf() parser.Config {
return parser.Config{
ClassKind: func(fname string) (isProj bool, ok bool) {
ext := modfile.ClassExt(fname)
c, ok := LookupClass(ext)
if ok {
isProj = c.IsProj(ext, fname)
}
return
},
}
}

func Spx(t *testing.T, gmx, spxcode, expected string) {
SpxEx(t, gmx, spxcode, expected, "index.tgmx", "bar.tspx")
}

func SpxEx(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile string) {
SpxWithConf(t, "gopSpxTest", Conf, gmx, spxcode, expected, gmxfile, spxfile, "")
}

func SpxEx2(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile, resultFile string) {
SpxWithConf(t, "gopSpxTest", Conf, gmx, spxcode, expected, gmxfile, spxfile, resultFile)
}

func SpxWithConf(t *testing.T, name string, conf *cl.Config, gmx, spxcode, expected, gmxfile, spxfile, resultFile string) {
t.Run(name, func(t *testing.T) {
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)

fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
if gmxfile == "" {
fs = memfs.SingleFile("/foo", spxfile, spxcode)
}
pkgs, err := parser.ParseFSDir(Conf.Fset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
bar := pkgs["main"]
pkg, err := cl.NewPackage("", bar, conf)
if err != nil {
t.Fatal("NewPackage:", err)
}
var b bytes.Buffer
err = pkg.WriteTo(&b, resultFile)
if err != nil {
t.Fatal("gogen.WriteTo failed:", err)
}
result := b.String()
if result != expected {
t.Fatalf("\nResult:\n%s\nExpected:\n%s\n", result, expected)
}
})
}

func SpxErrorEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string) {
fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
pkgs, err := parser.ParseFSDir(Conf.Fset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
conf := *Conf
conf.RelativeBase = "/foo"
conf.Recorder = nil
conf.NoFileLine = false
bar := pkgs["main"]
_, err = cl.NewPackage("", bar, &conf)
if err == nil {
t.Fatal("no error?")
}
if ret := err.Error(); ret != msg {
t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
}
}
73 changes: 5 additions & 68 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,93 +17,30 @@
package cl_test

import (
"bytes"
"os"
"testing"

"github.com/goplus/gop/cl"
"github.com/goplus/gop/cl/cltest"
"github.com/goplus/gop/parser"
"github.com/goplus/gop/parser/fsx/memfs"
"github.com/goplus/gop/scanner"
"github.com/goplus/mod/modfile"
)

func spxParserConf() parser.Config {
return parser.Config{
ClassKind: func(fname string) (isProj bool, ok bool) {
ext := modfile.ClassExt(fname)
c, ok := cltest.LookupClass(ext)
if ok {
isProj = c.IsProj(ext, fname)
}
return
},
}
}

func gopSpxTest(t *testing.T, gmx, spxcode, expected string) {
gopSpxTestEx(t, gmx, spxcode, expected, "index.tgmx", "bar.tspx")
cltest.SpxEx(t, gmx, spxcode, expected, "index.tgmx", "bar.tspx")
}

func gopSpxTestEx(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile string) {
gopSpxTestExConf(t, "gopSpxTest", cltest.Conf, gmx, spxcode, expected, gmxfile, spxfile, "")
cltest.SpxWithConf(t, "gopSpxTest", cltest.Conf, gmx, spxcode, expected, gmxfile, spxfile, "")
}

func gopSpxTestEx2(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile, resultFile string) {
gopSpxTestExConf(t, "gopSpxTest", cltest.Conf, gmx, spxcode, expected, gmxfile, spxfile, resultFile)
cltest.SpxWithConf(t, "gopSpxTest", cltest.Conf, gmx, spxcode, expected, gmxfile, spxfile, resultFile)
}

func gopSpxTestExConf(t *testing.T, name string, conf *cl.Config, gmx, spxcode, expected, gmxfile, spxfile, resultFile string) {
t.Run(name, func(t *testing.T) {
cl.SetDisableRecover(true)
defer cl.SetDisableRecover(false)

fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
if gmxfile == "" {
fs = memfs.SingleFile("/foo", spxfile, spxcode)
}
pkgs, err := parser.ParseFSDir(cltest.Conf.Fset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
bar := pkgs["main"]
pkg, err := cl.NewPackage("", bar, conf)
if err != nil {
t.Fatal("NewPackage:", err)
}
var b bytes.Buffer
err = pkg.WriteTo(&b, resultFile)
if err != nil {
t.Fatal("gogen.WriteTo failed:", err)
}
result := b.String()
if result != expected {
t.Fatalf("\nResult:\n%s\nExpected:\n%s\n", result, expected)
}
})
cltest.SpxWithConf(t, name, conf, gmx, spxcode, expected, gmxfile, spxfile, resultFile)
}

func gopSpxErrorTestEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string) {
fs := memfs.TwoFiles("/foo", spxfile, spxcode, gmxfile, gmx)
pkgs, err := parser.ParseFSDir(cltest.Conf.Fset, fs, "/foo", spxParserConf())
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatal("ParseFSDir:", err)
}
conf := *cltest.Conf
conf.RelativeBase = "/foo"
conf.Recorder = nil
conf.NoFileLine = false
bar := pkgs["main"]
_, err = cl.NewPackage("", bar, &conf)
if err == nil {
t.Fatal("no error?")
}
if ret := err.Error(); ret != msg {
t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
}
cltest.SpxErrorEx(t, msg, gmx, spxcode, gmxfile, spxfile)
}

func TestSpxNoGame(t *testing.T) {
Expand Down
Loading