Exemple #1
0
// Returns true if the string at arg0 starts with any of the following strings.
// Args:
// 0 - The source string
// 1..n - The prefixes to test
// Returns:
// true if the source string starts with any of the specified prefixes
func (s *StringsMod) strings_HasPrefix(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	src := args[0].String()
	for _, v := range args[1:] {
		if strings.HasPrefix(src, v.String()) {
			return runtime.Bool(true)
		}
	}
	return runtime.Bool(false)
}
Exemple #2
0
func (aEnt *agoraEnt) isType(entType game.EntType) runtime.FuncFn {
	return func(args ...runtime.Val) runtime.Val {
		aEnt.jm.engine.Pause()
		defer aEnt.jm.engine.Unpause()
		ent := aEnt.jm.engine.GetState().(*game.Game).Ents[aEnt.gid]
		if ent == nil {
			return runtime.Bool(false)
		}
		return runtime.Bool(ent.Type() == entType)
	}
}
Exemple #3
0
func TestMin(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	mm := new(MathMod)
	mm.SetCtx(ctx)

	cases := []struct {
		src []runtime.Val
		exp runtime.Val
	}{
		0: {
			src: []runtime.Val{runtime.Number(3), runtime.Number(0), runtime.Number(-12.74), runtime.Number(1)},
			exp: runtime.Number(-12.74),
		},
		1: {
			src: []runtime.Val{runtime.String("24"), runtime.Bool(true), runtime.Number(12.74)},
			exp: runtime.Number(1),
		},
		2: {
			src: []runtime.Val{runtime.Number(0), runtime.String("0")},
			exp: runtime.Number(0),
		},
	}

	for i, c := range cases {
		ret := mm.math_Min(c.src...)
		if ret != c.exp {
			t.Errorf("[%d] - expected %f, got %f", i, c.exp.Float(), ret.Float())
		}
	}
}
Exemple #4
0
func createFileInfo(fi os.FileInfo) runtime.Val {
	o := runtime.NewObject()
	o.Set(runtime.String("Name"), runtime.String(fi.Name()))
	o.Set(runtime.String("Size"), runtime.Number(fi.Size()))
	o.Set(runtime.String("IsDir"), runtime.Bool(fi.IsDir()))
	return o
}
Exemple #5
0
func (jm *JotaModule) Param(vs ...runtime.Val) runtime.Val {
	jm.dieOnTerminated()
	jm.paramsMutex.Lock()
	defer jm.paramsMutex.Unlock()
	paramName := vs[0].String()
	value, ok := jm.params[paramName]
	if !ok {
		return runtime.Nil
	}
	switch t := value.(type) {
	case string:
		return runtime.String(t)
	case bool:
		return runtime.Bool(t)
	case int:
		return runtime.Number(t)
	case float64:
		return runtime.Number(t)
	case linear.Vec2:
		return jm.newVec(t.X, t.Y)
	case game.Gid:
		return jm.newEnt(t)
	default:
		base.Error().Printf("Requested parameter of unexpected type: %T", t)
		return runtime.Nil
	}
}
Exemple #6
0
func TestStringsConcat(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)
	sm := new(StringsMod)
	sm.SetCtx(ctx)
	ret := sm.strings_Concat(runtime.String("hello"), runtime.Number(12), runtime.Bool(true), runtime.String("end"))
	exp := "hello12trueend"
	if ret.String() != exp {
		t.Errorf("expected %s, got %s", exp, ret)
	}
}
Exemple #7
0
func TestOsWriteFile(t *testing.T) {
	cases := []struct {
		src []runtime.Val
		exp string
	}{
		0: {
			exp: "",
		},
		1: {
			src: []runtime.Val{runtime.String("hello")},
			exp: "hello",
		},
		2: {
			src: []runtime.Val{runtime.String("string"), runtime.Number(3), runtime.Bool(true),
				runtime.Nil, runtime.Number(1.23)},
			exp: "string3truenil1.23",
		},
	}
	fn := "./testdata/writefile.txt"
	ctx := runtime.NewCtx(nil, nil)
	om := new(OsMod)
	om.SetCtx(ctx)
	for i, c := range cases {
		args := append([]runtime.Val{runtime.String(fn)}, c.src...)
		ret := om.os_WriteFile(args...)
		b, e := ioutil.ReadFile(fn)
		if e != nil {
			panic(e)
		}
		got := string(b)
		if ret.Int() != int64(len(c.exp)) {
			t.Errorf("[%d] - expected %d, got %d", i, len(c.exp), ret.Int())
		}
		if got != c.exp {
			t.Errorf("[%d] - expected '%s', got '%s'", i, c.exp, got)
		}
	}
}
Exemple #8
0
func (fp *FilepathMod) filepath_IsAbs(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.Bool(filepath.IsAbs(args[0].String()))
}
Exemple #9
0
func (m *MathMod) math_IsNaN(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(1, args)
	return runtime.Bool(math.IsNaN(args[0].Float()))
}
Exemple #10
0
func (m *MathMod) math_IsInf(args ...runtime.Val) runtime.Val {
	runtime.ExpectAtLeastNArgs(2, args)
	return runtime.Bool(math.IsInf(args[0].Float(), int(args[1].Int())))
}
Exemple #11
0
func TestFmtPrint(t *testing.T) {
	ctx := runtime.NewCtx(nil, nil)

	cases := []struct {
		src   []runtime.Val
		exp   string
		expln string
		start bool
	}{
		0: {
			src: []runtime.Val{runtime.Nil},
			exp: "nil",
		},
		1: {
			src:   []runtime.Val{runtime.Bool(true), runtime.Bool(false)},
			exp:   "truefalse",
			expln: "true false",
		},
		2: {
			// Ok, so print does *NOT* add spaces when the value is a native string
			src:   []runtime.Val{runtime.String("string"), runtime.Number(0), runtime.Number(-1), runtime.Number(17), runtime.String("pi"), runtime.Number(3.1415)},
			exp:   "string0-117pi3.1415",
			expln: "string 0 -1 17 pi 3.1415",
		},
		3: {
			src: []runtime.Val{runtime.String("func:"),
				runtime.NewNativeFunc(ctx, "", func(args ...runtime.Val) runtime.Val { return runtime.Nil })},
			exp:   "func:<func  (",
			expln: "func: <func  (",
			start: true,
		},
		4: {
			src: []runtime.Val{runtime.NewObject()},
			exp: "{}",
		},
	}

	fm := new(FmtMod)
	fm.SetCtx(ctx)
	buf := bytes.NewBuffer(nil)
	ctx.Stdout = buf
	for i, c := range cases {
		for j := 0; j < 2; j++ {
			var res runtime.Val
			buf.Reset()
			if j == 1 {
				if c.expln != "" {
					c.exp = c.expln
				}
				if !c.start {
					c.exp += "\n"
				}
				res = fm.fmt_Println(c.src...)
			} else {
				res = fm.fmt_Print(c.src...)
			}
			if (c.start && !strings.HasPrefix(buf.String(), c.exp)) || (!c.start && c.exp != buf.String()) {
				t.Errorf("[%d] - expected %s, got %s", i, c.exp, buf.String())
			}
			if !c.start && res.Int() != int64(len(c.exp)) {
				t.Errorf("[%d] - expected return value of %d, got %d", i, len(c.exp), res.Int())
			}
		}
	}
}