Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions eval/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package eval

import (
"fmt"
"math"
"testing"

"github.com/chermehdi/comet/parser"
"github.com/chermehdi/comet/std"
"github.com/stretchr/testify/assert"
"math"
"testing"
)

func TestEvaluator_Eval_Integers(t *testing.T) {
Expand Down Expand Up @@ -561,3 +562,41 @@ func assertFoundInScope(t *testing.T, ev *Evaluator, name string, expectedType s
func parseOrDie(s string) parser.Node {
return parser.New(s).Parse()
}

func ExampleBuiltinPrintf() {
// (anouard24): I'm not pround of this
// but i'm still learning Golang
// its enough for now
tests := []string{
`printf("Hi")`,
`println()
printf("Hi %d", 2021)`,
`println()
printf("% 7d", 5)`,
`println()
printf("%s", "Test")`,
`println()
printf("%8s", "Test")`,
`println()
printf("%t", true)`,
`println()
printf("%t", false)`,
`println()
printf("%04d%9s%t", 7, "Comet ", true)`,
}
evaluator := NewEvaluator()
for _, test := range tests {
rootNode := parseOrDie(test)
evaluator.Eval(rootNode)
}

// Output:
// Hi
// Hi 2021
// 5
// Test
// Test
// true
// false
// 0007 Comet true
}
7 changes: 6 additions & 1 deletion std/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ var Builtins = []*Builtin{
transArgs = append(transArgs, extractPrimitive(args[i]))
}
format := args[0].(*CometStr)
fmt.Printf(format.Value, transArgs)

if len(transArgs) > 0 {
fmt.Printf(format.Value, transArgs...)
} else {
fmt.Printf(format.Value)
}
return NopInstance
},
},
Expand Down