Skip to content

Commit

Permalink
convert to filetests
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Feb 28, 2025
1 parent 01cb064 commit e5d4563
Show file tree
Hide file tree
Showing 16 changed files with 176 additions and 218 deletions.
217 changes: 0 additions & 217 deletions gnovm/pkg/gnolang/gno_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,175 +130,6 @@ func TestBuiltinIdentifiersShadowing(t *testing.T) {
}
}

func TestConvertTo(t *testing.T) {
t.Parallel()

testFunc := func(source, msg string) {
defer func() {
if len(msg) == 0 {
return
}

r := recover()

if r == nil {
t.Fail()
}

err := r.(*PreprocessError)
c := strings.Contains(err.Error(), msg)
if !c {
t.Fatalf(`expected "%s", got "%s"`, msg, r)
}
}()

m := NewMachine("test", nil)

n := MustParseFile("main.go", source)
m.RunFiles(n)
m.RunMain()
}

type cases struct {
source string
msg string
}

tests := []cases{
{
`package test
func main() {
const a int = -1
println(uint(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to UintKind`,
},
{
`package test
func main() {
const a int = -1
println(uint8(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint8Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint16(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint16Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint32(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint32Kind`,
},
{
`package test
func main() {
const a int = -1
println(uint64(a))
}`,
`test/main.go:5:13: cannot convert constant of type IntKind to Uint64Kind`,
},
{
`package test
func main() {
const a float32 = 1.5
println(int32(a))
}`,
`test/main.go:5:13: cannot convert constant of type Float32Kind to Int32Kind`,
},
{
`package test
func main() {
println(int32(1.5))
}`,
`test/main.go:4:13: cannot convert (const (1.5 <untyped> bigdec)) to integer type`,
},
{
`package test
func main() {
const a float64 = 1.5
println(int64(a))
}`,
`test/main.go:5:13: cannot convert constant of type Float64Kind to Int64Kind`,
},
{
`package test
func main() {
println(int64(1.5))
}`,
`test/main.go:4:13: cannot convert (const (1.5 <untyped> bigdec)) to integer type`,
},
{
`package test
func main() {
const f = float64(1.0)
println(int64(f))
}`,
``,
},
{
`
package test
type Runner interface {
Run()
}
type Swimmer interface {
Swim()
}
func main() {
a := Runner(nil)
println(Swimmer(a))
}`, `test/main.go:14:10: test.Runner does not implement test.Swimmer (missing method Swim)`,
},
{
`
package test
type Writer interface {
Write([]byte) (int, error)
}
type Stringer interface {
String() string
}
func main() {
var x interface {
Writer
Stringer
}
var w Writer = Writer(x) // explicit conversion
println(w)
}
`,
``,
},
}

for _, tc := range tests {
testFunc(tc.source, tc.msg)
}
}

// run empty main().
func TestRunEmptyMain(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -460,54 +291,6 @@ func assertOutput(t *testing.T, input string, output string) {
assert.Nil(t, err)
}

func TestRunMakeStruct(t *testing.T) {
t.Parallel()

assertOutput(t, `package test
type Outfit struct {
Scarf string
Shirt string
Belts string
Strap string
Pants string
Socks string
Shoes string
}
func main() {
s := Outfit {
// some fields are out of order.
// some fields are left unset.
Scarf:"scarf",
Shirt:"shirt",
Shoes:"shoes",
Socks:"socks",
}
// some fields out of order are used.
// some fields left unset are used.
print(s.Shoes+","+s.Shirt+","+s.Pants+","+s.Scarf)
}`, `shoes,shirt,,scarf`)
}

func TestRunReturnStruct(t *testing.T) {
t.Parallel()

assertOutput(t, `package test
type MyStruct struct {
FieldA string
FieldB string
}
func myStruct(a, b string) MyStruct {
return MyStruct{
FieldA: a,
FieldB: b,
}
}
func main() {
s := myStruct("aaa", "bbb")
print(s.FieldA+","+s.FieldB)
}`, `aaa,bbb`)
}

// ----------------------------------------
// Benchmarks

Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ GNO_CASE:

validate := func(from Kind, to Kind, cmp func() bool) {
if isConst {
msg := fmt.Sprintf("cannot convert constant of type %s to %s\n", from, to)
msg := fmt.Sprintf("cannot convert constant of type %s to %s", from, to)
if cmp != nil && cmp() {
return
}
Expand Down
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a int = -1
println(uint(a))
}

// Error:
// main/files/convert6.gno:5:10: cannot convert constant of type IntKind to UintKind
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a int = -1
println(uint8(a))
}

// Error:
// main/files/convert6a.gno:5:10: cannot convert constant of type IntKind to Uint8Kind
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a int = -1
println(uint16(a))
}

// Error:
// main/files/convert6b.gno:5:10: cannot convert constant of type IntKind to Uint16Kind
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a int = -1
println(uint32(a))
}

// Error:
// main/files/convert6c.gno:5:10: cannot convert constant of type IntKind to Uint32Kind
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6d.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a int = -1
println(uint64(a))
}

// Error:
// main/files/convert6d.gno:5:10: cannot convert constant of type IntKind to Uint64Kind
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert6e.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a float32 = 1.5
println(int32(a))
}

// Error:
// main/files/convert6e.gno:5:10: cannot convert constant of type Float32Kind to Int32Kind
8 changes: 8 additions & 0 deletions gnovm/tests/files/convert7.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

func main() {
println(int32(1.5))
}

// Error:
// main/files/convert7.gno:4:10: cannot convert (const (1.5 <untyped> bigdec)) to integer type
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert7a.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const a float64 = 1.5
println(int64(a))
}

// Error:
// main/files/convert7a.gno:5:10: cannot convert constant of type Float64Kind to Int64Kind
8 changes: 8 additions & 0 deletions gnovm/tests/files/convert7b.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

func main() {
println(int64(1.5))
}

// Error:
// main/files/convert7b.gno:4:10: cannot convert (const (1.5 <untyped> bigdec)) to integer type
9 changes: 9 additions & 0 deletions gnovm/tests/files/convert7c.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
const f = float64(1.0)
println(int64(f))
}

// Output:
// 1
17 changes: 17 additions & 0 deletions gnovm/tests/files/interface47.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

type Runner interface {
Run()
}

type Swimmer interface {
Swim()
}

func main() {
a := Runner(nil)
println(Swimmer(a))
}

// Error:
// main/files/interface47.gno:13:10: main.Runner does not implement main.Swimmer (missing method Swim)
21 changes: 21 additions & 0 deletions gnovm/tests/files/interface48.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

type Writer interface {
Write([]byte) (int, error)
}

type Stringer interface {
String() string
}

func main() {
var x interface {
Writer
Stringer
}
var w Writer = Writer(x) // explicit conversion
println(w)
}

// Output:
// nil
Loading

0 comments on commit e5d4563

Please sign in to comment.