|
| 1 | +package gnolang |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestPrepocessBinaryExpressionPrimaryAndNative(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + out := new(bytes.Buffer) |
| 16 | + pkg := NewPackageNode("time", "time", nil) |
| 17 | + pkg.DefineGoNativeValue("Millisecond", time.Millisecond) |
| 18 | + pkg.DefineGoNativeValue("Second", time.Second) |
| 19 | + pkg.DefineGoNativeType(reflect.TypeOf(time.Duration(0))) |
| 20 | + pv := pkg.NewPackage() |
| 21 | + store := gonativeTestStore(pkg, pv) |
| 22 | + |
| 23 | + m := NewMachineWithOptions(MachineOptions{ |
| 24 | + PkgPath: "main", |
| 25 | + Output: out, |
| 26 | + Store: store, |
| 27 | + }) |
| 28 | + |
| 29 | + c := `package main |
| 30 | + import "time" |
| 31 | +func main() { |
| 32 | + var a int64 = 2 |
| 33 | + println(a * time.Second) |
| 34 | + |
| 35 | +}` |
| 36 | + n := MustParseFile("main.go", c) |
| 37 | + assert.Panics(t, func() { m.RunFiles(n) }, "should panic: invalid operation: int64 * time.Duration") |
| 38 | +} |
| 39 | + |
| 40 | +func TestPrepocessBinaryExpressionNativeAndNative(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + |
| 43 | + out := new(bytes.Buffer) |
| 44 | + pkg := NewPackageNode("time", "time", nil) |
| 45 | + pkg.DefineGoNativeValue("March", time.March) |
| 46 | + pkg.DefineGoNativeValue("Wednesday", time.Wednesday) |
| 47 | + pkg.DefineGoNativeType(reflect.TypeOf(time.Month(0))) |
| 48 | + pkg.DefineGoNativeType(reflect.TypeOf(time.Weekday(0))) |
| 49 | + pv := pkg.NewPackage() |
| 50 | + store := gonativeTestStore(pkg, pv) |
| 51 | + |
| 52 | + m := NewMachineWithOptions(MachineOptions{ |
| 53 | + PkgPath: "main", |
| 54 | + Output: out, |
| 55 | + Store: store, |
| 56 | + }) |
| 57 | + |
| 58 | + c := `package main |
| 59 | + import "time" |
| 60 | +func main() { |
| 61 | + println(time.March * time.Wednesday) |
| 62 | + |
| 63 | +}` |
| 64 | + n := MustParseFile("main.go", c) |
| 65 | + assert.Panics(t, func() { m.RunFiles(n) }, "should panic: invalid operation: time.Month * time.Weekday") |
| 66 | +} |
0 commit comments