예제 #1
0
파일: tokens_test.go 프로젝트: katydid/v0.1
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
파일: error_test.go 프로젝트: katydid/v0.1
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
파일: inject_test.go 프로젝트: katydid/v0.1
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
파일: bench_test.go 프로젝트: katydid/v0.1
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
파일: error_test.go 프로젝트: katydid/v0.1
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)
	}
}