-
Notifications
You must be signed in to change notification settings - Fork 19
/
programs_test.go
56 lines (48 loc) · 1.24 KB
/
programs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2019 The Scriggo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scriggo
import (
"reflect"
"testing"
"github.com/open2b/scriggo/internal/compiler"
)
func TestInitPackageLevelVariables(t *testing.T) {
// Test no globals.
globals := initPackageLevelVariables([]compiler.Global{})
if globals != nil {
t.Fatalf("expected nil, got %v", globals)
}
// Test zero value.
global := compiler.Global{
Pkg: "p",
Name: "a",
Type: reflect.TypeOf(0),
}
globals = initPackageLevelVariables([]compiler.Global{global})
g := globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
if g.Interface() != 0 {
t.Fatalf("unexpected %v, expecting 0", g.Interface())
}
// Test pointer value in globals.
n := 1
global = compiler.Global{
Pkg: "p",
Name: "a",
Type: reflect.TypeOf(n),
Value: reflect.ValueOf(&n).Elem(),
}
globals = initPackageLevelVariables([]compiler.Global{global})
n = 2
g = globals[0]
if g.Kind() != reflect.Int {
t.Fatalf("unexpected kind %v", g.Kind())
}
iface := g.Interface()
if iface != n {
t.Fatalf("unexpected %v (type %T), expecting %d (type %T)", iface, iface, n, n)
}
}