Added more tests

This commit is contained in:
2025-02-08 00:40:41 +01:00
parent 1d50720c28
commit feacc27f49
3 changed files with 57 additions and 5 deletions

55
src/types/types_test.go Normal file
View File

@ -0,0 +1,55 @@
package types_test
import (
"testing"
"git.akyoto.dev/cli/q/src/types"
"git.akyoto.dev/go/assert"
)
func TestName(t *testing.T) {
assert.Equal(t, types.Int.Name(), "Int64")
assert.Equal(t, types.PointerAny.Name(), "Pointer")
assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "Pointer:Int64")
}
func TestSize(t *testing.T) {
assert.Equal(t, types.Int.Size(), 8)
assert.Equal(t, types.Int8.Size(), 1)
assert.Equal(t, types.Int16.Size(), 2)
assert.Equal(t, types.Int32.Size(), 4)
assert.Equal(t, types.Int64.Size(), 8)
assert.Equal(t, types.PointerAny.Size(), 8)
assert.Equal(t, (&types.Pointer{To: types.Int}).Size(), 8)
}
func TestStruct(t *testing.T) {
s := types.NewStruct("Test")
assert.Equal(t, s.Name(), "Test")
assert.Equal(t, s.Size(), 0)
field := &types.Field{Name: "TestField", TypeName: "Int8"}
s.AddField(field)
s.Update(map[string]types.Type{"Int8": types.Int8})
assert.Equal(t, s.Size(), 1)
assert.Equal(t, s.FieldByName("TestField"), field)
assert.Nil(t, s.FieldByName("does-not-exist"))
}
func TestBasics(t *testing.T) {
assert.True(t, types.Is(types.Int, types.Int))
assert.True(t, types.Is(types.PointerAny, types.PointerAny))
assert.False(t, types.Is(types.Int, types.Float))
assert.False(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: types.Float}))
}
func TestSpecialCases(t *testing.T) {
// Case #1:
// For syscalls whose return type is `nil` we currently allow casting them to anything.
assert.True(t, types.Is(nil, types.Int))
assert.True(t, types.Is(nil, types.Float))
// Case #2:
// A pointer pointing to a known type fulfills the requirement of a pointer to anything.
assert.True(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: nil}))
assert.True(t, types.Is(&types.Pointer{To: types.Float}, &types.Pointer{To: nil}))
}