Skip to content

Commit

Permalink
test: add tests for circle depends
Browse files Browse the repository at this point in the history
  • Loading branch information
fefit committed Jan 10, 2023
1 parent 45afc79 commit 5e9d9c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
9 changes: 3 additions & 6 deletions fet.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ var (
)

// ImportNode
// every node will lookup it's prevs
// so nexts are not necessary
type ImportNode struct {
Value string
Prevs []*ImportNode
Nexts []*ImportNode
}

func (importNode *ImportNode) LookupCircle(search *ImportNode, chains []*ImportNode) []string {
Expand Down Expand Up @@ -208,7 +209,6 @@ func (imports *Imports) Add(cur string, depend string) []string {
node := &ImportNode{
Value: cur,
Prevs: []*ImportNode{},
Nexts: []*ImportNode{},
}
imports.Nodes[cur] = node
curNode = node
Expand All @@ -225,14 +225,11 @@ func (imports *Imports) Add(cur string, depend string) []string {
// add cur node to the next node's prevs
curNode,
},
Nexts: []*ImportNode{},
}
imports.Nodes[depend] = node
nextNode = node
}
// add next node to cur node's nexts
curNode.Nexts = append(curNode.Nexts, nextNode)
// judge if the next depend node appears in prevs chains
// judge if the next depend node in prevs chains
return curNode.LookupCircle(nextNode, []*ImportNode{})
}

Expand Down
34 changes: 34 additions & 0 deletions fet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,37 @@ func TestCompile(t *testing.T) {
}, "smaller")
})
}

func TestImportsDepends(t *testing.T) {
var initImports = func() Imports {
return Imports{
Nodes: map[string]*ImportNode{},
}
}
// A -> A depend itself
imports := initImports()
assert.True(t, len(imports.Add("A", "A")) > 0)
// A -> B B -> C C -> A
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("B", "C")) == 0)
assert.True(t, len(imports.Add("C", "A")) > 0)
// A -> B C -> A B -> C
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("C", "A")) == 0)
assert.True(t, len(imports.Add("B", "C")) > 0)
// C -> D A -> B D -> A B -> C
imports = initImports()
assert.True(t, len(imports.Add("C", "D")) == 0)
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("D", "A")) == 0)
assert.True(t, len(imports.Add("B", "C")) > 0)
// A -> B A -> C C -> D D -> A
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("A", "C")) == 0)
assert.True(t, len(imports.Add("D", "A")) == 0)
assert.True(t, len(imports.Add("C", "D")) > 0)

}

0 comments on commit 5e9d9c1

Please sign in to comment.