Skip to content

Commit

Permalink
Merge pull request #1887 from xushiwei/q
Browse files Browse the repository at this point in the history
cltest: spx
  • Loading branch information
xushiwei authored May 27, 2024
2 parents 30e1a64 + 8423bbd commit 9ba10ac
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 68 deletions.
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

0 comments on commit 9ba10ac

Please sign in to comment.