Beispiel #1
0
func TestScopes(t *testing.T) {
	tests := []struct {
		in string
	}{
		{"../../clang/testdata/hello.cpp"},
		{"../../net/testdata/CompleteSharp.cs"},
	}
	for _, test := range tests {
		if d, err := ioutil.ReadFile(test.in); err != nil {
			t.Error(err)
		} else {
			var p SCOPES
			if !p.Parse(string(d)) {
				t.Errorf("Failed to parse: %s\n%v", p.Error(), p.RootNode())
				continue
			}
			ex := "testdata/" + filepath.Base(test.in) + ".exp"
			cmp := p.RootNode().String()
			if exp, err := ioutil.ReadFile(ex); err != nil {
				t.Logf("Couldn't read the expected output file %s (%s); it'll be created", ex, err)
				if err := ioutil.WriteFile(ex, []byte(cmp), 0644); err != nil {
					t.Error(err)
				}
			} else if d := util.Diff(string(exp), cmp); len(d) != 0 {
				t.Error(d)
			}
		}
	}
}
Beispiel #2
0
func TestLoadTheme(t *testing.T) {
	type Test struct {
		in  string
		out string
	}
	tests := []Test{
		{"testdata/Monokai.tmTheme", "testdata/Monokai.tmTheme.res"},
	}
	for _, test := range tests {
		if d, err := ioutil.ReadFile(test.in); err != nil {
			t.Logf("Couldn't load file %s: %s", test.in, err)
		} else {
			var theme Theme
			if err := loaders.LoadPlist(d, &theme); err != nil {
				t.Error(err)
			} else {
				str := fmt.Sprintf("%s", theme)
				if d, err := ioutil.ReadFile(test.out); err != nil {
					if err := ioutil.WriteFile(test.out, []byte(str), 0644); err != nil {
						t.Error(err)
					}
				} else if diff := util.Diff(string(d), str); diff != "" {
					t.Error(diff)
				}

			}
		}
	}
}
Beispiel #3
0
func TestParse(t *testing.T) {
	if f, err := os.Open(testdata); err != nil {
		t.Fatal(err)
	} else if fi, err := f.Readdir(-1); err != nil {
		t.Fatal(err)
	} else {
		for i := range fi {
			if n := testdata + fi[i].Name(); strings.HasSuffix(n, ".cs") {
				var p CSHARP
				if d, err := ioutil.ReadFile(n); err != nil {
					t.Error(err)
				} else if !p.Parse(string(d)) {
					t.Errorf("%s: %s", n, p.Error())
				} else {
					back := p.RootNode().Children[len(p.RootNode().Children)-1]
					if back.Name != "EndOfFile" {
						t.Log(p.RootNode())
						t.Errorf("Didn't finish parsing %s: %s", n, p.Error())
					}
					root := p.RootNode()
					root.Simplify()
					res := root.String()
					if exp, err := ioutil.ReadFile(n + ".pt"); err != nil {
						t.Logf("Expected result for parsing %s does not exist and will be created", n)
						ioutil.WriteFile(n+".pt", []byte(res), 0644)
					} else if d := util.Diff(string(exp), res); len(d) > 0 {
						t.Error(d)
					}
				}
			}
		}
	}
}
Beispiel #4
0
func TestParseResult(t *testing.T) {
	var tests = make(map[string][]string)
	if dir, err := os.Open("./testdata"); err != nil {
		t.Fatal(err)
	} else if fi, err := dir.Readdir(0); err != nil {
		t.Fatal(err)
	} else {
		for _, f := range fi {
			if strings.HasSuffix(f.Name(), ".in") {
				path := "./testdata/" + f.Name()
				if input, err := ioutil.ReadFile(path); err != nil {
					t.Fatal(err)
				} else {
					data, err := ioutil.ReadFile(path + ".res")
					if err != nil {
						t.Logf("Error reading test data: %s. Testdata will be created", err)
					}
					tests[path] = []string{string(input), string(data)}
				}
			}
		}
	}
	for k, data := range tests {
		if !strings.HasSuffix(k, "j.in") {
			continue
		}
		s := time.Now()
		input, expected := data[0], data[1]
		res := ""
		if cmp, err := parseresult(input); err != nil {
			t.Error(err)
			continue
		} else {
			res = fmt.Sprintf("%s", cmp)
		}
		e := time.Now()
		t.Logf("Parse %s %s", k, e.Sub(s))
		if len(expected) <= 1 {
			// Just if we want to add new tests, this will spit out the newly added
			// test data
			fn := k + ".res"
			t.Logf("Creating new test data: %s", fn)
			if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", fn, err)
			}
			continue
		}
		s = time.Now()
		if d := util.Diff(expected, res); len(d) != 0 {
			t.Error(d)
		}
		e = time.Now()

		t.Logf("Diff %s %s", k, e.Sub(s))
	}

}
Beispiel #5
0
func TestClangUnsaved(t *testing.T) {
	if skip {
		t.Skipf("Clang not installed, skipping")
	}
	var (
		a        content.CompleteAtArgs
		b        content.CompletionResult
		c        Clang
		expected string
	)
	a.Location.File.Name = "test.cpp"
	a.Location.File.Contents = `
class Test {
public:
	int A;
	int B;
	int C;
};

void main() {
	Test t;
	t.
}

`
	a.Location.Line = 11
	a.Location.Column = 4
	if err := c.CompleteAt(&a, &b); err == nil {
		// Need to provide -xc++ or similar, so it should complain about this if we didn't
		t.Error("Expected an error, but didn't get one")
	}
	a.SessionOverrides.Set("compiler_flags", []string{"-x", "c++", "-fno-exceptions"})
	if err := c.CompleteAt(&a, &b); err != nil {
		t.Errorf("Unexpected error: %s", err)
	} else if strings.Index(b.Messages, "void main") == -1 {
		t.Errorf("Expected an error about main returning void but got: \"%s\"", b.Messages)
	}
	res := fmt.Sprintf("%s", b)
	fn := "./testdata/unsaved.cpp.res"
	if data, err := ioutil.ReadFile(fn); err != nil {
		t.Logf("Error reading test data: %s. Testdata will be created", err)
	} else {
		expected = string(data)
	}
	if len(expected) <= 1 {
		// Just if we want to add new tests, this will spit out the newly added
		// test data
		t.Logf("Creating new test data: %s", fn)
		if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
			t.Errorf("Couldn't write test data to %s: %s", fn, err)
		}
	} else if d := util.Diff(expected, res); len(d) != 0 {
		t.Error(d)
	}

}
Beispiel #6
0
func TestNet(t *testing.T) {
	paths := DefaultPaths()
	if len(paths) == 0 {
		t.Skip("Neither mono nor Windows .NET Framework paths were possible to get")
	}

	var (
		n    Net
		args content.CompleteAtArgs
		cmp  content.CompletionResult
	)

	tests := []struct {
		InFile       string
		Line, Column uint
	}{
		// TODO: this test is not platform independent as it depends on whatever framework you happen
		//       to have installed
		{"./testdata/CompleteSharp.cs", 40, 27},
		{"./testdata/CompleteSharp.cs", 40, 41},
		{"./testdata/CompleteSharp.cs", 47, 47},
		//{"./testdata/CompleteSharp.cs", 28, 14},
		{"./testdata/CompleteSharp.cs", 211, 46},
		{"./testdata/CompleteSharp.cs", 761, 83},
		{"./testdata/CompleteSharp.cs", 761, 27},
		{"./testdata/CompleteSharp.cs", 857, 29},
		{"./testdata/CompleteSharp.cs", 737, 15},
		{"./testdata/CompleteSharp.cs", 95, 38},
		{"./testdata/CompleteSharp.cs", 95, 45},
		{"./testdata/CompleteSharp.cs", 776, 39},
		{"./testdata/NamespaceTest.cs", 4, 15},
		{"./testdata/NamespaceTest.cs", 6, 15},
	}
	args.SessionId = "a"
	args.Settings().Set("net_paths", []string{"./testdata/"})
	args.Settings().Set("net_assemblies", []string{"CompleteSharp.exe"})
	for _, test := range tests {
		args.Location.File.Name = test.InFile
		args.Location.Line = test.Line
		args.Location.Column = test.Column
		ex := fmt.Sprintf("%s-%d-%d.cmp", test.InFile, test.Line, test.Column)
		if err := n.CompleteAt(&args, &cmp); err != nil {
			t.Errorf("Unable to complete %v: %s", test, err)
		} else if exp, err := ioutil.ReadFile(ex); err != nil {
			t.Logf("Couldn't read the expected output file %s (%s); it'll be created", ex, err)
			if err := ioutil.WriteFile(ex, []byte(cmp.String()), 0644); err != nil {
				t.Error(err)
			}
		} else if d := util.Diff(string(exp), cmp.String()); len(d) != 0 {
			t.Error(d)
		}
	}
}
Beispiel #7
0
func TestClangUnsaved(t *testing.T) {
	if skip {
		t.Skipf("Clang not installed, skipping")
	}
	var (
		a        content.CompleteAtArgs
		b        content.CompletionResult
		c        Clang
		expected string
	)
	a.Location.File.Name = "test.cpp"
	a.Location.File.Contents = `
class Test {
public:
	int A;
	int B;
	int C;
};

void main() {
	Test t;
	t.
}

`
	a.Location.Line = 11
	a.Location.Column = 4
	a.SessionOverrides.Set("compiler_flags", []string{"-x", "c++", "-fno-exceptions"})
	if err := c.CompleteAt(&a, &b); err != nil {
		t.Error(err)
	}
	res := fmt.Sprintf("%s", b)
	fn := "./testdata/unsaved.cpp.res"
	if data, err := ioutil.ReadFile(fn); err != nil {
		t.Logf("Error reading test data: %s. Testdata will be created", err)
	} else {
		expected = string(data)
	}
	if len(expected) <= 1 {
		// Just if we want to add new tests, this will spit out the newly added
		// test data
		t.Logf("Creating new test data: %s", fn)
		if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
			t.Errorf("Couldn't write test data to %s: %s", fn, err)
		}
	} else if d := util.Diff(expected, res); len(d) != 0 {
		t.Error(d)
	}

}
Beispiel #8
0
// This is not 100% what ST3 does, but IMO ST3 is wrong
func TestScopeName(t *testing.T) {
	var (
		w Window
		v = w.NewFile()
	)
	const (
		in      = "textmate/testdata/main.go"
		expfile = "testdata/scopename.res"
		syntax  = "textmate/testdata/Go.tmLanguage"
	)
	v.Settings().Set("syntax", syntax)
	if d, err := ioutil.ReadFile(in); err != nil {
		t.Fatal(err)
	} else {
		//		v.rootNode = nil
		e := v.BeginEdit()
		v.Insert(e, 0, string(d))
		v.EndEdit(e)
		last := ""
		str := ""
		lasti := 0
		for v.ScopeName(1) == "" {
			time.Sleep(250 * time.Millisecond)
		}
		for i := 0; i < v.buffer.Size(); i++ {
			if name := v.ScopeName(i); name != last {
				if last != "" {
					str += fmt.Sprintf("%d-%d: %s\n", lasti, i, last)
					lasti = i
				}
				last = name
			}
		}
		if i := v.Buffer().Size(); lasti != i {
			str += fmt.Sprintf("%d-%d: %s\n", lasti, i, last)
		}
		if d, err := ioutil.ReadFile(expfile); err != nil {
			if err := ioutil.WriteFile(expfile, []byte(str), 0644); err != nil {
				t.Error(err)
			}
		} else if diff := util.Diff(string(d), str); diff != "" {
			t.Error(diff)
		}

	}
}
Beispiel #9
0
func TestLine(t *testing.T) {
	for _, test := range []string{"./testdata/8", "./testdata/hello", "./testdata/game.bz2", "./testdata/listener.o.bz2", "./testdata/completion.bz2", "./testdata/hello4"} {
		rf, err := readFile(test)
		if err != nil {
			t.Error(err)
			continue
		}
		f, err := newSectionReader(rf)
		if err != nil {
			t.Error(err)
			continue
		}
		defer f.Close()
		bri := f.Reader("debug_line")
		res := ""
		for {
			var v lineHeader
			if err := bri.ReadInterface(&v); err != nil {
				if err == io.EOF {
					break
				}
				t.Error(err)
			}
			res += fmt.Sprintf("%s\n", v)
		}
		expected := ""
		fn := test + ".line"
		if data, err := ioutil.ReadFile(fn); err == nil {
			expected = string(data)
		}
		if len(expected) <= 1 {
			t.Logf("Creating new test data: %s", fn)
			if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", fn, err)
			}
		} else if d := util.Diff(expected, res); len(d) != 0 {
			t.Error(d)
		}
	}
}
Beispiel #10
0
func TestString(t *testing.T) {
	if cmp, err := loadData(); err != nil {
		t.Fatal(err)
	} else {
		expfile := "./testdata/big.json.gz.in"
		expected := ""
		if d, err := ioutil.ReadFile(expfile); err != nil {
			t.Log("Expected result file doesn't exist and will be created")
		} else {
			expected = string(d)
		}
		res := cmp.String()
		if len(expected) <= 1 {
			t.Logf("Creating new test data: %s", expfile)
			if err := ioutil.WriteFile(expfile, []byte(res), 0644); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", expfile, err)
			}
		} else if d := util.Diff(expected, res); len(d) != 0 {
			t.Error(d)
		}
	}
}
Beispiel #11
0
// This is not 100% what ST3 does
func TestExtractScope(t *testing.T) {
	var (
		w Window
		v = w.NewFile()
	)
	const (
		in      = "textmate/testdata/main.go"
		expfile = "testdata/scoperange.res"
		syntax  = "textmate/testdata/Go.tmLanguage"
	)
	v.Settings().Set("syntax", syntax)
	if d, err := ioutil.ReadFile(in); err != nil {
		t.Fatal(err)
	} else {
		//		v.rootNode = nil
		e := v.BeginEdit()
		v.Insert(e, 0, string(d))
		v.EndEdit(e)
		last := Region{-1, -1}
		str := ""
		nr := Region{0, 0}
		for v.ExtractScope(1) == nr {
			time.Sleep(time.Millisecond)
		}
		for i := 0; i < v.buffer.Size(); i++ {
			if r := v.ExtractScope(i); r != last {
				str += fmt.Sprintf("%d (%d, %d)\n", i, r.A, r.B)
				last = r
			}
		}
		if d, err := ioutil.ReadFile(expfile); err != nil {
			if err := ioutil.WriteFile(expfile, []byte(str), 0644); err != nil {
				t.Error(err)
			}
		} else if diff := util.Diff(string(d), str); diff != "" {
			t.Error(diff)
		}
	}
}
Beispiel #12
0
func TestCompleteGame(t *testing.T) {
	testfile := "./testdata/game.bz2"
	tests := []string{
		"asCObjectType",
		"asCScriptFunction",
	}
	if r, err := readFile(testfile); err != nil {
		t.Error(err)
	} else if dh, err := NewDWARFHelper(testfile, r); err != nil {
		t.Error(err)
	} else {
		res := ""
		expected := ""
		if data, err := ioutil.ReadFile(testfile + ".res"); err == nil {
			expected = string(data)
		}

		for _, test := range tests {
			res += test + "\n"
			if cmp, err := dh.Complete(content.FullyQualifiedName{Relative: test}); err != nil {
				t.Error(err)
			} else {
				res += cmp.String()
			}
		}

		if len(expected) <= 1 {
			fn := testfile + ".res"
			t.Logf("Creating new test data: %s", fn)
			if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", fn, err)
			}
		} else if d := util.Diff(expected, res); len(d) != 0 {
			t.Error(d)
		}
	}
}
Beispiel #13
0
func TestSublime(t *testing.T) {
	ed := backend.GetEditor()
	ed.Console().Buffer().AddCallback(func(b primitives.Buffer, pos, delta int) {
		t.Logf("%s", b.Substr(primitives.Region{pos, pos + delta}))
	})
	w := ed.NewWindow()
	Init()
	l := py.NewLock()
	py.AddToPath("testdata")
	py.AddToPath("testdata/plugins")
	if m, err := py.Import("sublime_plugin"); err != nil {
		t.Fatal(err)
	} else {
		scanpath("testdata/", m)
	}

	subl, err := py.Import("sublime")
	if err != nil {
		t.Fatal(err)
	}

	if w, err := _windowClass.Alloc(1); err != nil {
		t.Fatal(err)
	} else {
		(w.(*Window)).data = &backend.Window{}
		subl.AddObject("test_window", w)
	}

	og, err := py.Import("objgraph")
	if err != nil {
		log4go.Debug(err)
		return
	}
	gr, err := og.Dict().GetItemString("show_growth")
	if err != nil {
		log4go.Debug(err)
		return
	}

	log4go.Debug("Before")
	gr.Base().CallFunctionObjArgs()

	if dir, err := os.Open("testdata"); err != nil {
		t.Error(err)
	} else if files, err := dir.Readdirnames(0); err != nil {
		t.Error(err)
	} else {
		for _, fn := range files {
			if filepath.Ext(fn) == ".py" {
				log4go.Debug("Running %s", fn)
				if _, err := py.Import(fn[:len(fn)-3]); err != nil {
					log4go.Error(err)
					t.Error(err)
				} else {
					log4go.Debug("Ran %s", fn)
				}
			}
		}
	}

	var f func(indent string, v py.Object, buf *bytes.Buffer)
	f = func(indent string, v py.Object, buf *bytes.Buffer) {
		b := v.Base()
		if dir, err := b.Dir(); err != nil {
			t.Error(err)
		} else {
			if l, ok := dir.(*py.List); ok {
				sl := l.Slice()

				if indent == "" {
					for _, v2 := range sl {
						if item, err := b.GetAttr(v2); err != nil {
							t.Error(err)
						} else {
							ty := item.Type()
							line := fmt.Sprintf("%s%s\n", indent, v2)
							buf.WriteString(line)
							if ty == py.TypeType {
								f(indent+"\t", item, buf)
							}
							item.Decref()
						}
					}
				} else {
					for _, v2 := range sl {
						buf.WriteString(fmt.Sprintf("%s%s\n", indent, v2))
					}
				}

			} else {
				ty := dir.Type()
				t.Error("Unexpected type:", ty)
			}
			dir.Decref()
		}
	}
	buf := bytes.NewBuffer(nil)
	f("", subl, buf)

	l.Unlock()

	const expfile = "testdata/api.txt"
	if d, err := ioutil.ReadFile(expfile); err != nil {
		if err := ioutil.WriteFile(expfile, buf.Bytes(), 0644); err != nil {
			t.Error(err)
		}
	} else if diff := util.Diff(string(d), buf.String()); diff != "" {
		t.Error(diff)
	}
	ed.LogCommands(true)
	tests := []string{
		"state",
		"registers",
		"settings",
		"constants",
		"registers",
		"cmd_data",
		"marks",
	}

	for _, test := range tests {
		ed.CommandHandler().RunWindowCommand(w, "vintage_ex_run_data_file_based_tests", backend.Args{"suite_name": test})
	}
	for _, w := range ed.Windows() {
		for _, v := range w.Views() {
			if strings.HasSuffix(v.Buffer().FileName(), "sample.txt") {
				continue
			}
			if strings.Index(v.Buffer().Substr(primitives.Region{0, v.Buffer().Size()}), "FAILED") != -1 {
				t.Error(v.Buffer())
			}
		}
	}
	log4go.Debug("After")
	l.Lock()
	gr.Base().CallFunctionObjArgs()
	l.Unlock()

	var v *backend.View
	for _, v2 := range w.Views() {
		if v == nil || v2.Buffer().Size() > v.Buffer().Size() {
			v = v2
		}
	}

	// log4go.Debug("Before")
	// gr.Base().CallFunctionObjArgs()
	// for i := 0; i < 500; i++ {
	// 	ed.CommandHandler().RunTextCommand(v, "set_motion", backend.Args{"motion": "vi_j"})
	// }
	// for i := 0; i < 500; i++ {
	// 	ed.CommandHandler().RunTextCommand(v, "set_motion", backend.Args{"motion": "vi_k"})
	// }

	// log4go.Debug("After")
	// gr.Base().CallFunctionObjArgs()

}
Beispiel #14
0
func TestLoadAssembly(t *testing.T) {
	var (
		tests = make(map[string]string)
		err   error
	)

	testdata, err := os.Open(testdata_path)
	if err != nil {
		t.Fatalf("Failed to open %s: %s", testdata_path, err)
	}
	defer testdata.Close()

	fi, err := testdata.Readdir(0)
	if err != nil {
		t.Errorf("Failed to fully read %s: %s. Got %d useable entries.", testdata_path, err, len(fi))
	}

	for i := range fi {
		if strings.HasSuffix(fi[i].Name(), ".exe") || strings.HasSuffix(fi[i].Name(), ".dll") {
			path := testdata_path + fi[i].Name()
			data, err := ioutil.ReadFile(path + ".asm")
			if err != nil {
				t.Logf("Error reading test data: %s. Testdata will be created", err)
			}
			tests[path] = string(data)
		}
	}

	for k, v := range tests {
		t.Log("Testing", k)
		f, err := os.Open(k)
		if err != nil {
			t.Error(err)
			continue
		}
		defer f.Close()
		if asm, err := LoadAssembly(f); err != nil {
			t.Error(err)
			continue
		} else {
			var res string
			for i := range asm.Tables {
				if asm.Tables[i].Rows == 0 {
					continue
				}
				td := asm.Tables[i]
				res += fmt.Sprintln(td.RowType.Name())
				row := reflect.New(td.RowType).Interface()
				for i := uint32(0); i < td.Rows; i++ {
					data, err := td.Index(i + 1)
					if err != nil {
						t.Error(err)
						continue
					}
					if err := asm.Create(&binary.BinaryReader{Reader: bytes.NewReader(data), Endianess: binary.LittleEndian}, row); err != nil {
						t.Error(err)
					} else {
						res += fmt.Sprintf("\t%+v\n", row)
					}
				}
			}

			if types, err := asm.Types(); err != nil {
				t.Error(err)
			} else {
				for i := range types {
					res += fmt.Sprintf("\t%s\n", types[i])
				}
			}
			var idx = &ConcreteTableIndex{metadataUtil: &asm.MetadataUtil, index: 0, table: id_TypeDef}
			tdTable := asm.Tables[id_TypeDef]
			//ty := reflect.New(td.RowType).Interface().(*TypeDefRow)
			for i := uint32(0); i < tdTable.Rows; i++ {
				idx.index = i + 1
				td, err := TypeDefFromIndex(idx)
				if err != nil {
					t.Error(err)
					continue
				} else if n := td.Name(); check(&n, n) != nil {
					continue
				}

				if ct, err := td.ToContentType(); err != nil {
					t.Error(err)
					continue
				} else {
					res += fmt.Sprintln(ct)
				}
			}

			if len(v) <= 1 {
				// Just if we want to add new tests, this will spit out the newly added
				// test data
				fn := k + ".asm"
				t.Logf("Creating new test data: %s", fn)
				if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
					t.Errorf("Couldn't write test data to %s: %s", fn, err)
				}
				continue
			}

			if d := util.Diff(v, res); len(d) != 0 {
				t.Error(d)
			}
		}
	}
}
Beispiel #15
0
func TestInfo(t *testing.T) {
	for _, test := range []string{"./testdata/8", "./testdata/hello", "./testdata/game.bz2", "./testdata/listener.o.bz2", "./testdata/completion.bz2", "./testdata/hello4"} {
		t.Logf("\n%s", test)
		rf, err := readFile(test)
		if err != nil {
			t.Error(err)
			continue
		}
		f, err := newSectionReader(rf)
		if err != nil {
			t.Error(err)
			continue
		}
		defer f.Close()
		bri := f.Reader("debug_info")
		bra := f.Reader("debug_abbrev")
		brs := f.Reader("debug_str")
		var ih InfoHeader
		nextHeader, _ := bri.Seek(0, 1)
		var abbr_entries []AbbrevEntry
		buf := bytes.NewBuffer(nil)
		var indent = ""
		for {
			off, _ := bri.Seek(0, 1)
			if off >= nextHeader {
				if err := bri.ReadInterface(&ih); err != nil {
					if err != io.EOF {
						t.Error(err)
					}
					break
				} else {
					buf.WriteString(fmt.Sprintf("%+v\n", ih))
					nextHeader += int64(ih.Length) + 4
					abbr_entries = nil
					bra.Seek(ih.DebugAbbrevOffset, 0)
					for {
						var abr AbbrevEntry
						if err := bra.ReadInterface(&abr); err != nil {
							break
						} else {
							abbr_entries = append(abbr_entries, abr)
						}
					}
				}
			}
			var (
				ie InfoEntry
			)
			if err := bri.ReadInterface(&ie); err != nil {
				if err == io.EOF {
					break
				}
				t.Error(err)
				break
			}

			ie.header = &ih
			if ie.id <= 0 {
				indent = indent[:len(indent)-2]
				continue
			}
			//			buf.WriteString(fmt.Sprintf("%v, %d\n", ie.id, len(abbr_entries)))
			ie.ae = &abbr_entries[ie.id-1]
			ie.reader.Info = bri
			ie.reader.Abbrev = bra
			ie.reader.Str = brs
			buf.WriteString(fmt.Sprintf("%s%s [%d] ", indent, ie.Tag().String(), ie.id))
			if ie.ae.Children == DW_CHILDREN_yes {
				buf.WriteRune('*')
			}
			buf.WriteRune('\n')
			indent += "  "
			for _, attr := range ie.ae.Attributes {
				pos, _ := bri.Seek(0, 1)
				va := ie.Attribute(attr.Name)
				switch v := va.(type) {
				case uint64:
					buf.WriteString(fmt.Sprintf("%s0x%08x %s 0x%08x\n", indent, pos, attr, v))
				default:
					buf.WriteString(fmt.Sprintf("%s0x%08x %s %v\n", indent, pos, attr, v))
				}
			}

			if ie.ae.Children == DW_CHILDREN_no {
				indent = indent[:len(indent)-2]
			}
			buf.WriteRune('\n')
			off, _ = bri.Seek(0, 1)
			//			buf.WriteString(fmt.Sprintf("0x%x 0x%x", off, nextHeader))
		}
		res := buf.String()
		expected := ""
		fn := test + ".info"
		if data, err := ioutil.ReadFile(fn); err == nil {
			expected = string(data)
		}
		if len(expected) <= 1 {
			t.Logf("Creating new test data: %s", fn)
			if err := ioutil.WriteFile(fn, []byte(res), 0644); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", fn, err)
			}
		} else if d := util.Diff(expected, res); len(d) != 0 {
			t.Error(d)
		}
	}
}
Beispiel #16
0
func TestTmLanguage(t *testing.T) {
	files := []string{
		"testdata/Property List (XML).tmLanguage",
		"testdata/XML.plist",
		"testdata/Go.tmLanguage",
	}
	for _, fn := range files {
		if _, err := Provider.LanguageFromFile(fn); err != nil {
			t.Fatal(err)
		}
	}

	type test struct {
		in  string
		out string
		syn string
	}
	tests := []test{
		{
			"testdata/plist2.tmlang",
			"testdata/plist2.tmlang.res",
			"text.xml.plist",
		},
		{
			"testdata/Property List (XML).tmLanguage",
			"testdata/plist.tmlang.res",
			"text.xml.plist",
		},
		{
			"testdata/main.go",
			"testdata/main.go.res",
			"source.go",
		},
		{
			"testdata/go2.go",
			"testdata/go2.go.res",
			"source.go",
		},
		{
			"testdata/utf.go",
			"testdata/utf.go.res",
			"source.go",
		},
	}
	for _, t3 := range tests {

		var d0 string
		if d, err := ioutil.ReadFile(t3.in); err != nil {
			t.Errorf("Couldn't load file %s: %s", t3.in, err)
			continue
		} else {
			d0 = string(d)
		}

		if lp, err := NewLanguageParser(t3.syn, d0); err != nil {
			t.Error(err)
		} else if root, err := lp.Parse(); err != nil {
			t.Error(err)
		} else {
			//		fmt.Println(lp.RootNode())
			str := fmt.Sprintf("%s", root)
			if d, err := ioutil.ReadFile(t3.out); err != nil {
				if err := ioutil.WriteFile(t3.out, []byte(str), 0644); err != nil {
					t.Error(err)
				}
			} else if diff := util.Diff(string(d), str); diff != "" {
				t.Error(diff)
			}
		}
	}
}
Beispiel #17
0
func TestSpecificClasses(t *testing.T) {
	var (
		tests = make(map[Classname]string)
		err   error
	)

	classpath, err := DefaultClasspath()
	if err != nil {
		t.Skip(err)
	}

	c, err := NewCompositeArchive(classpath)
	if c == nil {
		t.Fatal(err)
	} else if err != nil {
		t.Log(err)
	}
	defer c.Close()

	testdata, err := os.Open(testdata_path)
	if err != nil {
		t.Fatalf("Failed to open %s: %s", testdata_path, err)
	}
	defer testdata.Close()

	fi, err := testdata.Readdir(0)
	if err != nil {
		t.Errorf("Failed to fully read %s: %s. Got %d useable entries.", testdata_path, err, len(fi))
	}

	for i := range fi {
		if strings.HasSuffix(fi[i].Name(), ".class") {
			if data, err := ioutil.ReadFile(testdata_path + fi[i].Name()); err != nil {
				t.Errorf("Error reading test data: %s", err)
			} else {
				tests[Filename(strings.Replace(fi[i].Name(), "_", "/", -1)).Classname()] = string(data)
			}
		}
	}

	for k, v := range tests {
		t.Log("Testing", k)

		d, err := c.LoadClass(k)
		if err != nil {
			t.Errorf("Failed to read file contents: %s", err)
			continue
		}

		b := bytes.NewReader(d)
		c, err := NewClass(b)
		if err != nil {
			t.Errorf("Failed to create Class struct: %s", err)
			continue
		}

		ret := c.String()
		if ct, err := c.ToContentType(); err != nil {
			t.Error(err)
		} else {
			ret += fmt.Sprintf("%s\n", ct)
		}

		if len(v) <= 1 {
			// Just if we want to add new tests, this will spit out the newly added
			// test data
			fn := testdata_path + strings.Replace(string(k.Filename()), "/", "_", -1)
			t.Logf("Creating new test data: %s", fn)
			if err := ioutil.WriteFile(fn, []byte(ret), 0666); err != nil {
				t.Errorf("Couldn't write test data to %s: %s", fn, err)
			}
			continue
		}

		if d := util.Diff(v, ret); len(d) != 0 {
			t.Error(d)
		}
	}
}