variables_test.go 7.5 KB
Newer Older
1
package proctl
2 3

import (
4
	"errors"
5
	"path/filepath"
E
epipho 已提交
6
	"sort"
7 8 9
	"testing"
)

E
epipho 已提交
10 11 12 13
type varTest struct {
	name    string
	value   string
	varType string
14
	err     error
E
epipho 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
}

func assertVariable(t *testing.T, variable *Variable, expected varTest) {
	if variable.Name != expected.name {
		t.Fatalf("Expected %s got %s\n", expected.name, variable.Name)
	}

	if variable.Type != expected.varType {
		t.Fatalf("Expected %s got %s\n", expected.varType, variable.Type)
	}

	if variable.Value != expected.value {
		t.Fatalf("Expected %#v got %#v\n", expected.value, variable.Value)
	}
}

31 32 33 34 35 36 37 38
func TestVariableEvaluation(t *testing.T) {
	executablePath := "../_fixtures/testvariables"

	fp, err := filepath.Abs(executablePath + ".go")
	if err != nil {
		t.Fatal(err)
	}

E
epipho 已提交
39
	testcases := []varTest{
40 41
		{"a1", "foofoofoofoofoofoo", "struct string", nil},
		{"a10", "ofo", "struct string", nil},
E
epipho 已提交
42
		{"a11", "[3]main.FooBar [{Baz: 1, Bur: a},{Baz: 2, Bur: b},{Baz: 3, Bur: c}]", "[3]main.FooBar", nil},
E
epipho 已提交
43 44
		{"a12", "[]main.FooBar len: 2, cap: 2, [{Baz: 4, Bur: d},{Baz: 5, Bur: e}]", "struct []main.FooBar", nil},
		{"a13", "[]*main.FooBar len: 3, cap: 3, [*{Baz: 6, Bur: f},*{Baz: 7, Bur: g},*{Baz: 8, Bur: h}]", "struct []*main.FooBar", nil},
45 46
		{"a2", "6", "int", nil},
		{"a3", "7.23", "float64", nil},
E
epipho 已提交
47
		{"a4", "[2]int [1,2]", "[2]int", nil},
E
epipho 已提交
48
		{"a5", "[]int len: 5, cap: 5, [1,2,3,4,5]", "struct []int", nil},
49 50 51 52 53 54 55 56 57 58 59 60 61
		{"a6", "main.FooBar {Baz: 8, Bur: word}", "main.FooBar", nil},
		{"a7", "*main.FooBar {Baz: 5, Bur: strum}", "*main.FooBar", nil},
		{"a8", "main.FooBar2 {Bur: 10, Baz: feh}", "main.FooBar2", nil},
		{"a9", "*main.FooBar nil", "*main.FooBar", nil},
		{"baz", "bazburzum", "struct string", nil},
		{"neg", "-1", "int", nil},
		{"f32", "1.2", "float32", nil},
		{"a6.Baz", "8", "int", nil},
		{"a7.Baz", "5", "int", nil},
		{"a8.Baz", "feh", "struct string", nil},
		{"a9.Baz", "nil", "int", errors.New("a9 is nil")},
		{"a9.NonExistent", "nil", "int", errors.New("a9 has no member NonExistent")},
		{"a8", "main.FooBar2 {Bur: 10, Baz: feh}", "main.FooBar2", nil}, // reread variable after member
E
epipho 已提交
62
		{"i32", "[2]int32 [1,2]", "[2]int32", nil},
E
epipho 已提交
63 64 65 66 67 68 69
		{"b1", "true", "bool", nil},
		{"b2", "false", "bool", nil}, {"i8", "1", "int8", nil},
		{"u16", "65535", "uint16", nil},
		{"u32", "4294967295", "uint32", nil},
		{"u64", "18446744073709551615", "uint64", nil},
		{"u8", "255", "uint8", nil},
		{"up", "5", "uintptr", nil},
70
		{"f", "main.barfoo", "func()", nil},
71 72
		{"ba", "[]int len: 200, cap: 200, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...+136 more]", "struct []int", nil},
		{"ms", "main.Nest {Level: 0, Nest: *main.Nest {Level: 1, Nest: *main.Nest {...}}}", "main.Nest", nil},
73
		{"NonExistent", "", "", errors.New("could not find symbol value for NonExistent")},
74 75
	}

76
	withTestProcess(executablePath, t, func(p *DebuggedProcess) {
77
		pc, _, _ := p.GoSymTable.LineToPC(fp, 57)
78

79
		_, err := p.Break(pc)
80 81 82 83 84 85 86
		assertNoError(err, t, "Break() returned an error")

		err = p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		for _, tc := range testcases {
			variable, err := p.EvalSymbol(tc.name)
87 88 89 90 91 92 93 94
			if tc.err == nil {
				assertNoError(err, t, "EvalSymbol() returned an error")
				assertVariable(t, variable, tc)
			} else {
				if tc.err.Error() != err.Error() {
					t.Fatalf("Unexpected error. Expected %s got %s", tc.err.Error(), err.Error())
				}
			}
E
epipho 已提交
95 96 97
		}
	})
}
98

E
epipho 已提交
99 100 101 102 103 104 105 106 107
func TestVariableFunctionScoping(t *testing.T) {
	executablePath := "../_fixtures/testvariables"

	fp, err := filepath.Abs(executablePath + ".go")
	if err != nil {
		t.Fatal(err)
	}

	withTestProcess(executablePath, t, func(p *DebuggedProcess) {
108
		pc, _, _ := p.GoSymTable.LineToPC(fp, 57)
E
epipho 已提交
109

110
		_, err := p.Break(pc)
E
epipho 已提交
111 112 113 114 115 116 117 118 119 120 121 122
		assertNoError(err, t, "Break() returned an error")

		err = p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		_, err = p.EvalSymbol("a1")
		assertNoError(err, t, "Unable to find variable a1")

		_, err = p.EvalSymbol("a2")
		assertNoError(err, t, "Unable to find variable a1")

		// Move scopes, a1 exists here by a2 does not
123
		pc, _, _ = p.GoSymTable.LineToPC(fp, 23)
E
epipho 已提交
124

125
		_, err = p.Break(pc)
E
epipho 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
		assertNoError(err, t, "Break() returned an error")

		err = p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		_, err = p.EvalSymbol("a1")
		assertNoError(err, t, "Unable to find variable a1")

		_, err = p.EvalSymbol("a2")
		if err == nil {
			t.Fatalf("Can eval out of scope variable a2")
		}
	})
}

type varArray []*Variable

// Len is part of sort.Interface.
func (s varArray) Len() int {
	return len(s)
}

// Swap is part of sort.Interface.
func (s varArray) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s varArray) Less(i, j int) bool {
	return s[i].Name < s[j].Name
}

func TestLocalVariables(t *testing.T) {
	executablePath := "../_fixtures/testvariables"

	fp, err := filepath.Abs(executablePath + ".go")
	if err != nil {
		t.Fatal(err)
	}

	testcases := []struct {
		fn     func(*ThreadContext) ([]*Variable, error)
		output []varTest
	}{
		{(*ThreadContext).LocalVariables,
			[]varTest{
172 173
				{"a1", "foofoofoofoofoofoo", "struct string", nil},
				{"a10", "ofo", "struct string", nil},
E
epipho 已提交
174
				{"a11", "[3]main.FooBar [{Baz: 1, Bur: a},{Baz: 2, Bur: b},{Baz: 3, Bur: c}]", "[3]main.FooBar", nil},
E
epipho 已提交
175 176
				{"a12", "[]main.FooBar len: 2, cap: 2, [{Baz: 4, Bur: d},{Baz: 5, Bur: e}]", "struct []main.FooBar", nil},
				{"a13", "[]*main.FooBar len: 3, cap: 3, [*{Baz: 6, Bur: f},*{Baz: 7, Bur: g},*{Baz: 8, Bur: h}]", "struct []*main.FooBar", nil},
177 178
				{"a2", "6", "int", nil},
				{"a3", "7.23", "float64", nil},
E
epipho 已提交
179
				{"a4", "[2]int [1,2]", "[2]int", nil},
E
epipho 已提交
180
				{"a5", "[]int len: 5, cap: 5, [1,2,3,4,5]", "struct []int", nil},
181 182 183 184
				{"a6", "main.FooBar {Baz: 8, Bur: word}", "main.FooBar", nil},
				{"a7", "*main.FooBar {Baz: 5, Bur: strum}", "*main.FooBar", nil},
				{"a8", "main.FooBar2 {Bur: 10, Baz: feh}", "main.FooBar2", nil},
				{"a9", "*main.FooBar nil", "*main.FooBar", nil},
E
epipho 已提交
185 186
				{"b1", "true", "bool", nil},
				{"b2", "false", "bool", nil},
187
				{"ba", "[]int len: 200, cap: 200, [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...+136 more]", "struct []int", nil},
188
				{"f", "main.barfoo", "func()", nil},
189
				{"f32", "1.2", "float32", nil},
E
epipho 已提交
190
				{"i32", "[2]int32 [1,2]", "[2]int32", nil},
191
				{"i8", "1", "int8", nil},
192
				{"ms", "main.Nest {Level: 0, Nest: *main.Nest {Level: 1, Nest: *main.Nest {...}}}", "main.Nest", nil},
E
epipho 已提交
193 194 195 196 197 198
				{"neg", "-1", "int", nil},
				{"u16", "65535", "uint16", nil},
				{"u32", "4294967295", "uint32", nil},
				{"u64", "18446744073709551615", "uint64", nil},
				{"u8", "255", "uint8", nil},
				{"up", "5", "uintptr", nil}}},
E
epipho 已提交
199 200
		{(*ThreadContext).FunctionArguments,
			[]varTest{
201 202
				{"bar", "main.FooBar {Baz: 10, Bur: lorem}", "main.FooBar", nil},
				{"baz", "bazburzum", "struct string", nil}}},
E
epipho 已提交
203 204 205
	}

	withTestProcess(executablePath, t, func(p *DebuggedProcess) {
206
		pc, _, _ := p.GoSymTable.LineToPC(fp, 57)
E
epipho 已提交
207

208
		_, err := p.Break(pc)
E
epipho 已提交
209 210 211 212 213 214 215 216 217 218
		assertNoError(err, t, "Break() returned an error")

		err = p.Continue()
		assertNoError(err, t, "Continue() returned an error")

		for _, tc := range testcases {
			vars, err := tc.fn(p.CurrentThread)
			assertNoError(err, t, "LocalVariables() returned an error")

			sort.Sort(varArray(vars))
219

E
epipho 已提交
220 221
			if len(tc.output) != len(vars) {
				t.Fatalf("Invalid variable count. Expected %d got %d.", len(tc.output), len(vars))
222 223
			}

E
epipho 已提交
224 225
			for i, variable := range vars {
				assertVariable(t, variable, tc.output[i])
226 227 228 229
			}
		}
	})
}