Exemplo n.º 1
0
func TestListTypeAfterMutations(t *testing.T) {
	smallTestChunks()
	defer normalProductionChunks()

	assert := assert.New(t)

	test := func(n int, c interface{}) {
		values := generateNumbersAsValues(n)

		l := NewList(values...)
		assert.Equal(l.Len(), uint64(n))
		assert.IsType(c, l.sequence())
		assert.True(l.Type().Equals(MakeListType(NumberType)))

		l = l.Append(String("a"))
		assert.Equal(l.Len(), uint64(n+1))
		assert.IsType(c, l.sequence())
		assert.True(l.Type().Equals(MakeListType(MakeUnionType(NumberType, StringType))))

		l = l.Splice(l.Len()-1, 1)
		assert.Equal(l.Len(), uint64(n))
		assert.IsType(c, l.sequence())
		assert.True(l.Type().Equals(MakeListType(NumberType)))
	}

	test(15, listLeafSequence{})
	test(1500, indexedMetaSequence{})
}
Exemplo n.º 2
0
func TestSetTypeAfterMutations(t *testing.T) {
	assert := assert.New(t)

	smallTestChunks()
	defer normalProductionChunks()

	test := func(n int, c interface{}) {
		values := generateNumbersAsValues(n)

		s := NewSet(values...)
		assert.Equal(s.Len(), uint64(n))
		assert.IsType(c, s.sequence())
		assert.True(s.Type().Equals(MakeSetType(NumberType)))

		s = s.Insert(String("a"))
		assert.Equal(s.Len(), uint64(n+1))
		assert.IsType(c, s.sequence())
		assert.True(s.Type().Equals(MakeSetType(MakeUnionType(NumberType, StringType))))

		s = s.Remove(String("a"))
		assert.Equal(s.Len(), uint64(n))
		assert.IsType(c, s.sequence())
		assert.True(s.Type().Equals(MakeSetType(NumberType)))
	}

	test(10, setLeafSequence{})
	test(2000, orderedMetaSequence{})
}
Exemplo n.º 3
0
func TestMapTypeAfterMutations(t *testing.T) {
	assert := assert.New(t)

	test := func(n int, c interface{}) {
		values := make([]Value, 2*n)
		for i := 0; i < n; i++ {
			values[2*i] = Number(i)
			values[2*i+1] = Number(i)
		}

		m := NewMap(values...)
		assert.Equal(m.Len(), uint64(n))
		assert.IsType(c, m.sequence())
		assert.True(m.Type().Equals(MakeMapType(NumberType, NumberType)))

		m = m.Set(String("a"), String("a"))
		assert.Equal(m.Len(), uint64(n+1))
		assert.IsType(c, m.sequence())
		assert.True(m.Type().Equals(MakeMapType(MakeUnionType(NumberType, StringType), MakeUnionType(NumberType, StringType))))

		m = m.Remove(String("a"))
		assert.Equal(m.Len(), uint64(n))
		assert.IsType(c, m.sequence())
		assert.True(m.Type().Equals(MakeMapType(NumberType, NumberType)))
	}

	test(10, mapLeafSequence{})
	test(1000, orderedMetaSequence{})
}
Exemplo n.º 4
0
func TestNewSet(t *testing.T) {
	assert := assert.New(t)
	s := NewSet()
	assert.True(MakeSetType(MakeUnionType()).Equals(s.Type()))
	assert.Equal(uint64(0), s.Len())

	s = NewSet(Number(0))
	assert.True(MakeSetType(NumberType).Equals(s.Type()))

	s = NewSet()
	assert.IsType(MakeSetType(NumberType), s.Type())

	s2 := s.Remove(Number(1))
	assert.IsType(s.Type(), s2.Type())
}
Exemplo n.º 5
0
func TestWrap(t *testing.T) {
	assert := assert.New(t)

	te := testError{"te"}
	we := Wrap(te)
	assert.Equal(te, we.Cause())
	assert.IsType(wrappedError{}, we)
	assert.Equal(we, Wrap(we))
	fmt.Printf("st: %s, cause: %s\n", we.Error(), we.Cause())
	assert.Nil(Wrap(nil))
}
Exemplo n.º 6
0
func TestTryCatch(t *testing.T) {
	assert := assert.New(t)

	assert.Panics(func() {
		TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				if !causeInTypes(err, testError2{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	})

	assert.Panics(func() {
		TryCatch(func() {
			panic(te)
		},
			func(err error) error {
				if !causeInTypes(err, testError{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	})

	assert.IsType(wrappedError{}, func() error {
		return TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				return err
			})
	}())

	assert.Error(func() error {
		return TryCatch(func() {
			panic(Wrap(te))
		},
			func(err error) error {
				if !causeInTypes(err, testError2{}, testError{}) {
					panic(err)
				}
				return Unwrap(err)
			})
	}())
}
Exemplo n.º 7
0
func TestParseError(t *testing.T) {
	assert := assert.New(t)

	assertParseError := func(s string) {
		e := d.Try(func() { Parse(s) })
		assert.IsType(d.UsageError{}, e)
	}

	assertParseError("foo")
	assertParseError("sha1")
	assertParseError("sha1-0")

	// too many digits
	assertParseError("sha1-00000000000000000000000000000000000000000")

	// 'g' not valid hex
	assertParseError("sha1-000000000000000000000000000000000000000g")

	// sha2 not supported
	assertParseError("sha2-0000000000000000000000000000000000000000")

	r := Parse("sha1-0000000000000000000000000000000000000000")
	assert.NotNil(r)
}
Exemplo n.º 8
0
// IsType asserts that the specified objects are of the same type.
func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) {
	if !assert.IsType(t, expectedType, object, msgAndArgs...) {
		t.FailNow()
	}
}