如何使函数适合 _test 文件?

这是我的代码


type Queue interface {

    Push(key interface{})

    Pop() interface{}

    Contains(key interface{}) bool

    Len() int

    Keys() []interface{}

}


type QueueData struct {

    size int

    data []interface{}

}


func New(size int) *QueueData {

    return &QueueData{

        size: size,

    }

}


func (q *QueueData) IsEmpty() bool {

    return len(q.data) == 0

}


// Peek : returns the next element in the queue

func (q *QueueData) Peek() (interface{}, error) {

    if len(q.data) == 0 {

        return 0, fmt.Errorf("Queue is empty")

    }

    return q.data[0], nil

}


// Queue : adds an element onto the queue and returns an pointer to the current queue

func (q *QueueData) Push(n interface{}) *QueueData {

    if q.Len() < q.size {

        q.data = append(q.data, n)

    } else {

        q.Pop()

        q.Push(n)

    }

    return q

}


// Dequeue : removes the next element from the queue and returns its value

//func (q *QueueFix) Pop() (interface{}, error) {

func (q *QueueData) Pop() interface{} {

    if len(q.data) == 0 {

        //return 0, fmt.Errorf("Queue is empty")

        return 0

    }

    element := q.data[0]

    q.data = q.data[1:]

    //return element, nil

    return element

}


func (q *QueueData) Len() int {

    return len(q.data)

}


func (q *QueueData) Keys() []interface{} {

    return q.data

}


func (q *QueueData) Contains(key interface{}) bool {

    cont := false

    for i := 0; i < q.Len(); i++ {

        if q.data[i] == key {

            cont = true

        }

    }

    return cont

}

我的测试看起来像这样......


var testValues = []interface{}{

    "lorem",

    "ipsum",

    1,

    2,

    3,

    "jack",

    "jill",

    "felix",

    "donking",

}


测试返回


.\Queue_test.go:60:4: 错误调用可能有格式化指令 %v FAIL /C /Users/richa/go/src [构建失败]


我不明白如何从 _test.go 文件中制作代码。还需要解释如何制作 _test.go。只是给这个测试一些参考。就像我添加 EvictPolicy 函数时一样,它未声明...


MMTTMM
浏览 149回答 1
1回答

沧海一幻觉

您看到的错误Queue_test.go:60:4:&nbsp;Error&nbsp;call&nbsp;has&nbsp;possible&nbsp;formatting&nbsp;directive&nbsp;%v&nbsp;FAIL看起来是指这条线t.Error("expected&nbsp;%v&nbsp;but&nbsp;recevied&nbsp;%v",&nbsp;expect,&nbsp;v)在这一行中,您调用t.Error以打印错误消息并将expectand&nbsp;vvars 格式化为错误字符串。但是,t.Error不会格式化附加参数。它只是打印它们。您应该t.Errorf像处理其他错误消息一样调用。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go