示例#1
0
func TestTokensSrcTree(t *testing.T) {
	fileDescriptorSet, err := protoparser.ParseFile("test.proto", ".")
	if err != nil {
		panic(err)
	}
	m, err := new("test", "SrcTree", fileDescriptorSet, nil)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", m.Dot())
}
示例#2
0
func TestCompileError(t *testing.T) {
	fileDescriptorSet, err := protoparser.ParseFile("person.proto", ".", gogoprotoImportPath)
	if err != nil {
		panic(err)
	}
	p := parser.NewParser()
	rules, err := p.ParseRules(lexer.NewLexer([]byte(compileErr)))
	if err != nil {
		t.Fatalf(err.Error())
	}
	protoTokens, err := tokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	_, _, err = compiler.Compile(rules, protoTokens)
	if err == nil {
		t.Fatalf("expected error")
	}
}
示例#3
0
func TestInject(t *testing.T) {
	fileDescriptorSet, err := protoparser.ParseFile("person.proto", ".", gogoprotoImportPath)
	if err != nil {
		panic(err)
	}
	p := parser.NewParser()
	rules, err := p.ParseRules(lexer.NewLexer([]byte(injectPerson)))
	if err != nil {
		t.Fatalf(err.Error())
	}
	protoTokens, err := tokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	e, rootToken, err := compiler.Compile(rules, protoTokens)
	if err != nil {
		panic(err)
	}
	typ := reflect.TypeOf((*InjectableInt64)(nil)).Elem()
	instances := inject.Implements(e, typ)
	for _, instance := range instances {
		instance.(InjectableInt64).SetValue(456)
	}
	m := robert
	data, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	s := scanner.NewProtoScanner(protoTokens, rootToken)
	err = s.Init(data)
	if err != nil {
		panic(err)
	}
	match, err := e.Eval(s)
	if err != nil {
		panic(err)
	}
	if !match {
		t.Fatalf("expected match")
	}
}
示例#4
0
func newBench(protoFilename string, katydidStr string) bench {
	fileDescriptorSet, err := protoparser.ParseFile(protoFilename, ".", gogoprotoImportPath)
	if err != nil {
		panic(err)
	}
	p := parser.NewParser()
	rules, err := p.ParseRules(lexer.NewLexer([]byte(katydidStr)))
	if err != nil {
		panic(err)
	}
	protoTokens, err := tokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	e, rootToken, err := compiler.Compile(rules, protoTokens)
	if err != nil {
		panic(err)
	}
	return bench{
		exec:    e,
		scanner: scanner.NewProtoScanner(protoTokens, rootToken),
	}
}
示例#5
0
func TestRuntimeError(t *testing.T) {
	fileDescriptorSet, err := protoparser.ParseFile("person.proto", ".", gogoprotoImportPath)
	if err != nil {
		panic(err)
	}
	p := parser.NewParser()
	rules, err := p.ParseRules(lexer.NewLexer([]byte(runtimeErr)))
	if err != nil {
		t.Fatalf(err.Error())
	}
	protoTokens, err := tokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	e, rootToken, err := compiler.Compile(rules, protoTokens)
	if err != nil {
		panic(err)
	}
	m := robert
	data, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	s := scanner.NewProtoScanner(protoTokens, rootToken)
	err = s.Init(data)
	if err != nil {
		panic(err)
	}
	match, err := e.Eval(s)
	if err == nil {
		t.Fatalf("expected error")
	}
	if match {
		t.Fatalf("expected match")
	}
}
示例#6
0
文件: test_test.go 项目: katydid/v0.1
func test(t *testing.T, protoFilename string, m proto.Message, katydidStr string, positive bool) {
	fileDescriptorSet, err := protoparser.ParseFile(protoFilename, ".", gogoprotoImportPath)
	if err != nil {
		panic(err)
	}
	p := parser.NewParser()
	rules, err := p.ParseRules(lexer.NewLexer([]byte(katydidStr)))
	if err != nil {
		t.Fatalf(err.Error())
	}
	outputStr := rules.String()
	if katydidStr != outputStr {
		t.Logf("input = <<%s>>", katydidStr)
		t.Logf("output = <<%s>>", outputStr)
		t.Fatalf("Parsed string should output same string from ast")
	}

	//Testing Query on Protocol Buffer Marshaled Structures
	protoTokens, err := tokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	e, rootToken, err := compiler.Compile(rules, protoTokens)
	if err != nil {
		panic(err)
	}
	data, err := proto.Marshal(m)
	if err != nil {
		panic(err)
	}
	s := scanner.NewProtoScanner(protoTokens, rootToken)
	err = s.Init(data)
	if err != nil {
		panic(err)
	}
	if match, err := e.Eval(s); err != nil {
		t.Errorf("Error: %v", err)
	} else if match != positive {
		t.Errorf("Expected a %v match from \n%v \non \n%v", positive, katydidStr, m)
	}

	//Testing Query on Json Marshaled Structures
	jsonTokens, err := jsontokens.NewZipped(rules, fileDescriptorSet)
	if err != nil {
		panic(err)
	}
	e, rootToken, err = compiler.Compile(rules, jsonTokens)
	if err != nil {
		panic(err)
	}
	jsonData, err := json.Marshal(m)
	if err != nil {
		panic(err)
	}
	jsonS := jsonscanner.NewScanner(jsonTokens, rootToken)
	err = jsonS.Init(jsonData)
	if err != nil {
		panic(err)
	}
	if match, err := e.Eval(jsonS); err != nil {
		t.Errorf("Error: %v", err)
	} else if match != positive {
		t.Errorf("Expected a %v match from \n%v \non \n%v", positive, katydidStr, m)
	}

	//Testing Query on Reflected Structures
	e, rootToken, err = compiler.Compile(rules, jsonTokens)
	if err != nil {
		panic(err)
	}
	if match, err := e.Eval(reflectscanner.NewScanner(jsonTokens, rootToken).Init(reflect.ValueOf(m))); err != nil {
		t.Errorf("Error: %v", err)
	} else if match != positive {
		t.Errorf("Expected a %v match from \n%v \non \n%v", positive, katydidStr, m)
	}
}