From 4d2b12ed4e93e252f362a38d09d0981a9ab3f0c8 Mon Sep 17 00:00:00 2001 From: refaktor Date: Mon, 18 Nov 2024 10:51:49 +0100 Subject: [PATCH] improved tests2 --- evaldo/builtins.go | 125 +------------- evaldo/builtins_spreadsheet.go | 130 ++++++++++++++- tests2/base.tests.rye | 287 ++++++++++++++++++++++++++++++++- tests2/readme.md | 21 +++ 4 files changed, 435 insertions(+), 128 deletions(-) create mode 100644 tests2/readme.md diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 13915da..1fabbf6 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -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.", @@ -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.", @@ -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 @@ -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 } @@ -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: diff --git a/evaldo/builtins_spreadsheet.go b/evaldo/builtins_spreadsheet.go index 6040448..3b47b16 100644 --- a/evaldo/builtins_spreadsheet.go +++ b/evaldo/builtins_spreadsheet.go @@ -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: @@ -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) { diff --git a/tests2/base.tests.rye b/tests2/base.tests.rye index 6cac1c5..382032f 100644 --- a/tests2/base.tests.rye +++ b/tests2/base.tests.rye @@ -245,7 +245,7 @@ section "base" "base text" { } { - equal { dict { "a" 123 } -> "b" } 123 + equal { dict { "a" 123 } -> "a" } 123 } group "list" @@ -264,6 +264,7 @@ section "base" "base text" { { equal { not true } 0 + equal { not false } 1 } group "and" @@ -288,6 +289,61 @@ section "base" "base text" { equal { false .or false } 0 } + group "odd" + "" + { + } + + { + equal { 3 .odd } 1 + equal { 2 .odd } 0 + } + + group "even" + "" + { + } + + { + equal { 3 .odd } 0 + equal { 2 .odd } 1 + } + + group "mod" + "" + { + } + + { + equal { 4 .mod 2 } 0 + equal { 5 .mod 2 } 1 + equal { 5 .mod 3 } 2 + } + + group "_+" + "" + { + } + + { + equal { 1 + 1 } 2 + equal { 3 + 4 } 7 + equal { 5.6 + 7.8 } 13.4 + equal { "A" + "b" } "Ab" + equal { "A" + 1 } "A1" + equal { { 1 2 } { 3 4 } } { 1 2 3 4 } + } + + group "_-" + "" + { + } + + { + equal { 2 - 1 } 1 + equal { 5 - 6 } -1 + } + group "embed" "" { @@ -297,13 +353,240 @@ section "base" "base text" { equal { embed 101 "val {}" } "val 101" } + group "difference" + "" + { + } + + { + equal { "abcde" .difference "cde" } "ab" + equal { difference { 1 2 3 4 } { 2 4 } } { 1 3 } + equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" } + } + + group "capitalize" + "" + { + } + + { + equal { capitalize "abcde" } "Abcde" + } + + group "to-lower" + "" + { + } + + { + equal { to-lower "ABCDE" } "abcde" + } + + group "to-upper" + "" + { + } + + { + equal { to-upper "abcde" } "ABCDE" + } + + group "join" + "" + { + } + + { + equal { join { "Mary" "Anne" } } "MaryAnne" + equal { join { "Spot" "Fido" "Rex" } } "SpotFidoRex" + } + + group "join\\with" + "" + { + arg "* to-join" + arg "* delimiter" + } + + { + equal { join\with { "Mary" "Anne" } " " } "Mary Anne" + equal { join\with { "Spot" "Fido" "Rex" } "/" } "Spot/Fido/Rex" + } + + group "split" + "" + { + } + + { + equal { split "a,b,c" "," } { "a" "b" "c" } + } + + group "split\\quoted" + "" + { + arg "to-split" + arg "splitter" + arg "quote" + } + + { + equal { split\quoted "`a,b`,c,d" "," "`" } { "`a,b`" "c" "d" } + } + + group "split\\many" + "" + { + arg "* string" + arg "* separator-set" + } + + { + equal { split\many "192.0.0.1" "." } { "192" "0" "0" "1" } + equal { split\many "abcde..e.q|bar" ".|" } { "abcd" "e" "q" "bar" } + } + + group "split\\every" + "" + { + arg "* string" + arg "* N" + } + + { + equal { split\every "abcdefg" 3 } { "abc" "def" "g" } + equal { split\every "abcdefg" 2 } { "ab" "cd" "ef" "g" } + equal { split\every "abcdef" 2 } { "ab" "cd" "ef" } + } + + group "first" + "" + { + } + + { + equal { first { 1 2 3 4 } } 1 + equal { rest "abcde" } "a" + equal { rest list { 1 2 3 } } 1 + } + + group "rest" + "" + { + } + + { + equal { rest { 1 2 3 4 } } { 2 3 4 } + equal { rest "abcde" } "bcde" + equal { rest list { 1 2 3 } } list { 2 3 } + } + + group "rest\\from" + "" + { + } + + { + equal { rest\from { 1 2 3 4 5 6 } 3 } { 4 5 6 } + equal { rest\from "abcdefg" 1 } "bcdefg" + equal { rest\from list { 1 2 3 4 } 2 } list { 3 4 } + } + + group "tail" + "" + { + } + + { + equal { tail { 1 2 3 4 5 6 7 } 3 } { 5 6 7 } + equal { tail "abcdefg" 4 } "defg" + equal { tail list { 1 2 3 4 } 1 } list { 4 } + } + + group "last" + "" + { + } + + { + equal { last { 1 2 } } 2 + equal { last "abcd" } "d" + equal { list { 4 5 6 } } 6 + } + + group "head" + "" + { + } + + { + equal { head { 4 5 6 7 } 3 } { 4 5 6 } + equal { head "abcdefg" 2 } "ab" + equal { head "abcdefg" 4 } "abcd" + equal { head list { 10 20 30 40 } 2 } list { 10 20 } + } + + group "nth" + "" + { + } + + { + equal { nth { 1 2 3 4 5 } 4 } 4 + equal { nth { "a" "b" "c" "d" "e" } 2 } "b" + } + + group "values" + "" + { + } + + { + equal { dict { "a" 1 "b" 2 "c" 3 } |values } { 1 2 3 } + } + + group "range" + "" + { + arg "* low-value" + arg "* high-value" + } + + { + equal { range 1 5 } { 1 2 3 4 5 } + } + group "is-empty" "" { } { - equal { { } .is-empty } 1 + equal { { } .is-empty } 1 + equal { dict { } |is-empty } 1 + equal { spreadsheet { } { } |is-empty } 1 + } + + group "length?" + "" + { + } + + { + equal { { 1 2 3 } .length? } 3 + equal { length? "abcd" } 4 + equal { spreadsheet { 'val } { 1 2 3 4 } |length? } 4 + equal { vector { 10 20 30 } |length? } 3 + } + + group "keys" + "" + { + } + + { + equal { dict { "a" 1 "b" 2 "c" 3 } |keys } { "a" "b" "c" } + equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c } } group "cmd" diff --git a/tests2/readme.md b/tests2/readme.md new file mode 100644 index 0000000..492ee56 --- /dev/null +++ b/tests2/readme.md @@ -0,0 +1,21 @@ +# Generated reference + +This is next, w-i-p folder for tests and builtin reference. Both are generated from evaldo/builtins*.go files. + +For more about this read: https://ryelang.org/cookbook/improving-rye/one-source/ + +## Use + +to generate do in cmd/parse_bultins folder: + + ./parse_builtins ../../evaldo/builtins.go > ../../tests2/base.tests.rye + ./parse_builtins ../../evaldo/builtins_spreadsheet.go > ../../tests2/spreadsheet.tests.rye + + +To run tests do in this folder: + + rye . test base + rye . test spreadsheet + +The generator, testing framework and tests and additional info is being improved right now. This is a very temporary state. +