Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions builtin/expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,57 @@ func TestInvokeExpr_Eval(t *testing.T) {
})
}

func TestVectorExpr_Eval(t *testing.T) {
t.Run("ConstMembers", func(t *testing.T) {
vec := NewVector(Keyword("foo"))

any, err := (VectorExpr{
Analyzer: &Analyzer{},
Vector: vec,
}).Eval(nil)

assert.NoError(t, err)
assert.NotNil(t, any)
assert.IsType(t, PersistentVector{}, any)
assert.Equal(t, NewVector(Keyword("foo")), any)

any, err = any.(core.Vector).EntryAt(0)
assert.NoError(t, err)
assert.Equal(t, Keyword("foo"), any)
})

t.Run("SymbolMember", func(t *testing.T) {
env := core.New(map[string]core.Any{"foo": Keyword("foo")})
vec := NewVector(Symbol("foo"))

any, err := (VectorExpr{
Analyzer: &Analyzer{},
Vector: vec,
}).Eval(env)

assert.NoError(t, err)
assert.NotNil(t, any)
assert.IsType(t, PersistentVector{}, any)
assert.Equal(t, NewVector(Keyword("foo")), any)

any, err = any.(core.Vector).EntryAt(0)
assert.NoError(t, err)
assert.Equal(t, Keyword("foo"), any)
})

t.Run("UnboundSymbolMember", func(t *testing.T) {
vec := NewVector(Symbol("foo"), Symbol("bar"), Symbol("baz"))

any, err := (VectorExpr{
Analyzer: &Analyzer{},
Vector: vec,
}).Eval(core.New(nil))

assert.Error(t, err)
assert.Nil(t, any)
})
}

func runExprTests(t *testing.T, table []exprTest) {
for _, tt := range table {
t.Run(tt.title, func(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion reader/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ func readVector(rd *Reader, _ rune) (core.Any, error) {
const vecEnd = ']'

v := builtin.EmptyVector.Transient()
return v.Persistent(), rd.Container(vecEnd, "Vector", func(val core.Any) error {
err := rd.Container(vecEnd, "Vector", func(val core.Any) error {
v.Cons(val)
return nil
})

return v.Persistent(), err
}
125 changes: 63 additions & 62 deletions reader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestReader_All(t *testing.T) {
}{
{
name: "ValidLiteralSample",
src: `123 "Hello World" 12.34 -0xF +010 true nil 0b1010 \a :hello`,
src: `123 "Hello World" 12.34 -0xF +010 true nil 0b1010 \a :hello [:foo "bar"]`,
want: []core.Any{
builtin.Int64(123),
builtin.String("Hello World"),
Expand All @@ -139,6 +139,7 @@ func TestReader_All(t *testing.T) {
builtin.Int64(10),
builtin.Char('a'),
builtin.Keyword("hello"),
builtin.NewVector(builtin.Keyword("foo"), builtin.String("bar")),
},
},
{
Expand Down Expand Up @@ -586,67 +587,67 @@ func TestReader_One_List(t *testing.T) {
})
}

// func TestReader_One_Vector(t *testing.T) {
// executeReaderTests(t, []readerTestCase{
// {
// name: "EmptyVector",
// src: `[]`,
// want: builtin.Vector{},
// },
// {
// name: "VectorWithOneEntry",
// src: `[help]`,
// want: builtin.Vector{builtin.Symbol("help")},
// },
// {
// name: "VectorWithMultipleEntry",
// src: `[+ 0xF 3.1413]`,
// want: builtin.Vector{
// builtin.Symbol("+"),
// builtin.Int64(15),
// builtin.Float64(3.1413),
// },
// },
// {
// name: "VectorWithCommaSeparator",
// src: `[+,0xF,3.1413]`,
// want: builtin.Vector{
// builtin.Symbol("+"),
// builtin.Int64(15),
// builtin.Float64(3.1413),
// },
// },
// {
// name: "MultiLine",
// src: `[+
// 0xF
// 3.1413
// ]`,
// want: builtin.Vector{
// builtin.Symbol("+"),
// builtin.Int64(15),
// builtin.Float64(3.1413),
// },
// },
// {
// name: "MultiLineWithComments",
// src: `[+ ; plus operator adds numerical values
// 0xF ; hex representation of 15
// 3.1413 ; value of math constant pi
// ]`,
// want: builtin.Vector{
// builtin.Symbol("+"),
// builtin.Int64(15),
// builtin.Float64(3.1413),
// },
// },
// {
// name: "UnexpectedEOF",
// src: "[+ 1 2 ",
// wantErr: true,
// },
// })
// }
func TestReader_One_Vector(t *testing.T) {
executeReaderTests(t, []readerTestCase{
{
name: "EmptyVector",
src: `[]`,
want: builtin.EmptyVector,
},
{
name: "VectorWithOneEntry",
src: `[help]`,
want: builtin.NewVector(builtin.Symbol("help")),
},
{
name: "VectorWithMultipleEntry",
src: `[+ 0xF 3.1413]`,
want: builtin.NewVector(
builtin.Symbol("+"),
builtin.Int64(15),
builtin.Float64(3.1413),
),
},
{
name: "VectorWithCommaSeparator",
src: `[+,0xF,3.1413]`,
want: builtin.NewVector(
builtin.Symbol("+"),
builtin.Int64(15),
builtin.Float64(3.1413),
),
},
{
name: "MultiLine",
src: `[+
0xF
3.1413
]`,
want: builtin.NewVector(
builtin.Symbol("+"),
builtin.Int64(15),
builtin.Float64(3.1413),
),
},
{
name: "MultiLineWithComments",
src: `[+ ; plus operator adds numerical values
0xF ; hex representation of 15
3.1413 ; value of math constant pi
]`,
want: builtin.NewVector(
builtin.Symbol("+"),
builtin.Int64(15),
builtin.Float64(3.1413),
),
},
{
name: "UnexpectedEOF",
src: "[+ 1 2 ",
wantErr: true,
},
})
}

type readerTestCase struct {
name string
Expand Down