Example #1
0
func Test_FileLexer(t *testing.T) {
	pgm := "public class foo{ private int val;" +
		" public int getVal() { return val; }" +
		" public foo(int val) { this.val = val; } }"

	rdr := NewStringReader(pgm)

	lx := NewLexer(rdr, false)
	testutil.AssertNotNil(t, lx, "Lexer should not be nil")
}
Example #2
0
func Test_TypeData_Primitive(t *testing.T) {
	allTypes := []string{
		"void", "boolean", "byte", "char", "short",
		"int", "long", "float", "double", "string",
	}

	for _, str := range allTypes {
		for dim := 0; dim < 5; dim++ {
			td := NewTypeDataPrimitive(str, dim)

			_, is_nil := td.TypeName()
			testutil.AssertEqual(t, dim == 0 && strings.EqualFold(str, "void"), is_nil,
				str, "should not be nil", is_nil)

			var expStr string
			switch str {
			case "boolean":
				expStr = "bool"
			case "short":
				expStr = "int16"
			case "long":
				expStr = "int64"
			case "float":
				expStr = "float32"
			case "double":
				expStr = "float64"
			default:
				expStr = str
			}
			for i := 1; i <= dim; i++ {
				expStr = "[]" + expStr
			}

			testutil.AssertEqual(t, td.String(), expStr, "Expected", expStr,
				"not", td.String())

			testutil.AssertFalse(t, td.isObject(), "Unexpected object", str)
		}
	}

	defer func() {
		if x := recover(); x != nil {
			testutil.AssertEqual(t, x, "Unrecognized primitive type xxx")
		}
	}()

	td := NewTypeDataPrimitive("xxx", 0)
	testutil.AssertNotNil(t, td, "Got nil TypeData for bad primitive type")
}
Example #3
0
func Test_GoProgram_Basic(t *testing.T) {
	name := "foo"
	gp := NewGoProgram(name, nil, false)

	gp.Analyze(nil)

	rcvr := gp.Receiver("foo")
	testutil.AssertEqual(t, rcvr, "rcvr", "Expected rcvr, not", rcvr)

	imap := gp.Imports()
	testutil.AssertNotNil(t, imap, "ImportMap() should not return nil")
	testutil.AssertEqual(t, len(imap), 0, "Did not expect ImportMap to contain", imap)

	decls := gp.Decls()
	testutil.AssertNotNil(t, decls, "Decls() should not return nil")
	testutil.AssertEqual(t, len(decls), 0, "Did not expect Decls to contain", decls)

	dout := &bytes.Buffer{}
	gp.Dump(dout)
	dstr := dout.String()
	expDstr := "package main\n"
	testutil.AssertEqual(t, dstr, expDstr, "Expected '", expDstr, "' not '", dstr, "'")

	file := gp.File()
	testutil.AssertNotNil(t, file, "File() should not return nil")
	testutil.AssertNotNil(t, file.Name, "File.Name should not be nil")
	testutil.AssertNotNil(t, file.Name.Name, "main",
		"File.Name.Name should be", "main", file.Name.Name)
	testutil.AssertNotNil(t, file.Decls, "File.Decls() should not return nil")
	testutil.AssertEqual(t, len(file.Decls), 0,
		"Did not expect File.Decls to contain", file.Decls)
	testutil.AssertNotNil(t, file.Imports, "File.Imports() should not return nil")
	testutil.AssertEqual(t, len(file.Imports), 0,
		"Did not expect File.Imports to contain", file.Imports)
	testutil.AssertEqual(t, len(file.Unresolved), 0,
		"File.Unresolved should be empty but contains", len(file.Unresolved),
		"entries")
	testutil.AssertEqual(t, len(file.Comments), 0,
		"File.Comments should be empty but contains", len(file.Comments),
		"entries")

	fs := gp.FileSet()
	fmt.Printf("FileSet -> %v\n", fs)

	sout := &bytes.Buffer{}
	gp.WriteString(sout)
	fmt.Printf("File -> %v\n", sout.String())

}
Example #4
0
func validate(t *testing.T, str string, dim int, td *TypeData,
	fd *FakeDictionary) {
	vt, is_nil := td.TypeName()
	testutil.AssertEqual(t, dim == 0 && strings.EqualFold(str, "void"), is_nil,
		str, "should not be nil", is_nil)
	if !is_nil {
		testutil.AssertNotNil(t, vt, str, "dim", dim, "is nil ::", vt)
	}

	expName := str
	if dim == 0 {
		expName = str
	} else {
		var star string
		if fd != nil && !fd.IsInterface(str) {
			star = "*"
		}
		expName = fmt.Sprintf("array_%s%s_dim%d", star, str, dim)
	}
	testutil.AssertEqual(t, expName, td.Name(), "Expected name", expName,
		"not", td.Name())

	var expStr string
	if fd == nil || fd.IsInterface(str) {
		expStr = str
	} else {
		expStr = "*" + str
	}
	for i := 1; i <= dim; i++ {
		expStr = "[]" + expStr
	}

	testutil.AssertEqual(t, expStr, td.String(), "Expected string ", expStr,
		"not", td.String())

	if fd != nil {
		if dim == 0 {
			testutil.AssertTrue(t, td.isObject(), "Expected object", str)
		} else {
			testutil.AssertFalse(t, td.isObject(), "Unexpected object", str)
		}
	}
}
Example #5
0
func Test_Main(t *testing.T) {
	src := "public class foo\n" +
		"{\n" +
		" private int val;\n" +
		" public int getVal() { return val; }\n" +
		" public foo(int val) { this.val = val; }\n" +
		" public static final void main(String[] args) {\n" +
		"  for (int i = 0; i < args.length; i++) {\n" +
		"   System.out.println(\"Arg#\" + i + \"=\" + args[i]);\n" +
		"   System.err.printf(\"Arg#%d=%s\\n\", i, args[i]);\n" +
		"  }\n" +
		" }\n" +
		"}\n"

	rdr := grammar.NewStringReader(src)

	lx := grammar.NewLexer(rdr, false)

	rtn := grammar.JulyParse(lx)
	testutil.AssertEqual(t, rtn, 0, "Expected", 0, "not", rtn)
	testutil.AssertNotNil(t, lx.JavaProgram(), "Parser did not return Java parse tree")

	pgm := NewGoProgram("", nil, false)
	pgm.Analyze(lx.JavaProgram())

	fmt.Println("========= PARSE TREE =============")
	pgm.WriteString(os.Stdout)
	fmt.Println()

	for _, rule := range StandardRules {
		pgm.RunTransform(rule, pgm, nil, nil)
	}

	fmt.Println("========= FINAL PROGRAM =============")
	pgm.Dump(os.Stdout)
}