Skip to content

Commit 117a0eb

Browse files
committed
x/format: TestClassSpx
1 parent d06ab69 commit 117a0eb

File tree

2 files changed

+116
-3
lines changed

2 files changed

+116
-3
lines changed

Diff for: x/format/gopclass_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package format_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/goplus/gop/x/format"
23+
)
24+
25+
func testClass(t *testing.T, name string, pkg string, class string, entry string, src, expect string) {
26+
t.Run(name, func(t *testing.T) {
27+
result, err := format.GopClassSource([]byte(src), pkg, class, entry, name)
28+
if err != nil {
29+
t.Fatal("format.GopClassSource failed:", err)
30+
}
31+
if ret := string(result); ret != expect {
32+
t.Fatalf("%s => Expect:\n%s\n=> Got:\n%s\n", name, expect, ret)
33+
}
34+
})
35+
}
36+
37+
func TestClassSpx(t *testing.T) {
38+
testClass(t, "spx class", "github.com/goplus/spx", "Calf", "Main", `package main
39+
40+
type Calf struct {
41+
spx.Sprite
42+
*Game
43+
index int
44+
info string
45+
}
46+
func (this *Calf) Update() {
47+
this.index++
48+
}
49+
func (this *Calf) Main() {
50+
this.OnStart(func() {
51+
this.Say("Hello Go+")
52+
})
53+
}
54+
func (this *Calf) Classfname() string {
55+
return "Calf"
56+
}
57+
`, `var (
58+
index int
59+
info string
60+
)
61+
62+
func Update() {
63+
index++
64+
}
65+
66+
onStart func() {
67+
say "Hello Go+"
68+
}
69+
`)
70+
}

Diff for: x/format/gopstyle.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,56 @@ func formatClass(file *ast.File, pkg string, class string, entry string) {
244244
classPkg: path.Base(pkg),
245245
className: class,
246246
}
247+
if file.Name.Name == "main" {
248+
file.NoPkgDecl = true
249+
}
250+
var fnEntry *ast.FuncDecl
251+
var decls []ast.Decl
252+
var varSpecs []ast.Spec
253+
for _, decl := range file.Decls {
254+
switch v := decl.(type) {
255+
case *ast.FuncDecl:
256+
if isClassFunc(v, class) {
257+
v.IsClass = true
258+
switch v.Name.Name {
259+
case entry:
260+
fnEntry = v
261+
file.ShadowEntry = v
262+
continue
263+
case "Classfname":
264+
v.Shadow = true
265+
}
266+
}
267+
case *ast.GenDecl:
268+
if v.Tok == token.TYPE {
269+
if spec, ok := v.Specs[0].(*ast.TypeSpec); ok && spec.Name.Name == class {
270+
if st, ok := spec.Type.(*ast.StructType); ok {
271+
for _, fs := range st.Fields.List {
272+
if len(fs.Names) == 0 {
273+
continue
274+
}
275+
varSpecs = append(varSpecs, &ast.ValueSpec{Names: fs.Names, Type: fs.Type})
276+
}
277+
continue
278+
}
279+
}
280+
}
281+
}
282+
decls = append(decls, decl)
283+
}
284+
if len(varSpecs) != 0 {
285+
decls = append([]ast.Decl{&ast.GenDecl{Tok: token.VAR, Specs: varSpecs}}, decls...)
286+
}
287+
if fnEntry != nil {
288+
decls = append(decls, fnEntry)
289+
}
290+
file.Decls = decls
291+
247292
for _, decl := range file.Decls {
248293
switch v := decl.(type) {
249294
case *ast.FuncDecl:
250295
// delay the process, because package level vars need to be processed first.
251296
funcs = append(funcs, v)
252-
if isClassFunc(v, class) && v.Name.Name == entry {
253-
file.ShadowEntry = v
254-
}
255297
case *ast.GenDecl:
256298
switch v.Tok {
257299
case token.IMPORT:
@@ -330,6 +372,7 @@ func funcRecv(v *ast.FuncDecl) *ast.Ident {
330372

331373
func formatFuncDecl(ctx *formatCtx, v *ast.FuncDecl) {
332374
if ctx.classMode && isClassFunc(v, ctx.className) {
375+
v.IsClass = true
333376
if recv := funcRecv(v); recv != nil {
334377
ctx.funcRecv = recv.Name
335378
defer func() {

0 commit comments

Comments
 (0)