package robot import ( "fmt" "log" ) type CaseNode struct { testCase *TestCase //用例名 Next *CaseNode //测试用例 } type LinkCase struct { Head *CaseNode force bool } func NewLinkCase() *LinkCase { return &LinkCase{} } func NewDefault(testCast *TestCase) *LinkCase { return &LinkCase{ force: true, Head: &CaseNode{testCase: testCast}, } } func (this *LinkCase) isEmpty() bool { return this.Head == nil } func (this *LinkCase) lastNode() *CaseNode { cur := this.Head for cur.Next != nil { cur = cur.Next } return cur } //尾部追加 func (this *LinkCase) Append(testCase *TestCase) { newNode := &CaseNode{testCase: testCase} if this.isEmpty() { this.Head = newNode } else { this.lastNode().Next = newNode } } //头部添加 func (this *LinkCase) Unshift(testCase *TestCase) { if this.force { log.Fatal("use `NewLinkCase` method create link if you want to use `Unshift` opt") return } newNode := &CaseNode{testCase: testCase} newNode.Next = this.Head this.Head = newNode } func (this *LinkCase) length() int { cur := this.Head count := 0 for cur.Next != nil { cur = cur.Next count++ } return count } func (this *LinkCase) Insert(index int, testCase *TestCase) { if this.force { log.Fatal("use `NewLinkCase` method create link if you want to use `Insert` opt") return } if index < 0 { this.Unshift(testCase) } else if index > this.length() { this.Append(testCase) } else { preNode := this.Head count := 0 for count < index-1 { preNode = preNode.Next count++ } newNode := &CaseNode{testCase: testCase, Next: preNode.Next} preNode.Next = newNode } } func (this *CaseNode) String() string { return fmt.Sprintf("value=%v", this.testCase.desc) }