Skip to content

Commit

Permalink
Merge pull request #381 from refaktor/allin
Browse files Browse the repository at this point in the history
improved the testing script in tests/
  • Loading branch information
refaktor authored Oct 22, 2024
2 parents d2334b0 + 8b26e20 commit 629d52e
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 40 deletions.
10 changes: 5 additions & 5 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 22 additions & 3 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions evaldo/builtins_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{}
Expand All @@ -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?")
}
Expand Down
86 changes: 86 additions & 0 deletions evaldo/builtins_term.go
Original file line number Diff line number Diff line change
@@ -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)
},
},
}
72 changes: 44 additions & 28 deletions tests/main.rye
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,45 @@ 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 }
probe test |try |probe
}
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 } {
}

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 }
}


}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 629d52e

Please sign in to comment.