From 182c1ff693de367d61590ab56230b9c48bf8dc53 Mon Sep 17 00:00:00 2001 From: refaktor Date: Wed, 23 Oct 2024 00:17:17 +0200 Subject: [PATCH 1/3] improved the testing script in tests/ --- env/object.go | 10 +++--- evaldo/builtins.go | 25 ++++++++++++-- evaldo/builtins_html.go | 8 ++--- tests/basics.rye | 2 +- tests/main.rye | 72 +++++++++++++++++++++++++---------------- 5 files changed, 76 insertions(+), 41 deletions(-) diff --git a/env/object.go b/env/object.go index 2a1e369d..a48d8509 100644 --- a/env/object.go +++ b/env/object.go @@ -150,13 +150,13 @@ func (i Decimal) GetKind() int { func (i Decimal) Equal(o Object) bool { if i.Type() != o.Type() { - fmt.Println(i.Type()) - fmt.Println(o.Type()) - fmt.Println("TYPES") + // fmt.Println(i.Type()) + // fmt.Println(o.Type()) + // fmt.Println("TYPES") return false } - fmt.Println(i.Value) - fmt.Println(o.(Decimal).Value) + // fmt.Println(i.Value) + // fmt.Println(o.(Decimal).Value) return i.Value == o.(Decimal).Value } diff --git a/evaldo/builtins.go b/evaldo/builtins.go index f01e6047..732218b9 100644 --- a/evaldo/builtins.go +++ b/evaldo/builtins.go @@ -1895,11 +1895,29 @@ var builtins = map[string]*env.Builtin{ } return nil }, - }, */ + }, */ + + "random": { + Argsn: 1, + Doc: "Accepts an integer n and returns a random integer between 0 and n in the half-open interval [0,n).", + Pure: true, + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + switch arg := arg0.(type) { + case env.Block: + val, err := rand.Int(rand.Reader, big.NewInt(int64(len(arg.Series.S)))) + if err != nil { + return MakeBuiltinError(ps, err.Error(), "random-integer") + } + return arg.Series.S[int(val.Int64())] + default: + return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "random-integer") + } + }, + }, - "random-integer": { + "random\\integer": { Argsn: 1, - Doc: "Accepts an integer n and eturns a random integer between 0 and n in the half-open interval [0,n).", + Doc: "Accepts an integer n and returns a random integer between 0 and n in the half-open interval [0,n).", Pure: true, Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { switch arg := arg0.(type) { @@ -8253,6 +8271,7 @@ func RegisterBuiltins(ps *env.ProgramState) { RegisterBuiltinsInContext(Builtins_math, ps, "math") osctx := RegisterBuiltinsInContext(Builtins_os, ps, "os") RegisterBuiltinsInSubContext(Builtins_pipes, ps, osctx, "pipes") + RegisterBuiltinsInContext(Builtins_term, ps, "term") // ## Archived modules // RegisterBuiltins2(Builtins_gtk, ps, "gtk") // RegisterBuiltins2(Builtins_nats, ps, "nats") diff --git a/evaldo/builtins_html.go b/evaldo/builtins_html.go index d8c857ba..a7a74e9c 100644 --- a/evaldo/builtins_html.go +++ b/evaldo/builtins_html.go @@ -311,7 +311,7 @@ myloop: // fmt.Println("IN BLOCK") ser := es.Ser // TODO -- make helper function that "does" a block es.Ser = node.Code.Series - EvalBlockInj(es, env.NewString(tok.Data), true) + EvalBlockInj(es, *env.NewString(tok.Data), true) if es.ErrorFlag { return es.Res } @@ -420,14 +420,14 @@ var Builtins_html = map[string]*env.Builtin{ switch n := arg1.(type) { case env.Integer: if int(n.Value) < len(tok.Attr) { - return env.NewString(tok.Attr[int(n.Value)].Val) + return *env.NewString(tok.Attr[int(n.Value)].Val) } else { return env.Void{} } case env.Word: for _, a := range tok.Attr { if a.Key == ps.Idx.GetWord(n.Index) { - return env.NewString(a.Val) + return *env.NewString(a.Val) } } return env.Void{} @@ -451,7 +451,7 @@ var Builtins_html = map[string]*env.Builtin{ case env.Native: switch tok := tok1.Value.(type) { case html.Token: - return env.NewString(tok.Data) + return *env.NewString(tok.Data) default: return MakeBuiltinError(ps, "Not xml-start element.", "rye-html-start//name?") } diff --git a/tests/basics.rye b/tests/basics.rye index c0798169..4690c4ab 100644 --- a/tests/basics.rye +++ b/tests/basics.rye @@ -196,7 +196,7 @@ section "Working with numbers" mold\nowrap ?mod { { integer } { integer } } { - equal { 3 .mod 2 } 1 + equal { 3 .mod 2 } 2 ; TODO } diff --git a/tests/main.rye b/tests/main.rye index 889a1168..21ff09be 100644 --- a/tests/main.rye +++ b/tests/main.rye @@ -10,9 +10,9 @@ test-framework: context { cnt: 0 failed: 0 - section: fn { name descr code } { print join { "SECTION: " name newline " * " descr } , do code } + section: fn { name descr code } { term/magenta term/bold print name term/reset\all print " " + descr , do code print "" } - group: fn { name descr args code } { inc! 'cnt print " GROUP: " + name , do code } + group: fn { name descr args code } { inc! 'cnt term/blue prns " " + name term/reset\all , do code } error-: fn { test } { ; try { do\in root-ctx test } @@ -20,12 +20,12 @@ test-framework: context { } error: fn { test } { ; try { do\in root-ctx test } - try test :got |type? |= 'error |either { print join { " - OK: " mold got } } { inc! 'failed , print join { " - Error: expected error but got: " mold got } } + try test :got |type? |= 'error |either { prns join { "✓" mold got } } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { newline "expected error but got: " mold got } } } equal: fn { test res } { do\in root-ctx test :got = res - |either { print " - OK" } { inc! 'failed , print join { " - Error: expected " mold res ", got " mold got } } + |either { term/green prns "✓" term/reset\all } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { "expected " mold res ", got " mold got } } } equal\todo: fn { test res } { @@ -33,14 +33,22 @@ test-framework: context { stdout: fn { test res } { capture-stdout { do\in root-ctx test } :got = res - |either { print " - OK" } { inc! 'failed , print join { " - Error: expected " mold res ", got " mold got } } + |either { term/green prns "✓" term/reset\all } { inc! 'failed , term/red prns " ✗ Failed:" term/reset\all print join { "expected " mold res ", got " mold got } } } comment: fn { str } { } - end: fn { } { wrap { print "---------------" } { print "CNT: " + cnt , print "FAILED: " + failed , if failed > 0 { exit 1 } } } + end: fn { } { ;prns "Tests:" term/bold prn cnt term/reset\bold , print "FAILED: " + failed , if failed > 0 { exit 1 } } + prns "\nTests:" term/bold prn cnt term/reset\bold + either is-zero failed + { term/bold term/green print "\nALL PASSED" term/reset\all } + { term/red print "\nFAILED: " + failed term/reset\all } + + if failed > 0 { exit 1 } + } + } @@ -99,32 +107,40 @@ Examples: } first args |^fix { print-help } |switch { - test { - sections: { } - either args .length? > 1 { - either ( second args ) = 'ls { - print "Test sections available:" - menu .for { .concat* " * " |print } - } { - sections:: [ second args ] - } +test { + sections: { } + either args .length? > 1 { + either ( second args ) = 'ls { + print "Test sections available:" + menu .for { .concat* " * " |print } } { - sections:: menu - } - if sections .length? > 0 { ; todo add is-empty builtin - errors: 0 - for sections { .to-string + ".rye" |to-file |load |^check { "group does not exist!" } |do\in* test-framework |+ errors ::errors } - print "===================" - print " FAILED TESTS: " + errors - print "" - print "run `rye .` to see the new test tool options" + sections:: [ second args ] } + } { + sections:: menu } - doc { - for menu { .generate-doc-file menu } - print "docs generated" + if sections .length? > 0 { ; todo add is-empty builtin + errors: 0 + for sections { + .to-string .pass { term/bold , prns "\n#" , .to-upper .prns , print "#\n" } + |+ ".rye" |to-file |load |^check { "group does not exist!" } |do\in* test-framework |+ errors ::errors } + print "" + + if ( length? sections ) > 1 { + term/orange + term/bold + print "TOTAL FAILED TESTS: " + errors + print "" + term/reset\all + } + print "" } - _ { print-help } +} +doc { + for menu { .generate-doc-file menu } + print "docs generated" +} +_ { print-help } } ; maybe we need this at some point From 5d00ac210d916ab7a96299ac9b910d803320587f Mon Sep 17 00:00:00 2001 From: refaktor Date: Wed, 23 Oct 2024 00:20:23 +0200 Subject: [PATCH 2/3] adding missing builtins term --- evaldo/builtins_term.go | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 evaldo/builtins_term.go diff --git a/evaldo/builtins_term.go b/evaldo/builtins_term.go new file mode 100644 index 00000000..dfc549a8 --- /dev/null +++ b/evaldo/builtins_term.go @@ -0,0 +1,86 @@ +//go:build !b_no_term +// +build !b_no_term + +package evaldo + +import ( + "github.com/refaktor/rye/env" + "github.com/refaktor/rye/term" +) + +var Builtins_term = map[string]*env.Builtin{ + + "red": { + Argsn: 0, + Doc: "Take input from a user.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorRed() + return env.NewInteger(1) + }, + }, + "blue": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBlue() + return env.NewInteger(1) + }, + }, + "green": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorGreen() + return env.NewInteger(1) + }, + }, + "orange": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorOrange() + return env.NewInteger(1) + }, + }, + "magenta": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorMagenta() + return env.NewInteger(1) + }, + }, + "cyan": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBlue() + return env.NewInteger(1) + }, + }, + "bold": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ColorBold() + return env.NewInteger(1) + }, + }, + "reset\\bold": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.ResetBold() + return env.NewInteger(1) + + }, + }, + "reset\\all": { + Argsn: 0, + Doc: "Open file.", + Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object { + term.CloseProps() + return env.NewInteger(1) + }, + }, +} From 8b26e208af1c84c3412c3f67817bdaaf3ee44ddd Mon Sep 17 00:00:00 2001 From: refaktor Date: Wed, 23 Oct 2024 00:23:15 +0200 Subject: [PATCH 3/3] fixing the test --- tests/basics.rye | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/basics.rye b/tests/basics.rye index 4690c4ab..c0798169 100644 --- a/tests/basics.rye +++ b/tests/basics.rye @@ -196,7 +196,7 @@ section "Working with numbers" mold\nowrap ?mod { { integer } { integer } } { - equal { 3 .mod 2 } 2 + equal { 3 .mod 2 } 1 ; TODO }