Skip to content

Commit

Permalink
improved tests2
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Nov 18, 2024
1 parent a423b90 commit 4d2b12e
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 128 deletions.
125 changes: 4 additions & 121 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ var builtins = map[string]*env.Builtin{
// VALUES

// Tests:
// equal { dict { "a" 123 } -> "b" } 123
// equal { dict { "a" 123 } -> "a" } 123
"dict": { // ***
Argsn: 1,
Doc: "Constructs a Dict from the Block of key and value pairs.",
Expand Down Expand Up @@ -6412,7 +6412,7 @@ var builtins = map[string]*env.Builtin{
// Tests:
// equal { "abcde" .difference "cde" } "ab"
// equal { difference { 1 2 3 4 } { 2 4 } } { 1 3 }
// equal { difference list { "Bob" "Sal" "Joe" } { "Joe" } } { "Bob" "Sal" }
// equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" }
"difference": {
Argsn: 2,
Doc: "Finds the difference (values in first but not in second) of two values.",
Expand Down Expand Up @@ -8087,8 +8087,8 @@ var builtins = map[string]*env.Builtin{
},
// Tests:
// equal { { } .is-empty } 1
// equal { dict .is-empty } 1
// equal { spreadsheet { } { } .is-empty } 1
// equal { dict { } |is-empty } 1
// equal { spreadsheet { } { } |is-empty } 1
"is-empty": { // **
Argsn: 1,
Doc: "Accepts a collection (String, Block, Dict, Spreadsheet) and returns it's length.", // TODO -- accept context
Expand Down Expand Up @@ -8148,23 +8148,6 @@ var builtins = map[string]*env.Builtin{
}
},
},
// Args:
// * sheet
"ncols": {
Doc: "Accepts a Spreadsheet and returns number of columns.",
Argsn: 1,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Dict:
case env.Block:
case env.Spreadsheet:
return *env.NewInteger(int64(len(s1.Cols)))
default:
fmt.Println("Error")
}
return nil
},
},
// Tests:
// equal { dict { "a" 1 "b" 2 "c" 3 } |keys } { "a" "b" "c" }
// equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c }
Expand Down Expand Up @@ -8194,106 +8177,6 @@ var builtins = map[string]*env.Builtin{
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .col-sum 'a } 6
"col-sum": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r := s1.Sum(name)
if r.Type() == env.ErrorType {
ps.ErrorFlag = true
}
return r

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .col-avg 'a } 2.
"col-avg": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r, err := s1.Sum_Just(name)
if err != nil {
ps.ErrorFlag = true
return env.NewError(err.Error())
}
n := s1.NRows()
return *env.NewDecimal(r / float64(n))

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .A1 } 1

"A1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row first column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[0]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},
// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .B1 } 2
"B1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row second column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[1]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

/* Terminal functions .. move to it's own later */

// Tests:
Expand Down
130 changes: 125 additions & 5 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
case env.Block:
rdata := data1.Series.S

for i := 0; i < len(rdata)/hlen; i++ {
rowd := make([]any, hlen)
for ii := 0; ii < hlen; ii++ {
rowd[ii] = rdata[i*hlen+ii]
if hlen > 0 {
for i := 0; i < len(rdata)/hlen; i++ {
rowd := make([]any, hlen)
for ii := 0; ii < hlen; ii++ {
rowd[ii] = rdata[i*hlen+ii]
}
spr.AddRow(*env.NewSpreadsheetRow(rowd, spr))
}
spr.AddRow(*env.NewSpreadsheetRow(rowd, spr))
}
return *spr
case env.List:
Expand Down Expand Up @@ -1211,6 +1213,124 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
}
},
},

// TODO --- moved from base ... name appropriately and deduplicate
// Args:
// * sheet
"ncols": {
Doc: "Accepts a Spreadsheet and returns number of columns.",
Argsn: 1,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Dict:
case env.Block:
case env.Spreadsheet:
return *env.NewInteger(int64(len(s1.Cols)))
default:
fmt.Println("Error")
}
return nil
},
},
// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } |col-sum "a" } 6
"col-sum": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r := s1.Sum(name)
if r.Type() == env.ErrorType {
ps.ErrorFlag = true
}
return r

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } |col-avg 'a } 2
"col-avg": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r, err := s1.Sum_Just(name)
if err != nil {
ps.ErrorFlag = true
return env.NewError(err.Error())
}
n := s1.NRows()
return *env.NewDecimal(r / float64(n))

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } |A1 } 1

"A1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row first column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[0]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},
// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } |B1 } 2
"B1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row second column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[1]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},
}

func RyeValueToSpreadsheetRow(spr *env.Spreadsheet, obj env.Object) (bool, string, *env.SpreadsheetRow) {
Expand Down
Loading

0 comments on commit 4d2b12e

Please sign in to comment.