From 2ddb959609657140cfa006b74b7b55ca4b9ce678 Mon Sep 17 00:00:00 2001 From: refaktor Date: Tue, 20 Feb 2024 00:05:34 +0100 Subject: [PATCH] fixed tests, added probe --- evaldo/builtins.go | 8 + tests/basics.rye | 14 +- tests/builtins.rye | 1278 ------------------------------------------ tests/main.rye | 12 +- tests/structures.rye | 2 +- 5 files changed, 22 insertions(+), 1292 deletions(-) delete mode 100644 tests/builtins.rye diff --git a/evaldo/builtins.go b/evaldo/builtins.go index 9f4a24ac..2283e8db 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -1319,6 +1319,14 @@ var builtins = map[string]*env.Builtin{ return arg0 }, }, + "probe": { // ** + Argsn: 1, + Doc: "Prints information about a value.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + fmt.Println(arg0.Inspect(*ps.Idx)) + return arg0 + }, + }, "inspect": { // ** Argsn: 1, Doc: "Returs information about a value.", diff --git a/tests/basics.rye b/tests/basics.rye index a29f0471..a000129d 100644 --- a/tests/basics.rye +++ b/tests/basics.rye @@ -379,11 +379,11 @@ section "Doers and evaluators" equal { do { 101 + 101 } } 202 } - group "do-in" - mold\nowrap ?do-in + group "do\in" + mold\nowrap ?do\in { { context } { block } } { - equal { cc: context { x: 101 } do-in cc { x + 101 } } 202 + equal { cc: context { x: 101 } do\in cc { x + 101 } } 202 } group "with" @@ -401,11 +401,11 @@ section "Doers and evaluators" equal { try { 10 + unknown } |type? } 'error } - group "do-in\try" - mold\nowrap ?do-in\try + group "do\in\try" + mold\nowrap ?do\in\try { { context } { block } } { - equal { ctx: context { x: 101 } do-in\try ctx { x / 0 } |type? } 'error + equal { ctx: context { x: 101 } do\in\try ctx { x / 0 } |type? } 'error } group "vals" @@ -481,4 +481,4 @@ section "Function creating functions" } -end \ No newline at end of file +end diff --git a/tests/builtins.rye b/tests/builtins.rye deleted file mode 100644 index 2328c2c0..00000000 --- a/tests/builtins.rye +++ /dev/null @@ -1,1278 +0,0 @@ -; Test / docs definition ... mold/nowrap ?function get's docstrings from live functions -; title "Function reference: core builtins" - -section "Printing functions" -"Functions that print values to stdout." -{ - group "print" - mold\nowrap ?print - { { object } } - { - stdout { print 123 } "123" + newline - } - group "prn" - mold\nowrap ?prn - { { object } } - { - stdout { prn 123 } "123" - } - group "prns" - mold\nowrap ?prns - { { object } } - { - stdout { prns 123 } "123 " - } - group "print\val" - mold\nowrap ?print\val - { { object } } - { - stdout { print\val 33 "value is: {{}}" } "value is: 33" + newline - ; stdout { print\val "OK" "value is: {{}}" } "value is: 33" + newline ; TODO-BUG quotes the string - ; stdout { { "Jane Austen" } print\val "Hello {{}}!" } "value is: 33" + newline - } - ; group "print-ssv" - ; mold\nowrap ?print-ssv - ; { { function } } - ; { - ; } - ;group "print-csv" - ;mold\nowrap ?print-csv - ;{ { function } } - ;{ - ;} - ;group "print-json" - ;mold\nowrap ?print-json - ;{ { function } } - ;{ - ;} - group "probe" - mold\nowrap ?probe - { { function } } - { - stdout { probe 33 } "[Integer: 33]" + newline - } - ; group "inspect" - ; mold\nowrap ?inspect - ; { { function } } - ; { - ; stdout { inspect 33 } "33" - ; } -} - -section "Working with numbers" -"Functions that mostly work with numbers." -{ - group "inc" - mold\nowrap ?inc - { { integer } } - { - equal { inc 100 } 101 - } - - group "is-positive" - mold\nowrap ?is-positive - { { integer decimal } } - { - equal { is-positive 1 } 1 - equal { 0 .is-positive } 0 - ; TODO equal { -1 .is-positive } 0 - ; TODO -- same for decimal - } - - group "is-zero" - mold\nowrap ?is-zero - { { integer decimal } } - { - equal { is-zero 0 } 1 - equal { 0 .is-zero } 1 - equal { is-zero 0.0 } 1 - equal { is-zero 0.1 } 0 - } - - group "factor-of" - mold\nowrap ?factor-of - { { integer } { integer } } - { - equal { factor-of 10 2 } 1 - equal { 10 .factor-of 3 } 0 - } - - group "odd" - mold\nowrap ?odd - { { integer } } - { - equal { odd 3 } 1 - ; TODO - } - - group "even" - mold\nowrap ?even - { { integer } } - { - equal { even 2 } 1 - ; TODO - } - - group "mod" - mold\nowrap ?mod - { { integer } { integer } } - { - equal { 3 .mod 2 } 1 - ; TODO - } - - $ - group "+-*/" - "" - { { integer decimal } { integer decimal } } - { - } - - - group "!=><+" - "" - { { integer decimal } { integer decimal } } - { - } $ - -} - - -section "Logic functions" -"Function that help with logical operations." -{ - - group "true" - mold\nowrap ?true - { { } } - { - equal { true } 1 - } - - group "false" - mold\nowrap ?false - { { function } } - { - equal { false } 0 - } - - group "not" - mold\nowrap ?not - { { function } } - { - equal { not 0 } 1 - equal { not 1 } 0 - } - - group "and" - mold\nowrap ?and - { { function } } - { - equal { 1 .and 0 } 0 - equal { 1 .and 1 } 1 - equal { 0 .and 0 } 0 - } - - group "or" - mold\nowrap ?or - { { function } } - { - equal { 1 .or 0 } 1 - equal { 1 .or 1 } 1 - equal { 0 .or 0 } 0 - } - - group "xor" - mold\nowrap ?xor - { { function } } - { - equal { 0 .xor 0 } 0 - equal { 1 .xor 1 } 0 - equal { 0 .xor 1 } 1 - equal { 1 .xor 0 } 1 - } - group "all" - mold\nowrap ?all - { { block } } - { - equal { all { 1 1 1 } } 1 - equal { { 0 0 1 } .all } 0 - } - - group "any" - mold\nowrap ?any - { { block } } - { - equal { any { 0 0 1 } } 1 - equal { { 0 0 0 } .any } 0 - } - - ;group "any\with" - ;mold\nowrap ?any\with - ;{ { object } { block } } - ;{ - ; equal { any\with 1 { + 0 , + 0 } } 1 - ;} -} - -section "Working with strings" -"Functions that mostly work with strings." -{ - - group "capitalize" - mold\nowrap ?capitalize - { { string } } - { - equal { capitalize "anne" } "Anne" - } - - group "to-lower" - mold\nowrap ?to-lower - { { string } } - { - equal { to-lower "ANNE" } "anne" - } - - group "to-upper" - mold\nowrap ?to-upper - { { string } } - { - equal { to-upper "anne" } "ANNE" - } - - group "join" - mold\nowrap ?join - { { block list } } - { - equal { join [ "Marry" "Anne" ] } "MarryAnne" - } - - group "join\with" - mold\nowrap ?join\with - { { block list } { string } } - { - equal { join\with [ "Marry" "Anne" ] "," } "Marry,Anne" - } - - group "split" - mold\nowrap ?split - { { string } { string } } - { - equal { split "Marry,Anne" "," } { "Marry" "Anne" } - } - - group "split\quoted" - mold\nowrap ?split\quoted - { { string } { string } } - { - equal { split\quoted $"Marry,Anne",Joe$ "," $"$ } { "Marry,Anne" "Joe" } ;" } - } - - group "split\every" - mold\nowrap ?split\every - { { string } { integer } } - { - equal { split\every "abcbce" 3 } { "abc" "bce" } - } - - comment "Many of functions that work with other collections also work with strings." -} - -section "Working with blocks and lists" -"Functions that mostly work with collections (blocks / lists)." -{ - - group "range" - mold\nowrap ?range - { { integer } { integer } } - { - equal { range 1 5 } { 1 2 3 4 5 } - } - - group "first" - mold\nowrap ?first - { { block } } - { - equal { first { 1 2 3 4 5 } } 1 - equal { first list { 1 2 3 4 5 } } 1 - equal { first "12345" } "1" - equal { try { first { } } |type? } 'error - equal { try { first list { } } |type? } 'error - equal { try { first "" } |type? } 'error - } - - group "rest" - mold\nowrap ?rest - { { block } } - { - equal { rest { 1 2 3 4 5 } } { 2 3 4 5 } - equal { rest list { 1 2 3 4 5 } } list { 2 3 4 5 } - equal { rest "12345" } "2345" - equal { try { rest { } } |type? } 'error - equal { try { rest list { } } |type? } 'error - equal { try { rest "" } |type? } 'error - } - - group "rest\from" - mold\nowrap ?rest\from - { { block } { integer } } - { - equal { rest\from { 1 2 3 4 5 } 2 } { 3 4 5 } - equal { rest\from list { 1 2 3 4 5 } 2 } list { 3 4 5 } - equal { rest\from "12345" 2 } "345" - equal { try { rest\from { } } |type? } 'error - equal { try { rest\from list { } } |type? } 'error - equal { try { rest\from "" } |type? } 'error - } - - group "second" - mold\nowrap ?second - { { block } } - { - equal { second { 1 2 3 4 5 } } 2 - equal { second list { 1 2 3 4 5 } } 2 - equal { second "12345" } "2" - equal { try { second { } } |type? } 'error - equal { try { second list { } } |type? } 'error - equal { try { second "" } |type? } 'error - } - - group "third" - mold\nowrap ?third - { { block } } - { - equal { third { 1 2 3 4 5 } } 3 - equal { third list { 1 2 3 4 5 } } 3 - equal { third "12345" } "3" - equal { try { third { } } |type? } 'error - equal { try { third list { } } |type? } 'error - equal { try { third "" } |type? } 'error - } - - group "nth" - mold\nowrap ?nth - { { block } } - { - equal { nth { 1 2 3 4 5 } 4 } 4 - equal { nth list { 1 2 3 4 5 } 4 } 4 - equal { nth "12345" 4 } "4" - equal { try { nth { } } |type? } 'error - equal { try { nth list { } } |type? } 'error - equal { try { nth "" } |type? } 'error - } - - group "->" - mold\nowrap ?_-> - { { string } } - { - equal { { 11 22 33 } -> 1 } 22 - equal { list { 11 22 33 } -> 1 } 22 - equal { dict { "a" 11 "b" 22 "c" 33 } -> "b" } 22 - } - - group "<-" - mold\nowrap ?_<- - { { string } } - { - equal { 2 <- { 11 22 33 } } 33 - equal { 2 <- list { 11 22 33 } } 33 - equal { "c" <- dict { "a" 11 "b" 22 "c" 33 } } 33 - } - - - group "last" - mold\nowrap ?last - { { block } } - { - equal { last { 1 2 3 4 5 } } 5 - equal { last list { 1 2 3 4 5 } } 5 - equal { last "12345" } "5" - equal { try { last { } } |type? } 'error - equal { try { last list { } } |type? } 'error - equal { try { last "" } |type? } 'error - } - - group "head" - mold\nowrap ?head - { { block } { integer } } - { - equal { head { 1 2 3 4 5 } 2 } { 1 2 } - equal { head { 1 2 3 4 5 } 10 } { 1 2 3 4 5 } - equal { head { } 2 } { } - equal { head list { 1 2 3 4 5 } 2 } list { 1 2 } - equal { head list { 1 2 3 4 5 } 10 } list { 1 2 3 4 5 } - equal { head list { } 2 } list { } - equal { head "12345" 2 } "12" - equal { head "12345" 10 } "12345" - equal { head "" 2 } "" - } - - - group "tail" - mold\nowrap ?tail - { { block } { integer } } - { - equal { tail { 1 2 3 4 5 } 2 } { 4 5 } - equal { tail { 1 2 3 4 5 } 10 } { 1 2 3 4 5 } - equal { tail { } 2 } { } - equal { tail list { 1 2 3 4 5 } 2 } list { 4 5 } - equal { tail list { 1 2 3 4 5 } 10 } list { 1 2 3 4 5 } - equal { tail list { } 2 } list { } - equal { tail "12345" 2 } "45" - equal { tail "12345" 10 } "12345" - equal { tail "" 2 } "" - } - - group "length?" - mold\nowrap ?length? - { { block string list } } - { - equal { length? "1234567" } 7 - equal { { 1 2 3 4 5 } .length? } 5 - equal { list { 1 2 3 } |length? } 3 - } - - group "list" - mold\nowrap ?list - { { block } } - { - equal { list { "a" 1 "b" 2 } |type? } 'list - } - - group "dict" - mold\nowrap ?dict - { { block } } - { - equal { dict { "a" 1 "b" 2 } |type? } 'dict - } - - group "min" - mold\nowrap ?dict - { { block } } - { - equal { min { 8 2 10 6 } } 2 - equal { min list { 8 2 10 6 } } 2 - equal { try { min { } } |type? } 'error - equal { try { min list { } } |type? } 'error - } - group "max" - mold\nowrap ?dict - { { block } } - { - equal { max { 8 2 10 6 } } 10 - equal { max list { 8 2 10 6 } } 10 - equal { try { max { } } |type? } 'error - equal { try { max list { } } |type? } 'error - } - group "avg" - mold\nowrap ?dict - { { block } } - { - equal { avg { 8 2 10 6 } } 6.5 - equal { avg list { 8 2 10 6 } } 6.5 - equal { try { avg { } } |type? } 'error - equal { try { avg list { } } |type? } 'error - } - group "sum" - mold\nowrap ?sum - { { block } } - { - equal { sum { 8 2 10 6 } } 26 - equal { sum { 8 2 10 6.5 } } 26.5 - equal { sum { } } 0 - equal { sum list { 8 2 10 6 } } 26 - equal { sum list { 8 2 10 6.5 } } 26.5 - equal { sum list { } } 0 - } - group "union" - mold\nowrap ?union - { { block } } - { - equal { union { 8 2 } { 1 9 } |length? } 4 ; sortorder isn't deterministic so far ... think if it should be - equal { union { } { 1 9 } |length? } 2 - equal { union { } { } } { } - } - group "intersection" - mold\nowrap ?intersection - { { block } } - { - equal { intersection { 1 3 5 6 } { 2 3 4 5 } } { 3 5 } - equal { intersection { } { 2 3 4 } } { } - equal { intersection { } { } } { } - } - group "unique" - mold\nowrap ?append! - { { word } { object } } - { - equal { { 3 2 3 5 3 2 } .list .unique .length? } 3 ; result order is not deterministic list { 3 2 5 } - } - - group "contains" - mold\nowrap ?contains - { { block string list } } - { - equal { contains "abbaca" "b" } 1 - equal { contains { 1 2 3 } 2 } 1 - equal { contains list { 1 2 3 } 2 } 1 - } -} - - -section "Type conversion and checking" -"Functions that convert between Rye value types." -{ - group "to-word" - mold\nowrap ?to-word - { { string word } } - { - equal { to-word "test" } 'test - equal { to-word 'test } 'test - } - group "to-integer" - mold\nowrap ?to-integer - { { string number } } - { - equal { to-integer "123" } 123 - } - - group "to-string" - mold\nowrap ?to-string - { { string word number } } - { - equal { to-string 123 } "123" - equal { to-string 'word } "word" - } - - group "to-uri" - mold\nowrap ?to-uri - { { string } } - { - equal { to-uri "https://example.com" } https://example.com - } - - group "to-file" - mold\nowrap ?to-file - { { string } } - { - equal { to-file "file.txt" } %file.txt - } - - group "to-context" - mold\nowrap ?to-context - { { string } } - { - equal { cc: to-context dict { "name" "Jim" } , cc/name } "Jim" - } - - group "is-integer" - mold\nowrap ?is-integer - { { object } } - { - equal { is-integer 123 } 1 - equal { is-integer "ABC" } 0 - } - - group "is-decimal" - mold\nowrap ?is-decimal - { { object } } - { - equal { is-decimal 123.456 } 1 - equal { is-decimal 123 } 0 - equal { is-decimal "ABC" } 0 - } - - group "is-number" - mold\nowrap ?is-number - { { object } } - { - equal { is-number 123 } 1 - equal { is-number 123.456 } 1 - equal { is-number "ABC" } 0 - } - - group "is-string" - mold\nowrap ?is-string - { { object } } - { - equal { is-string "ABC" } 1 - equal { is-string 123 } 0 - equal { is-string %file.txt } 0 - equal { is-string test@gmail.com } 0 - } - - group "type?" - mold\nowrap ?type? - { { object } } - { - equal { type? "ABC" } 'string - equal { type? 123 } 'integer - ; TODO -- add for other types - } -} - -section "Serializers and loaders" -"Functions that print values to stdio" -{ - group "mold" - mold\nowrap ?mold - { { function } } - { - equal { mold 33 } "33" - } - group "mold\unwrap" - mold\nowrap ?mold\nowrap - { { function } } - { - equal { mold\nowrap "33" } $"33"$ - } - group "capture-stdout" - mold\nowrap ?capture-stdout - { { block } } - { - equal { capture-stdout { "out" .print , "not this" } } "out" + newline - } - - group "dump" - mold\nowrap ?dump - { { any } { word } } - { - equal { fn { x } { x + 1 } |dump } { fn { x } { x ._+ 1 } } - } - - group "doc\of?" - mold\nowrap ?doc\of? - { { function } } - { - equal { fn { x "docstring" } { x + 1 } |doc\of? } "docstring" - } - - group "load" - mold\nowrap ?load - { { block } } - { - equal { load "1 2 3" |type? } 'block - equal { load "1 2 3" |length? } 3 - equal { load "1 2 { print 4 }" |third |length? } 2 - equal { load "1 2 { print 4 }" |third |first |type? } 'word - } - - -} - -$ section "Context functions" -"Functions that mostly work with blocks." -{ - group "doc" - mold\nowrap ?doc - { { string } } - { - ; TODO - } - - group "doc?" - mold\nowrap ?doc? - { { } } - { - ; TODO - } - -} $ - - -section "Conditional functions" -"Program flow control functions" -{ - - group "if" - mold\nowrap ?if - { { object } { block } } - { - equal { if 1 { 101 } } 101 - equal { if 0 { 101 } } 0 - } - group "otherwise" - mold\nowrap ?otherwise - { { object } { block } } - { - equal { otherwise 1 { 101 } } 0 - equal { otherwise 0 { 101 } } 101 - } - group "^if" - mold\nowrap ?^if - { { object } { block } } - { - equal { ^if 1 { 101 } } 101 - equal { ^if 0 { 101 } } 0 - } - group "^otherwise" - mold\nowrap ?^otherwise - { { object } { block } } - { - equal { ^otherwise 1 { 101 } } 0 - equal { ^otherwise 0 { 101 } } 101 - } - group "either" - mold\nowrap ?either - { { object } { block } { block } } - { - equal { either 1 { 101 } { 202 } } 101 - equal { either 0 { 101 } { 202 } } 202 - } - group "switch" - mold\nowrap ?switch - { { object } { block } } - { - equal { switch 1 { 1 { 101 } 2 { 202 } } } 101 - equal { switch 2 { 1 { 101 } 2 { 202 } } } 202 - } - group "cases" - mold\nowrap ?cases - { { object } { block } { block } } - { - equal { cases 1 { { 1 } { + 10 } { 1 } { + 100 } } } 111 - equal { cases 1 { { 0 } { + 10 } { 1 } { + 100 } } } 101 - ; TODO cases doesn't do error handling well yet - } -} - -section "Looping functions" -"Program flow control functions" -{ - - group "loop" - mold\nowrap ?if - { { object } { block } } - { - stdout { loop 3 { prns "x" } } "x x x " - } - group "for" - mold\nowrap ?otherwise - { { object } { block } } - { - stdout { for { 1 2 3 } { prns "x" } } "x x x " - stdout { { "a" "b" "c" } .for { .prns } } "a b c " - } - group "forever" - mold\nowrap ?forever - { { object } { block } } - { - stdout { forever { "once" .prn .return } } "once" - } - group "forever\with" - mold\nowrap ?forever\with - { { object } { block } } - { - stdout { forever\with 1 { .prn .return } } "1" - } -} - - -section "Doers and evaluators" -"Functions that do code and evaluate blocks." -{ - - group "do" - mold\nowrap ?do - { { block } } - { - equal { do { 101 + 101 } } 202 - } - - group "do-in" - mold\nowrap ?do-in - { { context } { block } } - { - equal { cc: context { x: 101 } do-in cc { x + 101 } } 202 - } - - group "with" - mold\nowrap ?with - { { object } { block } } - { - equal { 1 .with { + 100 } } 101 - } - - - group "try" - mold\nowrap ?try - { { block } } - { - equal { try { 10 + unknown } |type? } 'error - } - - group "do-in\try" - mold\nowrap ?do-in\try - { { context } { block } } - { - equal { ctx: context { x: 101 } do-in\try ctx { x / 0 } |type? } 'error - } - - group "vals" - mold\nowrap ?vals - { { block } } - { - equal { x: 101 vals { x } } { 101 } - equal { name: "Anne" vals { name "Goodall" } } { "Anne" "Goodall" } - equal { y: 202 vals { 101 y y + 101 } } { 101 202 303 } - ; TODO expression guards don't work - } - - group "vals\with" - mold\nowrap ?vals\with - { { object } { block } } - { - equal { vals\with 100 { + 1 } } { 101 } - ; TODO equal { x: 303 vals\with 100 { + 1 } } { 101 } ; loops forever - } - - group "time-it" - mold\nowrap ?time-it - { { object } { block } } - { - equal { time-it { 1 + 1 } } 0 - } -} - -section "Function creating functions!?" -"Functions that create functions." -{ - - group "does" - mold\nowrap ?does - { { block } } - { - stdout { f: does { prns "Hey" } f f } "Hey Hey " - } - - group "fn" - mold\nowrap ?fn - { { block } } - { - stdout { f: fn { x } { prns x } f "A" f "B" } "A B " - } - - group "fn1" - mold\nowrap ?fn1 - { { block } } - { - stdout { f: fn1 { .prns } f "A" f "B" } "A B " - } - - group "pfn" - mold\nowrap ?pfn - { { block } } - { - equal { add10: pfn { x } { x + 10 } add10 9 } 19 - equal { x: 10 f: pfn { } { prns 1 } try { f } |type? } 'error ; prns is not a pure function so isn't avail. - equal { x: 10 f: pfn { } { x } try { f } |type? } 'error ; x from parent context is also not avail, even read - } - comment "Pure functions only have access to other pure functions." - - - group "closure" - mold\nowrap ?closure - { { block } } - { - stdout { x: 10 cl: fn { } { prns x } , context { x: 99 cl } } "99 " - stdout { x: 10 cl: closure { } { prns x } , context { x: 99 cl } } "10 " - } - - -} - - -section "Code flow combinators" -"Program flow control functions" -{ - - group "pass" - mold\nowrap ?pass - { { object } { block } } - { - equal { "xxx" .pass { + "yyy" :xy } } "xxx" - } - group "keep" - mold\nowrap ?keep - { { object } { block } } - { - equal { 10 .keep { + 1 } { * 10 } } 11 - } - group "wrap" - mold\nowrap ?wrap - { { object } { block } } - { - ; equal { wrap { 1 + 1 } { 10 + 20 } } 30 - } -} - - - - - -section "Higher order like functions" -"Functions for usual HOF patterns, map, reduce, filter." -{ - - group "map" - mold\nowrap ?map - { { block } { block } } - { - equal { map { 1 2 3 } { + 1 } } { 2 3 4 } - equal { map { } { + 1 } } { } - equal { map { "aaa" "bb" "c" } { .length? } } { 3 2 1 } - equal { map list { "aaa" "bb" "c" } { .length? } } list { 3 2 1 } - equal { map list { 3 4 5 6 } { .factor-of 3 } } list { 1 0 0 1 } - equal { map list { } { + 1 } } list { } - ; equal { map "abc" { + "-" } .join } "a-b-c-" ; TODO doesn't work, fix join - equal { map "123" { .to-integer } } { 1 2 3 } - equal { map "123" ?to-integer } { 1 2 3 } - equal { map "" { + "-" } } { } - } - group "map\pos" - mold\nowrap ?map\pos - { { block } { block } } - { - equal { map\pos { 1 2 3 } 'i { + i } } { 2 4 6 } - equal { map\pos { } 'i { + i } } { } - equal { map\pos list { 1 2 3 } 'i { + i } } list { 2 4 6 } - equal { map\pos list { } 'i { + i } } list { } - equal { map\pos "abc" 'i { + i } } { "a1" "b2" "c3" } - equal { map\pos "" 'i { + i } } { } - } - group "filter" - mold\nowrap ?filter - { { block } { block } } - { - equal { filter { 1 2 3 4 } { .even } } { 2 4 } - equal { filter { 1 2 3 4 } ?even } { 2 4 } - equal { filter { } { .even } } { } - equal { filter list { 1 2 3 4 } { .even } } list { 2 4 } - equal { filter list { 1 2 3 4 } ?even } list { 2 4 } - equal { filter list { } { .even } } list { } - equal { filter "1234" { .to-integer .even } } { "2" "4" } - equal { filter "01234" ?to-integer } { "1" "2" "3" "4" } - equal { filter "" { .to-integer .even } } { } - - } - group "seek" - mold\nowrap ?seek - { { block } { block } } - { - equal { seek { 1 2 3 4 } { .even } } 2 - equal { seek list { 1 2 3 4 } { .even } } 2 - equal { seek "1234" { .to-integer .even } } "2" - equal { try { seek { 1 2 3 4 } { > 5 } } |type? } 'error - equal { try { seek list { 1 2 3 4 } { > 5 } } |type? } 'error - equal { try { seek "1234" { .to-integer > 5 } } |type? } 'error - } - group "purge" - mold\nowrap ?purge - { { block } { block } } - { - equal { purge { 1 2 3 } { .even } } { 1 3 } - equal { purge { } { .even } } { } - equal { purge list { 1 2 3 } { .even } } list { 1 3 } - equal { purge list { } { .even } } list { } - equal { purge "1234" { .to-integer .even } } { "1" "3" } - equal { purge "" { .to-integer .even } } { } - } - group "reduce" - mold\nowrap ?reduce - { { block } { block } } - { - equal { reduce { 1 2 3 } 'acc { + acc } } 6 - equal { reduce list { 1 2 3 } 'acc { + acc } } 6 - equal { reduce "abc" 'acc { + acc } } "cba" - equal { try { reduce { } 'acc { + acc } } |type? } 'error - equal { try { reduce list { } 'acc { + acc } } |type? } 'error - equal { try { reduce "" 'acc { + acc } } |type? } 'error - } - group "fold" - mold\nowrap ?fold - { { block } { block } } - { - equal { fold { 1 2 3 } 'acc 1 { + acc } } 7 - equal { fold { } 'acc 1 { + acc } } 1 - equal { fold list { 1 2 3 } 'acc 1 { + acc } } 7 - equal { fold list { } 'acc 1 { + acc } } 1 - equal { fold "abc" 'acc "123" { + acc } } "cba123" - equal { fold "" 'acc "123" { + acc } } "123" - } - group "partition" - mold\nowrap ?partition - { { block } { block } } - { - equal { partition { 1 2 3 4 } { > 2 } } { { 1 2 } { 3 4 } } - equal { partition { "a" "b" 1 "c" "d" } { .is-integer } } { { "a" "b" } { 1 } { "c" "d" } } - equal { partition { "a" "b" 1 "c" "d" } ?is-integer } { { "a" "b" } { 1 } { "c" "d" } } - equal { partition { } { > 2 } } { { } } - equal { partition list { 1 2 3 4 } { > 2 } } list vals { list { 1 2 } list { 3 4 } } - equal { partition list { "a" "b" 1 "c" "d" } ?is-integer } list vals { list { "a" "b" } list { 1 } list { "c" "d" } } - equal { partition list { } { > 2 } } list vals { list { } } - equal { partition "aaabbccc" { , } } list { "aaa" "bb" "ccc" } - equal { partition "" { , } } list { "" } - equal { partition "aaabbccc" ?is-string } list { "aaabbccc" } - } - group "group" - mold\nowrap ?group - { { block } { block } } - ; TODO-FIX these tests sometimes fail although the printed results look identical, could be something in the comparison function (dict key ordering maybe?) - { - ; Equality for dicts doesn't yet work consistently - ;equal { { "Anne" "Mitch" "Anya" } .group { .first } } dict vals { "A" list { "Anne" "Anya" } "M" list { "Mitch" } } - ;equal { { "Anne" "Mitch" "Anya" } .group ?first } dict vals { "A" list { "Anne" "Anya" } "M" list { "Mitch" } } - ;equal { { } .group { .first } } dict vals { } - ;equal { { "Anne" "Mitch" "Anya" } .list .group { .first } } dict vals { "A" list { "Anne" "Anya" } "M" list { "Mitch" } } - ;equal { { "Anne" "Mitch" "Anya" } .list .group ?first } dict vals { "A" list { "Anne" "Anya" } "M" list { "Mitch" } } - equal { { } .list .group { .first } } dict vals { } - equal { try { { 1 2 3 4 } .group { .even } } |type? } 'error ; TODO keys can only be string currently - } - group "produce" - mold\nowrap ?produce - { { block } { block } } - { - equal { produce 5 0 { + 3 } } 15 - } - group "sum-up" - mold\nowrap ?sum-up - { { block } { block } } - { - equal { sum-up { 1 2 3 } { * 10 } } 60 - equal { sum-up { 1 2 3 } { * 2.5 } } 15.0 - equal { sum-up { 1 2.5 3.5 } { * 10 } } 70.0 - equal { sum-up { "1" "2" "3" } ?to-integer } 6 - equal { sum-up { } { * 10 } } 0 - equal { sum-up list { 1 2 3 } { * 10 } } 60 - equal { sum-up list { 1 2.5 3.5 } { * 10 } } 70.0 - equal { sum-up list { "1" "2" "3" } ?to-integer } 6 - equal { sum-up list { } { * 10 } } 0 - } -} - -section "Context related functions" -"Functions for handling and working with Context." -{ - - group "current-ctx" - mold\nowrap ?current-ctx - { } - { - equal { current-ctx |type? } 'ctx ; TODO should probably rename to context - } - - group "parent-ctx" - mold\nowrap ?parent-ctx - { } - { - equal { cc: context { f: does { parent-ctx |type? } } , cc/f } 'ctx - } - -; TODO -- make something that clears color formatting or solve it some other way -; group "ls" -; mold\nowrap ?ls -; { } -; { -; example { cc: context { x: 123 , ls } } 'ctx -; } - - group "context" - mold\nowrap ?context - { { block } } - { - equal { context { x: does { 1 + 1 } } :cc , cc/x } 2 - } - - group "isolate" - mold\nowrap ?isolate - { { block } } - { - equal { isolate { x: does { 1 + 1 } } :cc , try { cc/x } |type? } 'error - } - - group "raw-context" - mold\nowrap ?raw-context - { { block } } - { - equal { cc: raw-context { name: "Jim" } |type? } 'ctx - } - - group "private" - mold\nowrap ?private - { { block } } - { - equal { private { x: 3 , x + 30 } :x } 33 - } - - group "extend" - mold\nowrap ?extend - { { block } } - { - equal { c1: context { x: 1 } , c2: extend c1 { y: 2 } , c2/x } 1 - } - - group "bind" - mold\nowrap ?bind - { { block } } - { - equal { isolate { x: does { 1 + 1 } } |bind current-ctx :cc , cc/x } 2 - } - - group "unbind" - mold\nowrap ?unbind - { { block } } - { - equal { context { x: does { 1 + 1 } } |unbind :cc , try { cc/x } |type? } 'error - } - -} - -$ section "Spreadsheet related functions" -"Functions for handling and working with Context." -{ - - group "spreadsheet" - mold\nowrap ?spreadsheet - { } - { - equal { spreadsheet { 'id 'name 'score } } 'ctx ; TODO should probably rename to context - } - -} $ - -section "Failure handling functions" -"Functions for handling and working with Context." -{ - - group "fail" - mold\nowrap ?fail - { } - { - equal { fail 404 |disarm |code? } 404 - equal { fail "message" |disarm |mold } "Error: message " - equal { fail { 501 "auth error" } |disarm |mold } "Error(501): auth error " - } - - group "failure" - mold\nowrap ?failure - { } - { - equal { failure 403 |type? } 'error ; todo --- should be failure? - } - group "code?" - mold\nowrap ?fail - { } - { - equal { failure 403 |code? } 403 - } - - group "disarm" - mold\nowrap ?fail - { } - { - equal { failure 403 |disarm } failure 403 - } - - group "failed?" - mold\nowrap ?fail - { } - { - equal { fail 403 |failed? } 1 - } - - group "check" - mold\nowrap ?fail - { } - { - equal { 1 / 0 |check 501 |code? } 501 - } - - group "fix" - mold\nowrap ?fail - { } - { - equal { 100 / 0 |fix { 99 } |+ 1 } 100 - equal { 100 / 1 |fix { 99 } |+ 1 } 101 - } - - group "assert-equal" - mold\nowrap ?assert-equal - { } - { - equal { assert-equal 10 10 } 1 - equal { assert-equal 10 20 |disarm |type? } 'error - } - -} - - - -section "Functions that change values in-place" -"" -{ - - group "inc!" - mold\nowrap ?inc! - { { word } } - { - equal { a: 100 , inc! 'a } 101 - equal { a: 100 , inc! 'a , a } 101 - } - - group "change!" - mold\nowrap ?change! - { { any } { word } } - { - equal { a: 1 change! 2 'a } 1 - equal { a: 2 change! 2 'a } 0 - } - - comment "Functions that change values in-place are used more rarely in Rye and have ! at the end." - - group "remove-last!" - mold\nowrap ?remove-last! - { { word } } - { - equal { b: { 1 2 3 } , remove-last! 'b , b } { 1 2 } - } - - group "append!" - mold\nowrap ?append! - { { word } { object } } - { - equal { b: { 1 2 3 } , append! 4 'b , b } { 1 2 3 4 } - equal { b: { 1 2 3 } , append! { 4 5 } 'b , b } { 1 2 3 { 4 5 } } - } - - group "change\ nth!" - mold\nowrap ?change\nth! - { { word } { object } } - { - equal { b: { 1 2 3 } , change\nth! b 2 4 } { 1 4 3 } - equal { b: { 1 2 3 } , change\nth! b 2 { 4 5 } } { 1 { 4 5 } 3 } - equal { b: list { 1 2 3 } , change\nth! b 2 4 } list { 1 4 3 } - equal { b: list { 1 2 3 } , change\nth! b 2 list { 4 5 } } list vals { 1 list { 4 5 } 3 } - equal { try { b: { 1 2 3 } , change\nth! b 4 0 } |type? } 'error - equal { try { b: list { 1 2 3 } , change\nth! b 4 0 } |type? } 'error - } - - - group "sort!" - mold\nowrap ?append! - { { word } { object } } - { - equal { b: { 4 1 7 2 } , sort! b , b } { 1 2 4 7 } ; TOTHINK -- should it accept tagword or block directly? - } - group "reverse!" - mold\nowrap ?append! - { { word } { object } } - { - ; equal { b: { 4 1 7 2 } , reverse! b , b } { 2 7 1 4 } ; TOTHINK -- should it accept tagword or block directly? - } - -} - - -; TODO equal { list { 3 4 5 6 } |map { .factor-of 3 } } list { 1 0 0 1 } - - -end \ No newline at end of file diff --git a/tests/main.rye b/tests/main.rye index 61a952d9..72d6cee5 100644 --- a/tests/main.rye +++ b/tests/main.rye @@ -15,7 +15,7 @@ test-framework: context { group: fn { name descr args code } { inc! 'cnt print " GROUP: " + name , do code } equal: fn { test res } { - do-in root-ctx test :got = res + do\in root-ctx test :got = res |either { print " - OK" } { inc! 'failed , print join { " - Error: expected " mold res ", got " mold got } } } @@ -23,7 +23,7 @@ test-framework: context { } stdout: fn { test res } { - capture-stdout { do-in root-ctx test } :got = res + capture-stdout { do\in root-ctx test } :got = res |either { print " - OK" } { inc! 'failed , print join { " - Error: expected " mold res ", got " mold got } } } @@ -42,7 +42,7 @@ docs-framework: context { group: fn { name descr args code } { print join { "

" name "

" descr "

" } , do code , print "
" } equal: fn { test res } { - do-in root-ctx test :got = res + do\in root-ctx test :got = res |if { print join [ "
" mold\nowrap test newline "; returns "  replace mold got newline newline + "; "  "
" ] } } @@ -50,7 +50,7 @@ docs-framework: context { } stdout: fn { test res } { - capture-stdout { do-in root-ctx test } :got = res + capture-stdout { do\in root-ctx test } :got = res 1 |if { print join [ "
" mold\nowrap test newline "; prints " replace mold\nowrap got newline newline + "; " "
" ] } } @@ -69,7 +69,7 @@ generate-menu: fn { menu } { generate-doc-file: fn { filename menu } { join { %tpl/header.html .read .replace "{{title}}" capitalize filename ; |replace "{{menu}}" generate-menu menu - capture-stdout { do-in docs-framework load to-file filename + ".rye" } + capture-stdout { do\in docs-framework load to-file filename + ".rye" } read %tpl/footer.html } |write* to-file filename + ".html" } @@ -79,7 +79,7 @@ menu: { "basics" "structures" "validation" "misc" } switch arg { "test" { errors: 0 - for menu { + ".rye" |to-file |load |do-in* test-framework |+ errors :errors } + for menu { + ".rye" |to-file |load |do\in* test-framework |+ errors :errors } print "===============" print "FAILED TESTS: " + errors } diff --git a/tests/structures.rye b/tests/structures.rye index a057b514..f2ed0e69 100644 --- a/tests/structures.rye +++ b/tests/structures.rye @@ -519,7 +519,7 @@ section "Serializers and loaders" mold\nowrap ?dump { { any } { word } } { - equal { fn { x } { x + 1 } |dump } { fn { x } { x ._+ 1 } } + equal { fn { x } { x + 1 } |dump } "fn { x } { x ._+ 1 }" } group "doc\of?"