Skip to content

Commit

Permalink
feat: improve unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed May 21, 2024
1 parent 3c7b5ed commit 0148ad8
Showing 1 changed file with 37 additions and 43 deletions.
80 changes: 37 additions & 43 deletions gnovm/pkg/gnolang/preprocess_test.go
Original file line number Diff line number Diff line change
@@ -1,66 +1,60 @@
package gnolang

import (
"bytes"
"fmt"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestPrepocessBinaryExpressionPrimaryAndNative(t *testing.T) {
t.Parallel()
func TestPreprocess_BinaryExpressionOneNative(t *testing.T) {
pn := NewPackageNode("time", "time", nil)
pn.DefineGoNativeValue("Millisecond", time.Millisecond)
pn.DefineGoNativeValue("Second", time.Second)
pn.DefineGoNativeType(reflect.TypeOf(time.Duration(0)))
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

out := new(bytes.Buffer)
pkg := NewPackageNode("time", "time", nil)
pkg.DefineGoNativeValue("Millisecond", time.Millisecond)
pkg.DefineGoNativeValue("Second", time.Second)
pkg.DefineGoNativeType(reflect.TypeOf(time.Duration(0)))
pv := pkg.NewPackage()
store := gonativeTestStore(pkg, pv)

m := NewMachineWithOptions(MachineOptions{
PkgPath: "main",
Output: out,
Store: store,
})

c := `package main
const src = `package main
import "time"
func main() {
var a int64 = 2
println(time.Second * a)
}`
n := MustParseFile("main.go", c)
assert.Panics(t, func() { m.RunFiles(n) })
}

func TestPrepocessBinaryExpressionNativeAndNative(t *testing.T) {
t.Parallel()
out := new(bytes.Buffer)
pkg := NewPackageNode("time", "time", nil)
pkg.DefineGoNativeValue("March", time.March)
pkg.DefineGoNativeValue("Wednesday", time.Wednesday)
pkg.DefineGoNativeType(reflect.TypeOf(time.Month(0)))
pkg.DefineGoNativeType(reflect.TypeOf(time.Weekday(0)))
pv := pkg.NewPackage()
store := gonativeTestStore(pkg, pv)
}`
n := MustParseFile("main.go", src)

m := NewMachineWithOptions(MachineOptions{
PkgPath: "main",
Output: out,
Store: store,
})
defer func() {
err := recover()
assert.Contains(t, fmt.Sprint(err), "incompatible types in binary expression")
}()
Preprocess(store, pn, n)
}

c := `package main
func TestPreprocess_BinaryExpressionBothNative(t *testing.T) {
pn := NewPackageNode("time", "time", nil)
pn.DefineGoNativeValue("March", time.March)
pn.DefineGoNativeValue("Wednesday", time.Wednesday)
pn.DefineGoNativeType(reflect.TypeOf(time.Month(0)))
pn.DefineGoNativeType(reflect.TypeOf(time.Weekday(0)))
pv := pn.NewPackage()
store := gonativeTestStore(pn, pv)
store.SetBlockNode(pn)

const src = `package main
import "time"
func main() {
println(time.March * time.Wednesday)
}`
n := MustParseFile("main.go", c)
assert.Panics(t, func() { m.RunFiles(n) })
n := MustParseFile("main.go", src)

defer func() {
err := recover()
assert.Contains(t, fmt.Sprint(err), "incompatible types in binary expression")
}()
Preprocess(store, pn, n)
}

0 comments on commit 0148ad8

Please sign in to comment.