PPysolate SpineREAD THE CODE · UNDERSTAND THE SYSTEM
课程与章节
源码索引 / CURRENT IMPLEMENTATION

learning/go-patterns/patterns/slice-boundary/slice_boundary_test.go

45 行 · 点击行号可定位;函数链接随声明位置更新,不手填行号。

跳到函数 / 声明
package sliceboundary

import "testing"

func TestNewBatchCopiesInput(t *testing.T) {
	input := []string{"go", "is", "small"}
	batch := NewBatch(input)
	input[0] = "python"

	if got, want := batch.Items(), []string{"go", "is", "small"}; !equalStrings(got, want) {
		t.Fatalf("Items() = %#v, want %#v", got, want)
	}
}

func TestItemsReturnsCopy(t *testing.T) {
	batch := NewBatch([]string{"one", "two"})
	items := batch.Items()
	items[0] = "changed"

	if got, want := batch.Items(), []string{"one", "two"}; !equalStrings(got, want) {
		t.Fatalf("Items() after caller mutation = %#v, want %#v", got, want)
	}
}

func TestZeroValueBatchIsUsable(t *testing.T) {
	var batch Batch
	if got := batch.Len(); got != 0 {
		t.Fatalf("zero-value Len() = %d, want 0", got)
	}
	if got := batch.Items(); got != nil {
		t.Fatalf("zero-value Items() = %#v, want nil", got)
	}
}

func equalStrings(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}