Exemplo n.º 1
0
func TestGraphs(t *testing.T) {
	for _, test := range typeGraphTests {
		if os.Getenv("FILTER") != "" && !strings.Contains(test.name, os.Getenv("FILTER")) {
			continue
		}

		fmt.Printf("Running test: %v\n", test.name)

		graph, err := compilergraph.NewGraph("tests/" + test.input + "/" + test.entrypoint + ".seru")
		if err != nil {
			t.Errorf("Got error on test %s: %v", test.name, err)
		}

		testSRG := srg.NewSRG(graph)
		testIDL := webidl.NewIRG(graph)

		loader := packageloader.NewPackageLoader(graph.RootSourceFilePath, []string{}, testSRG.PackageLoaderHandler(), testIDL.PackageLoaderHandler())
		srgResult := loader.Load(packageloader.Library{TESTLIB_PATH, false, ""})

		// Make sure we had no errors during construction.
		if !assert.True(t, srgResult.Status, "Got error for SRG construction %v: %s", test.name, srgResult.Errors) {
			continue
		}

		// Construct the type graph.
		result := typegraph.BuildTypeGraph(testSRG.Graph, GetConstructor(testSRG), webidltc.GetConstructor(testIDL))

		if test.expectedError == "" {
			// Make sure we had no errors during construction.
			if !assert.True(t, result.Status, "Got error for type graph construction %v: %s", test.name, result.Errors) {
				continue
			}

			currentLayerView := result.Graph.GetFilteredJSONForm(graph.RootSourceFilePath)

			if os.Getenv("REGEN") == "true" {
				test.writeJson(currentLayerView)
			} else {
				// Compare the constructed graph layer to the expected.
				expectedLayerView := test.json()
				assert.Equal(t, expectedLayerView, currentLayerView, "Graph view mismatch on test %s\nExpected: %v\nActual: %v\n\n", test.name, expectedLayerView, currentLayerView)
			}
		} else {
			// Make sure we had an error during construction.
			if !assert.False(t, result.Status, "Found no error for type graph construction %v: %s", test.name, result.Errors) {
				continue
			}

			// Make sure the error expected is found.
			assert.Equal(t, 1, len(result.Errors), "In test %v: Expected one error, found: %v", test.name, result.Errors)
			assert.Equal(t, test.expectedError, result.Errors[0].Error(), "Error mismatch on test %v", test.name)
		}
	}
}
Exemplo n.º 2
0
// ParseAndBuildScopeGraph conducts full parsing and type graph construction for the project
// starting at the given root source file.
func ParseAndBuildScopeGraph(rootSourceFilePath string, vcsDevelopmentDirectories []string, libraries ...packageloader.Library) Result {
	graph, err := compilergraph.NewGraph(rootSourceFilePath)
	if err != nil {
		log.Fatalf("Could not instantiate graph: %v", err)
	}

	// Create the SRG for the source and load it.
	sourcegraph := srg.NewSRG(graph)
	webidlgraph := webidl.NewIRG(graph)

	loader := packageloader.NewPackageLoader(rootSourceFilePath, vcsDevelopmentDirectories, sourcegraph.PackageLoaderHandler(), webidlgraph.PackageLoaderHandler())
	loaderResult := loader.Load(libraries...)
	if !loaderResult.Status {
		return Result{
			Status:   false,
			Errors:   loaderResult.Errors,
			Warnings: loaderResult.Warnings,
		}
	}

	// Construct the type graph.
	resolver := typerefresolver.NewResolver(sourcegraph)
	srgConstructor := srgtc.GetConstructorWithResolver(sourcegraph, resolver)
	typeResult := typegraph.BuildTypeGraph(sourcegraph.Graph, srgConstructor, webidltc.GetConstructor(webidlgraph))
	if !typeResult.Status {
		return Result{
			Status:   false,
			Errors:   typeResult.Errors,
			Warnings: combineWarnings(loaderResult.Warnings, typeResult.Warnings),
		}
	}

	// Freeze the resolver's cache.
	resolver.FreezeCache()

	// Construct the scope graph.
	scopeResult := buildScopeGraphWithResolver(sourcegraph, webidlgraph, typeResult.Graph, resolver)
	return Result{
		Status:   scopeResult.Status,
		Errors:   scopeResult.Errors,
		Warnings: combineWarnings(loaderResult.Warnings, typeResult.Warnings, scopeResult.Warnings),
		Graph:    scopeResult.Graph,
	}
}
Exemplo n.º 3
0
func TestLookupReturnType(t *testing.T) {
	graph, err := compilergraph.NewGraph("tests/returntype/returntype.seru")
	if !assert.Nil(t, err, "Got graph creation error: %v", err) {
		return
	}

	testSRG := srg.NewSRG(graph)
	testIDL := webidl.NewIRG(graph)

	loader := packageloader.NewPackageLoader(graph.RootSourceFilePath, []string{}, testSRG.PackageLoaderHandler(), testIDL.PackageLoaderHandler())

	srgResult := loader.Load(packageloader.Library{TESTLIB_PATH, false, ""})
	if !assert.True(t, srgResult.Status, "Got error for SRG construction: %v", srgResult.Errors) {
		return
	}

	// Construct the type graph.
	result := typegraph.BuildTypeGraph(testSRG.Graph, GetConstructor(testSRG), webidltc.GetConstructor(testIDL))
	if !assert.True(t, result.Status, "Got error for TypeGraph construction: %v", result.Errors) {
		return
	}

	// Ensure that the function and the property getter have return types.
	module, found := testSRG.FindModuleBySource(compilercommon.InputSource("tests/returntype/returntype.seru"))
	if !assert.True(t, found, "Could not find source module") {
		return
	}

	resolvedclass, foundClass := module.ResolveTypePath("SomeClass")
	if !assert.True(t, foundClass, "Could not find SomeClass") {
		return
	}

	someclass := resolvedclass.ResolvedType.AsType()

	// Check the function.
	dosomethingFunc, foundFunc := someclass.FindMember("DoSomething")
	if !assert.True(t, foundFunc, "Could not find DoSomething") {
		return
	}

	dosomethingFuncReturnType, hasReturnType := result.Graph.LookupReturnType(dosomethingFunc.Node())
	if !assert.True(t, hasReturnType, "Could not find return type for DoSomething") {
		return
	}

	assert.Equal(t, "Integer", dosomethingFuncReturnType.String(), "Expected int for DoSomething return type, found: %v", dosomethingFuncReturnType)

	// Check the property getter.
	someProp, foundProp := someclass.FindMember("SomeProp")
	if !assert.True(t, foundProp, "Could not find SomeProp") {
		return
	}

	getter, _ := someProp.Getter()
	somePropReturnType, hasPropReturnType := result.Graph.LookupReturnType(getter.GraphNode)
	if !assert.True(t, hasPropReturnType, "Could not find return type for SomeProp getter") {
		return
	}

	assert.Equal(t, "SomeClass", somePropReturnType.String(), "Expected SomeClass for SomeProp return type, found: %v", somePropReturnType)
}