Esempio n. 1
0
func MultiValueReturnSpec(c gospec.Context) {
	c.Specify("Functions with zero or more than one return values work.", func() {
		context := polish.MakeContext()
		polish.AddIntMathContext(context)
		rev3 := func(a, b, c int) (int, int, int) {
			return c, b, a
		}
		context.AddFunc("rev3", rev3)
		rev5 := func(a, b, c, d, e int) (int, int, int, int, int) {
			return e, d, c, b, a
		}
		context.AddFunc("rev5", rev5)

		res, err := context.Eval("- - - - rev5 rev3 1 2 rev3 4 5 6")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		// - - - - rev5 rev3 1 2 rev3 4 5 6
		// - - - - rev5 rev3 1 2 6 5 4
		// - - - - rev5 6 2 1 5 4
		// - - - - 4 5 1 2 6
		// - - - -1 1 2 6
		// - - -2 2 6
		// - -4 6
		// -10
		c.Expect(int(res[0].Int()), Equals, -10)
	})
}
Esempio n. 2
0
func ParsingSpec(c gospec.Context) {
	c.Specify("Whitespace is parsed properly.", func() {
		context := polish.MakeContext()
		polish.AddIntMathContext(context)
		res, err := context.Eval("    +           1                      3")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(int(res[0].Int()), Equals, 4)
	})
}
Esempio n. 3
0
func ErrorSpec(c gospec.Context) {
	c.Specify("Type-mismatch panics are caught and returned as errors.", func() {
		context := polish.MakeContext()
		polish.AddIntMathContext(context)
		_, err := context.Eval("+ 1.0 2.0")
		c.Assume(err.Error(), Not(Equals), nil)
	})
	c.Specify("Panics from inside functions are caught and returned as errors.", func() {
		context := polish.MakeContext()
		context.AddFunc("panic", func() { panic("rawr") })
		_, err := context.Eval("panic")
		c.Assume(err.Error(), Not(Equals), nil)
	})
}
Esempio n. 4
0
func ParseOrderSpec(c gospec.Context) {
	c.Specify("Parse order can be changed.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		context.SetParseOrder(polish.Float) // Only parse as floats
		res, err := context.Eval("    +           1                      3")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Float(), Equals, 4.0)

		context = polish.MakeContext()
		polish.AddIntMathContext(context)
		context.SetParseOrder(polish.Float) // Only parse as floats
		res, err = context.Eval("    +           1                      3")
		c.Assume(len(res), Equals, 0)
		c.Assume(err, Not(Equals), nil)
	})
}
Esempio n. 5
0
func IntOperatorSpec(c gospec.Context) {
	c.Specify("All standard int operators parse.", func() {
		context := polish.MakeContext()
		polish.AddIntMathContext(context)
		c.Specify("+ works", func() {
			_, err := context.Eval("+ 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("- works", func() {
			_, err := context.Eval("- 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("* works", func() {
			_, err := context.Eval("* 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("/ works", func() {
			_, err := context.Eval("/ 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("< works", func() {
			_, err := context.Eval("< 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("<= works", func() {
			_, err := context.Eval("<= 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("> works", func() {
			_, err := context.Eval("> 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify(">= works", func() {
			_, err := context.Eval(">= 1 2")
			c.Assume(err, Equals, nil)
		})
		c.Specify("== works", func() {
			_, err := context.Eval("== 1 2")
			c.Assume(err, Equals, nil)
		})
	})
}
Esempio n. 6
0
func IntContextSpec(c gospec.Context) {
	c.Specify("Int context works properly.", func() {
		context := polish.MakeContext()
		polish.AddIntMathContext(context)
		v1 := (3*3*3)/(2*2*2*2) - (6 * 6)
		v2 := (5 * 5 * 5 * 5 * 5)
		res, err := context.Eval("- / ^ 3 3 ^ 2 4 ^ 6 2")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(int(res[0].Int()), Equals, v1)
		res, err = context.Eval("^ 5 5")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(int(res[0].Int()), Equals, v2)
		res, err = context.Eval("< - / ^ 3 3 ^ 2 4 ^ 6 2 ^ 5 5")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, v1 < v2)
	})
}