Skip to content

Commit

Permalink
TASK: code analysis tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-daehne committed Oct 7, 2023
1 parent db0f2b5 commit 89cfc5b
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
32 changes: 32 additions & 0 deletions analysis/codeAnalyzer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package analysis

import (
"regexp"
"testing"

"github.com/sandstorm/dependency-analysis/dataStructures"
)

// this test runs the code analysis against this project
// if you change the package structure, you need to adjust the expectations as well
func TestBuildDependencyGraph(t *testing.T) {
t.Run("analyze sda", func(t *testing.T) {
settings := &AnalyzerSettings{
SourcePath: "..",
Depth: 1,
IncludePattern: regexp.MustCompile(".*"),
}
actual, err := BuildDependencyGraph(settings)
AssertNil(t, err)
expected := dataStructures.NewDirectedStringGraph().
AddEdge("main", "cmd").
AddEdge("cmd", "analysis").
AddEdge("cmd", "dataStructures").
AddEdge("cmd", "rendering").
AddEdge("analysis", "parsing").
AddEdge("analysis", "dataStructures").
AddEdge("parsing", "dataStructures").
AddEdge("rendering", "dataStructuress")
AssertEquals(t, "incorrect graph", expected, actual)
})
}
11 changes: 10 additions & 1 deletion analysis/graphAnalyzer_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package analysis

import (
"github.com/sandstorm/dependency-analysis/dataStructures"
"reflect"
"testing"

"github.com/sandstorm/dependency-analysis/dataStructures"
)

func AssertEquals(t *testing.T, message string, expected interface{}, actual interface{}) {
Expand All @@ -16,6 +17,14 @@ func AssertEquals(t *testing.T, message string, expected interface{}, actual int
actual, reflect.TypeOf(actual))
}

func AssertNil(t *testing.T, actual interface{}) {
if actual == nil {
return
}
t.Errorf("expected nil, received %v (type %v)",
actual, reflect.TypeOf(actual))
}

func TestWeightByNumberOfDescendant(t *testing.T) {
testCases := []struct {
name string
Expand Down
10 changes: 10 additions & 0 deletions dataStructures/directedGraph.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dataStructures

import "fmt"

// directed graph with nodes of type string
type DirectedStringGraph struct {
// all nodes with their set of children (might be empty)
Expand Down Expand Up @@ -50,3 +52,11 @@ func (this *DirectedStringGraph) GetEdges() map[string][]string {
}
return result
}

func (this *DirectedStringGraph) String() string {
result := "{ "
for source, targets := range this.Edges {
result += fmt.Sprintf("%v -> %v ", source, targets)
}
return result + "}"
}
14 changes: 13 additions & 1 deletion dataStructures/stringSet.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dataStructures

import (
"fmt"
)

type StringSet struct {
content map[string]bool
}
Expand Down Expand Up @@ -31,9 +35,17 @@ func (this *StringSet) Remove(value string) {
func (this *StringSet) ToArray() []string {
result := make([]string, len(this.content))
var i = 0
for key, _ := range this.content {
for key := range this.content {
result[i] = key
i++
}
return result
}

func (this *StringSet) String() string {
result := "[ "
for key := range this.content {
result += fmt.Sprintf("%v ", key)
}
return result + "]"
}
2 changes: 1 addition & 1 deletion dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function test() {
# runs the dependency analysis against this project
function self_check() {
go run . validate .
go run . visualize .
go run . visualize --show-image=false .
}

# Builds the project locall and creates a release
Expand Down

0 comments on commit 89cfc5b

Please sign in to comment.