eval/vals: In FromGo, keep float64 as is.

This addresses #816.
This commit is contained in:
Qi Xiao 2019-04-26 23:29:20 +01:00
parent 3927f7cf75
commit 321a74da70
8 changed files with 27 additions and 24 deletions

View File

@ -4,9 +4,10 @@ import "testing"
func TestBuiltinFnContainer(t *testing.T) {
Test(t,
That(`range 3`).Puts("0", "1", "2"),
That(`range 1 3`).Puts("1", "2"),
That(`range 0 10 &step=3`).Puts("0", "3", "6", "9"),
That(`range 3`).Puts(0.0, 1.0, 2.0),
That(`range 1 3`).Puts(1.0, 2.0),
That(`range 0 10 &step=3`).Puts(0.0, 3.0, 6.0, 9.0),
That(`repeat 4 foo`).Puts("foo", "foo", "foo", "foo"),
That(`explode [foo bar]`).Puts("foo", "bar"),
@ -19,8 +20,9 @@ func TestBuiltinFnContainer(t *testing.T) {
That(`echo foobar | all`).Prints("foobar\n"),
That(`{ put foo bar; echo foobar } | all`).Puts(
"foo", "bar").Prints("foobar\n"),
That(`range 100 | take 2`).Puts("0", "1"),
That(`range 100 | drop 98`).Puts("98", "99"),
That(`range 100 | take 2`).Puts(0.0, 1.0),
That(`range 100 | drop 98`).Puts(98.0, 99.0),
That(`has-key [foo bar] 0`).Puts(true),
That(`has-key [foo bar] 0:1`).Puts(true),

View File

@ -11,9 +11,9 @@ func TestBuiltinFnFlow(t *testing.T) {
That(`echo "1\n233" | each $put~`).Puts("1", "233"),
That(`each $put~ [1 233]`).Puts("1", "233"),
That(`range 10 | each [x]{ if (== $x 4) { break }; put $x }`).
Puts("0", "1", "2", "3"),
Puts(0.0, 1.0, 2.0, 3.0),
That(`range 10 | each [x]{ if (== $x 4) { fail haha }; put $x }`).
Puts("0", "1", "2", "3").Errors(),
Puts(0.0, 1.0, 2.0, 3.0).Errors(),
// TODO: test peach
That(`fail haha`).Errors(),

View File

@ -1,6 +1,9 @@
package eval
import "testing"
import (
"math"
"testing"
)
func TestBuiltinFnNum(t *testing.T) {
Test(t,
@ -18,13 +21,13 @@ func TestBuiltinFnNum(t *testing.T) {
That(">= 3 2 3").Puts(false),
// TODO test more edge cases
That("+ 233100 233").Puts("233333"),
That("- 233333 233100").Puts("233"),
That("- 233").Puts("-233"),
That("* 353 661").Puts("233333"),
That("/ 233333 353").Puts("661"),
That("/ 1 0").Puts("+Inf"),
That("^ 16 2").Puts("256"),
That("+ 233100 233").Puts(233333.0),
That("- 233333 233100").Puts(233.0),
That("- 233").Puts(-233.0),
That("* 353 661").Puts(233333.0),
That("/ 233333 353").Puts(661.0),
That("/ 1 0").Puts(math.Inf(1)),
That("^ 16 2").Puts(256.0),
That("% 23 7").Puts("2"),
)
}

View File

@ -38,7 +38,7 @@ func TestBuiltinSpecial(t *testing.T) {
// while
That("x=0; while (< $x 4) { put $x; x=(+ $x 1) }").
Puts("0", "1", "2", "3"),
Puts("0", 1.0, 2.0, 3.0),
That("x = 0; while (< $x 4) { put $x; break }").Puts("0"),
That("x = 0; while (< $x 4) { fail haha }").Errors(),

View File

@ -2,7 +2,7 @@ package eval
import "testing"
func TestOp(t *testing.T) {
func TestCompileEffect(t *testing.T) {
Test(t,
// Chunks
// ------
@ -21,7 +21,7 @@ func TestOp(t *testing.T) {
That(`echo "Albert\nAllan\nAlbraham\nBerlin" | sed s/l/1/g | grep e`).
Prints("A1bert\nBer1in\n"),
// Pure channel pipeline
That(`put 233 42 19 | each [x]{+ $x 10}`).Puts("243", "52", "29"),
That(`put 233 42 19 | each [x]{+ $x 10}`).Puts(243.0, 52.0, 29.0),
// Pipeline draining.
That(`range 100 | put x`).Puts("x"),
// TODO: Add a useful hybrid pipeline sample
@ -55,7 +55,7 @@ func TestOp(t *testing.T) {
// Spacey assignment.
That("a @b = 2 3 foo; put $a $b[1]").Puts("2", "foo"),
// Spacey assignment with temporary assignment
That("x = 1; x=2 y = (+ 1 $x); put $x $y").Puts("1", "3"),
That("x = 1; x=2 y = (+ 1 $x); put $x $y").Puts("1", 3.0),
// Redirections
// ------------

View File

@ -9,7 +9,7 @@ import (
"github.com/elves/elvish/util"
)
func TestValue(t *testing.T) {
func TestCompileValue(t *testing.T) {
Test(t,
// Compounding
// -----------
@ -72,7 +72,7 @@ func TestValue(t *testing.T) {
// Closure captures new local variables every time
That(`fn f []{ x=0; put []{x=(+ $x 1)} []{put $x} }
{inc1,put1}=(f); $put1; $inc1; $put1
{inc2,put2}=(f); $put2; $inc2; $put2`).Puts("0", "1", "0", "1"),
{inc2,put2}=(f); $put2; $inc2; $put2`).Puts("0", 1.0, "0", 1.0),
// Rest argument.
That("[x @xs]{ put $x $xs } a b c").Puts("a", vals.MakeList("b", "c")),

View File

@ -79,8 +79,6 @@ func FromGo(a interface{}) interface{} {
switch a := a.(type) {
case int:
return strconv.Itoa(a)
case float64:
return strconv.FormatFloat(a, 'g', -1, 64)
case rune:
return string(a)
default:

View File

@ -46,7 +46,7 @@ func TestScanToGo(t *testing.T) {
var fromGoTests = tt.Table{
tt.Args(12).Rets("12"),
tt.Args(1.5).Rets("1.5"),
tt.Args(1.5).Rets(1.5),
tt.Args('x').Rets("x"),
tt.Args(nil).Rets(nil),
tt.Args(someType{"foo"}).Rets(someType{"foo"}),