Esempio n. 1
0
func TestLeaf(t *testing.T) {
	v := leaf("foo")

	assert.Equal(t, depthT(0), v.depth())
	assert.Equal(t, int64(3), v.length())

	assert.Equal(t, v, v.slice(0, v.length()))
	assert.Equal(t, v, v.slice(-100, 100))
	assert.Equal(t, leaf("f"), v.slice(0, 1))
	assert.Equal(t, leaf("f"), v.slice(-100, 1))
	assert.Equal(t, leaf("o"), v.slice(2, v.length()))
	assert.Equal(t, leaf(""), v.slice(2, 1))

	assert.Equal(t, v, v.dropPrefix(0))
	assert.Equal(t, v, v.dropPrefix(-100))
	assert.Equal(t, leaf("oo"), v.dropPrefix(1))
	assert.Equal(t, leaf(""), v.dropPrefix(3))
	assert.Equal(t, leaf(""), v.dropPrefix(100))

	assert.Equal(t, v, v.dropPostfix(3))
	assert.Equal(t, v, v.dropPostfix(100))
	assert.Equal(t, leaf("fo"), v.dropPostfix(2))
	assert.Equal(t, leaf(""), v.dropPostfix(0))
	assert.Equal(t, leaf(""), v.dropPostfix(-100))

	buf := bytes.NewBuffer(nil)
	v.WriteTo(buf)
	assert.Equal(t, "foo", buf.String())

	counter := 0
	err := v.walkLeaves(func(l string) error {
		if counter > 0 {
			t.Errorf("leaf.walkLeaves: function called too many times")
			return errors.New("called a lot")
		}
		counter++

		assert.Equal(t, string(v), l)

		return nil
	})
	assert.Nil(t, err)
}
Esempio n. 2
0
func TestWalk(t *testing.T) {
	defer disableCoalesce()()

	for _, r := range []Rope{Rope{}, emptyRope} {
		r.Walk(func(_ string) error {
			t.Error("call to empty Rope's Walk parameter")
			return nil
		})
	}

	for _, r := range []Rope{
		New("abc").AppendString("def").AppendString("ghi"),
	} {
		str := r.String()
		err := r.Walk(func(part string) error {
			assert.Equal(t, str[:len(part)], part)
			str = str[len(part):]
			return nil
		})
		assert.Nil(t, err)
		assert.Equal(t, "", str)
	}

	for _, r := range []Rope{
		New("abc").AppendString("def").AppendString("ghi"),
	} {
		str := r.String()
		err := r.Walk(func(part string) error {
			assert.Equal(t, str[:len(part)], part)
			str = str[len(part):]
			if len(str) < 4 {
				return errors.New("stop now")
			}
			return nil
		})
		assert.Equal(t, err, errors.New("stop now"))
		assert.True(t, 0 < len(str) && len(str) < 4)
	}

}
Esempio n. 3
0
func TestReadAt(t *testing.T) {
	want := treeR.Bytes()

	buf := make([]byte, len(want)+1)

	for start := 0; start < len(buf); start++ {
		for end := start; end <= len(buf); end++ {
			length := end - start
			b := buf[0:length]
			n, err := treeR.ReadAt(b, int64(start))

			// Basic io.ReaderAt contract
			assert.True(t, n <= length)
			if n < length {
				assert.Equal(t, io.EOF, err)
			}

			// Expected actual end and length
			eEnd := end
			if eEnd > len(want) {
				eEnd = len(want)
			}
			eLen := eEnd - start

			// Check for correctness
			assert.Equal(t, eLen, n)
			if eLen < length {
				assert.Equal(t, io.EOF, err)
			} else if eLen > length {
				assert.Nil(t, err)
			} else {
				assert.True(t, err == nil || err == io.EOF)
			}

			assert.Equal(t, want[start:eEnd], b[:n])
		}
	}
}
Esempio n. 4
0
func TestConcatWalkLeaves(t *testing.T) {
	counter := 0
	err := tree.walkLeaves(func(l string) error {
		switch counter {
		case 0:
			assert.Equal(t, string(lhs), l)
		case 1:
			assert.Equal(t, string(rhs), l)
		case 2:
			t.Errorf("leaf.walkLeaves: function called too many times")
		default:
			// Ignore more than two calls, error has already been produced.
		}
		counter++

		return nil
	})
	assert.Nil(t, err)

	counter = 0
	err = tree.walkLeaves(func(l string) error {
		switch counter {
		case 0:
			assert.Equal(t, string(lhs), l)
			return errors.New("stop now")
		case 1:
			t.Errorf("leaf.walkLeaves: did not stop after returning true")
		default:
			// Ignore more than two calls, error has already been produced.
		}
		counter++

		return nil
	})
	assert.Equal(t, errors.New("stop now"), err)
}