Exemplo 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)
	})
}
Exemplo n.º 2
0
func ActionSpec(c gospec.Context) {
	game.RegisterActions()
	c.Specify("Actions are loaded properly.", func() {
		basic := game.MakeAction("Basic Test")
		_, ok := basic.(*actions.BasicAttack)
		c.Expect(ok, Equals, true)
	})

	c.Specify("Actions can be gobbed without loss of type.", func() {
		buf := bytes.NewBuffer(nil)
		enc := gob.NewEncoder(buf)

		var as []game.Action
		as = append(as, game.MakeAction("Move Test"))
		as = append(as, game.MakeAction("Basic Test"))

		err := enc.Encode(as)
		c.Assume(err, Equals, nil)

		dec := gob.NewDecoder(buf)
		var as2 []game.Action
		err = dec.Decode(&as2)
		c.Assume(err, Equals, nil)

		_, ok := as2[0].(*actions.Move)
		c.Expect(ok, Equals, true)

		_, ok = as2[1].(*actions.BasicAttack)
		c.Expect(ok, Equals, true)

	})
}
Exemplo n.º 3
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)
	})
}
Exemplo n.º 4
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)
	})
}
Exemplo n.º 5
0
func TermSpec(c gospec.Context) {
	g, err := yed.ParseFromFile("state.xgml")
	c.Assume(err, Equals, nil)
	aig := ai.NewGraph()
	aig.Graph = &g.Graph
	aig.Context = polish.MakeContext()
	polish.AddIntMathContext(aig.Context)
	polish.AddIntMathContext(aig.Context)
	c.Specify("Calling AiGraph.Term() will terminate evaluation early.", func() {
		var nearest int = 7
		nearest_func := func() int {
			return nearest
		}

		dist := 0
		term := true
		dist_func := func() int {
			if nearest == 6 && term {
				aig.Term() <- nil
			}
			return dist
		}

		attacks := 0
		attack_func := func() int {
			attacks++
			return 0
		}

		aig.Context.AddFunc("dist", dist_func)
		aig.Context.AddFunc("nearest", nearest_func)
		aig.Context.AddFunc("move", func() int { nearest--; return 0 })
		aig.Context.AddFunc("wait", func() int { return 0 })
		aig.Context.AddFunc("attack", attack_func)
		aig.Eval(2, func() bool { return true })

		c.Expect(attacks, Equals, 0)
		c.Expect(nearest, Equals, 6)

		term = false
		aig.Eval(2, func() bool { return true })
		c.Expect(nearest, Equals, 4)
	})
}
Exemplo n.º 6
0
func NumRemainingValuesSpec(c gospec.Context) {
	c.Specify("Can handle any number of terms remaining after evaluation.", func() {
		context := polish.MakeContext()
		context.AddFunc("makeZero", func() {})
		context.AddFunc("makeTwo", func() (int, int) { return 1, 2 })
		res, err := context.Eval("makeTwo")
		c.Assume(len(res), Equals, 2)
		c.Assume(err, Equals, nil)
		res, err = context.Eval("makeZero")
		c.Assume(len(res), Equals, 0)
		c.Assume(err, Equals, nil)
	})
}
Exemplo n.º 7
0
func ChunkSpec(c gospec.Context) {
	g, err := yed.ParseFromFile("state.xgml")
	c.Assume(err, Equals, nil)
	aig := ai.NewGraph()
	aig.Graph = &g.Graph
	aig.Context = polish.MakeContext()
	polish.AddIntMathContext(aig.Context)
	polish.AddIntMathContext(aig.Context)
	c.Specify("cont() returning false will terminate evaluation early.", func() {
		var nearest int = 7
		nearest_func := func() int {
			return nearest
		}

		dist := 0
		term := true
		dist_func := func() int {
			if nearest == 6 && term {
				aig.Term() <- nil
			}
			return dist
		}

		attacks := 0
		attack_func := func() int {
			attacks++
			return 0
		}

		aig.Context.AddFunc("dist", dist_func)
		aig.Context.AddFunc("nearest", nearest_func)
		aig.Context.AddFunc("move", func() int { nearest--; return 0 })
		aig.Context.AddFunc("wait", func() int { return 0 })
		aig.Context.AddFunc("attack", attack_func)
		_, err := aig.Eval(4, func() bool { return false })
		// Only have time for 1 move before we terminate early
		c.Expect(err, Equals, nil)
		c.Expect(nearest, Equals, 6)
	})
}
Exemplo n.º 8
0
func Float64ContextSpec(c gospec.Context) {
	c.Specify("Float64 context works properly.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		v1 := math.E * math.Pi * math.Exp(1.23456-math.Log10(77))
		res, err := context.Eval("* e * pi ^ e - 1.23456 log10 77.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Float(), IsWithin(1e-9), v1)
		res, err = context.Eval("< e pi")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
	})
}
Exemplo n.º 9
0
func AiSpec(c gospec.Context) {
	c.Specify("Load a simple .xgml file.", func() {
		g, err := yed.ParseFromFile("state.xgml")
		c.Assume(err, Equals, nil)
		aig := ai.NewGraph()
		aig.Graph = &g.Graph
		aig.Context = polish.MakeContext()
		polish.AddIntMathContext(aig.Context)

		dist := 0
		dist_func := func() int {
			return dist
		}

		var nearest int = 7
		nearest_func := func() int {
			return nearest
		}

		attacks := 0
		attack_func := func() int {
			attacks++
			return 0
		}

		aig.Context.AddFunc("dist", dist_func)
		aig.Context.AddFunc("nearest", nearest_func)
		aig.Context.AddFunc("move", func() int { nearest--; return 0 })
		aig.Context.AddFunc("wait", func() int { return 0 })
		aig.Context.AddFunc("attack", attack_func)
		aig.Eval(2, func() bool { return true })

		c.Expect(attacks, Equals, 0)
		c.Expect(nearest, Equals, 4)
	})
}
Exemplo n.º 10
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)
	})
}
Exemplo n.º 11
0
func UnorderedTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test that the coder can decode packets out of order.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable1{})
		tr.Register(encodable2{})
		tr.Complete()

		v1 := encodable1{1, 2}
		v2 := encodable2{3}
		v3 := encodable2{4}
		v4 := encodable1{5, 6}

		b1 := bytes.NewBuffer(nil)
		err := tr.Encode(v1, b1)
		c.Assume(err, gospec.Equals, error(nil))

		b2 := bytes.NewBuffer(nil)
		err = tr.Encode(v2, b2)
		c.Assume(err, gospec.Equals, error(nil))

		b3 := bytes.NewBuffer(nil)
		err = tr.Encode(v3, b3)
		c.Assume(err, gospec.Equals, error(nil))

		b4 := bytes.NewBuffer(nil)
		err = tr.Encode(v4, b4)
		c.Assume(err, gospec.Equals, error(nil))

		buf := bytes.NewBuffer(nil)
		buf.Write(b1.Bytes())
		buf.Write(b3.Bytes())
		buf.Write(b4.Bytes())
		buf.Write(b2.Bytes())

		d1, err := tr.Decode(buf)
		c.Assume(err, gospec.Equals, error(nil))
		d3, err := tr.Decode(buf)
		c.Assume(err, gospec.Equals, error(nil))
		d4, err := tr.Decode(buf)
		c.Assume(err, gospec.Equals, error(nil))
		d2, err := tr.Decode(buf)
		c.Assume(err, gospec.Equals, error(nil))

		c.Expect(d1, gospec.Equals, v1)
		c.Expect(d2, gospec.Equals, v2)
		c.Expect(d3, gospec.Equals, v3)
		c.Expect(d4, gospec.Equals, v4)
	})
}
Exemplo n.º 12
0
func BasicTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test the coder.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable1{})
		tr.Register(encodable2{})
		tr.Register(encodable3([]byte{}))
		tr.Register(encodable4{})
		tr.Register(encodable5{})
		tr.Register(encodable7{})
		tr.Register(encodable8{})
		tr.Register(encodable9{})
		tr.Register(encodable10{})
		tr.Register(encodable11{})
		tr.Register(encodable12{})
		tr.Register(encodable13{})
		tr.Register(encodable14{})
		tr.Complete()

		c.Specify("Test that encoder handles int32.", func() {
			e1 := encodable1{1, 2}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e1, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec1, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d1, ok := dec1.(encodable1)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec1.(ider).id(), gospec.Equals, 1)
			c.Expect(d1, gospec.Equals, e1)
		})

		c.Specify("Test that encoder handles byte.", func() {
			e2 := encodable2{14}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e2, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec2, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d2, ok := dec2.(encodable2)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec2.(ider).id(), gospec.Equals, 2)
			c.Expect(d2, gospec.Equals, e2)
		})

		c.Specify("Test that encoder handles string.", func() {
			e3 := encodable3("fudgeball")
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e3, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec3, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d3, ok := dec3.(encodable3)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec3.(ider).id(), gospec.Equals, 3)
			c.Expect(string(d3), gospec.Equals, string(e3))
		})

		c.Specify("Test that encoder handles composite structs.", func() {
			e4 := encodable4{
				A: e4sub1{
					B: 10,
					C: 20,
					D: e4sub2{
						E: "foo",
						F: "bar",
					},
				},
				G: 12345,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e4, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec4, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d4, ok := dec4.(encodable4)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec4.(ider).id(), gospec.Equals, 4)
			c.Expect(d4, gospec.Equals, e4)
		})

		c.Specify("Test that encoder handles []byte.", func() {
			e5 := encodable5{
				A: []byte("Monkeyball"),
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e5, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec5, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d5, ok := dec5.(encodable5)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec5.(ider).id(), gospec.Equals, 5)
			c.Expect(string(d5.A), gospec.Equals, string(e5.A))
		})

		c.Specify("Test that encoder handles int and uint.", func() {
			e7 := encodable7{
				A: 1,
				B: 2,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e7, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec7, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d7, ok := dec7.(encodable7)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec7.(ider).id(), gospec.Equals, 7)
			c.Expect(string(d7.A), gospec.Equals, string(e7.A))
			c.Expect(string(d7.B), gospec.Equals, string(e7.B))
		})

		c.Specify("Test that encoder handles []int and []uint.", func() {
			e8 := encodable8{
				A: []int{1, 2, 3},
				B: []uint{6, 7, 8},
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e8, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec8, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d8, ok := dec8.(encodable8)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec8.(ider).id(), gospec.Equals, 8)
			c.Expect(fmt.Sprintf("%v", d8.A), gospec.Equals, fmt.Sprintf("%v", e8.A))
			c.Expect(fmt.Sprintf("%v", d8.B), gospec.Equals, fmt.Sprintf("%v", e8.B))
		})

		c.Specify("Test that encoder handles pointers.", func() {
			sub := e9sub{
				B: 123,
			}
			psub := &sub
			ppsub := &psub
			e9 := encodable9{
				A: &ppsub,
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e9, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec9, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d9, ok := dec9.(encodable9)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec9.(ider).id(), gospec.Equals, 9)
			c.Assume(d9.A, gospec.Not(gospec.Equals), (*e9sub)(nil))
			c.Expect((**d9.A).B, gospec.Equals, (**e9.A).B)
		})

		c.Specify("Test that encoder can ignore unexported fields.", func() {
			e10 := encodable10{
				A: 10,
				b: 12,
				C: "foo",
				d: "bar",
			}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e10, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec10, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d10, ok := dec10.(encodable10)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec10.(ider).id(), gospec.Equals, 10)
			c.Expect(d10.A, gospec.Equals, e10.A)
			c.Expect(d10.b, gospec.Equals, int32(0))
			c.Expect(d10.C, gospec.Equals, e10.C)
			c.Expect(d10.d, gospec.Equals, "")
		})

		c.Specify("Test that encoder can handle nil pointers.", func() {
			e11 := encodable11{}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e11, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec11, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d11, ok := dec11.(encodable11)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec11.(ider).id(), gospec.Equals, 11)
			c.Expect(d11.A, gospec.Equals, (*e11sub1)(nil))
			c.Expect(d11.B, gospec.Equals, (*e11sub2)(nil))
			c.Expect(d11.C, gospec.Equals, (Barer)(nil))
		})

		c.Specify("Test that encoder can handle aliased primitives.", func() {
			e12 := encodable12{A: 123, B: 456}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e12, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec12, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d12, ok := dec12.(encodable12)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec12.(ider).id(), gospec.Equals, 12)
			c.Expect(d12.A, gospec.Equals, (LooksLikeAnInt)(123))
			c.Expect(d12.B, gospec.Equals, (LooksLikeAUInt)(456))
		})

		c.Specify("Test that encoder can handle slices of nil values.", func() {
			ints := make([]*int, 5)
			for i := range ints {
				n := i
				ints[i] = &n
			}
			e13 := encodable13{A: make([]*int, 5), B: ints}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e13, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec13, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d13, ok := dec13.(encodable13)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec13.(ider).id(), gospec.Equals, 13)
			c.Expect(len(d13.A), gospec.Equals, len(e13.A))
			for i := range d13.A {
				c.Expect(d13.A[i], gospec.Equals, e13.A[i])
			}
			c.Expect(len(d13.B), gospec.Equals, len(e13.B))
			for i := range d13.B {
				c.Expect(*d13.B[i], gospec.Equals, *e13.B[i])
			}
		})

		c.Specify("Test that encoder can handle bools.", func() {
			e14 := encodable14{A: true, B: false}
			buf := bytes.NewBuffer(nil)
			err := tr.Encode(e14, buf)
			c.Assume(err, gospec.Equals, error(nil))
			dec14, err := tr.Decode(buf)
			c.Assume(err, gospec.Equals, error(nil))
			d14, ok := dec14.(encodable14)
			c.Assume(ok, gospec.Equals, true)
			c.Expect(dec14.(ider).id(), gospec.Equals, 14)
			c.Expect(d14.A, gospec.Equals, e14.A)
			c.Expect(d14.B, gospec.Equals, e14.B)
		})
	})
}
Exemplo n.º 13
0
func EventSpec(c gospec.Context) {
	input := gin.Make()
	AB_binding := input.MakeBinding('a', []gin.KeyId{'b'}, []bool{true})
	AB := input.BindDerivedKey("AB", AB_binding)
	CD_binding := input.MakeBinding('c', []gin.KeyId{'d'}, []bool{true})
	CD := input.BindDerivedKey("CD", CD_binding)
	AB_CD_binding := input.MakeBinding(AB.Id(), []gin.KeyId{CD.Id()}, []bool{true})
	_ = input.BindDerivedKey("AB CD", AB_CD_binding)
	events := make([]gin.OsEvent, 0)

	check := func(lengths ...int) {
		groups := input.Think(10, false, events)
		c.Assume(len(groups), Equals, len(lengths))
		for i, length := range lengths {
			c.Assume(len(groups[i].Events), Equals, length)
		}
	}

	c.Specify("Testing order 'abcd'.", func() {
		injectEvent(&events, 'a', 1, 1)
		injectEvent(&events, 'b', 1, 2)
		injectEvent(&events, 'c', 1, 3)
		injectEvent(&events, 'd', 1, 4)
		check(1, 1, 1, 1)
	})

	c.Specify("Testing order 'dbca'.", func() {
		injectEvent(&events, 'd', 1, 1)
		injectEvent(&events, 'b', 1, 2)
		injectEvent(&events, 'c', 1, 3)
		injectEvent(&events, 'a', 1, 4)
		check(1, 1, 2, 3)
	})

	c.Specify("Testing order 'dcba'.", func() {
		injectEvent(&events, 'd', 1, 1)
		injectEvent(&events, 'c', 1, 2)
		injectEvent(&events, 'b', 1, 3)
		injectEvent(&events, 'a', 1, 4)
		check(1, 2, 1, 3)
	})

	c.Specify("Testing order 'bcda'.", func() {
		injectEvent(&events, 'b', 1, 1)
		injectEvent(&events, 'c', 1, 2)
		injectEvent(&events, 'd', 1, 3)
		injectEvent(&events, 'a', 1, 4)
		check(1, 1, 1, 2)
	})

	// This test also checks that a derived key stays down until the primary key is released
	// CD is used here after D is released to trigger AB_CD
	c.Specify("Testing order 'dcbDad'.", func() {
		injectEvent(&events, 'd', 1, 1)
		injectEvent(&events, 'c', 1, 2)
		injectEvent(&events, 'b', 1, 3)
		injectEvent(&events, 'd', 0, 4)
		injectEvent(&events, 'a', 1, 5)
		injectEvent(&events, 'd', 1, 6)
		check(1, 2, 1, 1, 3, 1)
	})
}
Exemplo n.º 14
0
func BundlerSpec(c gospec.Context) {
	c.Specify("Basic Bundler functionality.", func() {
		var params core.EngineParams
		params.Id = 1234
		params.Delay = 2
		params.Frame_ms = 5
		params.Max_frames = 25
		bundles := make(chan core.FrameBundle)
		local_event := make(chan core.Event)
		var bundler core.Bundler
		bundler.Params = params
		bundler.Local_bundles = bundles
		bundler.Local_event = local_event
		bundler.Time_delta = nil
		ticker := &core.FakeTicker{}
		bundler.Ticker = ticker
		bundler.Start()
		c.Expect(1, Equals, 1)
		go func() {
			for i := 0; i < 10; i++ {
				if i%2 == 0 {
					local_event <- &EventA{i}
					local_event <- &EventB{fmt.Sprintf("%d", i)}
				} else {
					local_event <- &EventB{fmt.Sprintf("%d", i)}
					local_event <- &EventA{i}
				}
				// Advance two frames, so we will have two events per every other
				// frame.
				ticker.Inc(10)
			}
			bundler.Shutdown()
		}()
		var frame core.StateFrame = 0
		for bundle := range bundles {
			c.Expect(bundle.Frame, Equals, frame)
			events, ok := bundle.Bundle[params.Id]
			c.Assume(ok, Equals, true)
			c.Expect(len(events.Engine), Equals, 0)
			if ok {
				if frame%2 == 0 {
					c.Expect(len(events.Game), Equals, 2)
				} else {
					c.Expect(len(events.Game), Equals, 0)
				}
			}
			if frame%2 == 0 {
				c.Expect(len(bundle.Bundle), Equals, 1)
				c.Specify("checking bundle values", func() {
					index_a := 0
					if frame%4 != 0 {
						index_a = 1
					}
					ea, aok := events.Game[index_a].(*EventA)
					eb, bok := events.Game[(index_a+1)%2].(*EventB)
					c.Assume(aok, Equals, true)
					c.Assume(bok, Equals, true)
					c.Specify("checking event data", func() {
						c.Expect(ea.Data, Equals, int(frame/2))
						c.Expect(eb.Data, Equals, fmt.Sprintf("%d", frame/2))
					})
				})
			}
			frame++
		}
	})
}
Exemplo n.º 15
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)
		})
	})
}
Exemplo n.º 16
0
func Float64AndBooleanContextSpec(c gospec.Context) {
	c.Specify("Boolean context works properly with a float64 context.", func() {
		context := polish.MakeContext()
		polish.AddFloat64MathContext(context)
		polish.AddBooleanContext(context)
		res, err := context.Eval("&& < e pi >= log2 pi log2 e")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
		res, err = context.Eval("^^ < 3.0 4.0 < 4.0 3.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
		res, err = context.Eval("^^ < 3.0 4.0 < 3.0 4.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, false)
		res, err = context.Eval("! ^^ < 3.0 4.0 < 3.0 4.0")
		c.Assume(len(res), Equals, 1)
		c.Assume(err, Equals, nil)
		c.Expect(res[0].Bool(), Equals, true)
	})
}
Exemplo n.º 17
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)
	})
}
Exemplo n.º 18
0
func ConditionsSpec(c gospec.Context) {
	c.Specify("Conditions are loaded properly.", func() {
		basic := status.MakeCondition("Basic Test")
		_, ok := basic.(*status.BasicCondition)
		c.Expect(ok, Equals, true)
		c.Expect(basic.Strength(), Equals, 5)
		c.Expect(basic.Kind(), Equals, status.Fire)
		var b status.Base
		b = basic.ModifyBase(b, status.Unspecified)
		c.Expect(b.Attack, Equals, 3)
	})

	c.Specify("Conditions can be gobbed without loss of type.", func() {
		buf := bytes.NewBuffer(nil)
		enc := gob.NewEncoder(buf)

		var cs []status.Condition
		cs = append(cs, status.MakeCondition("Basic Test"))

		err := enc.Encode(cs)
		c.Assume(err, Equals, nil)

		dec := gob.NewDecoder(buf)
		var cs2 []status.Condition
		err = dec.Decode(&cs2)
		c.Assume(err, Equals, nil)

		_, ok := cs2[0].(*status.BasicCondition)
		c.Expect(ok, Equals, true)
	})

	c.Specify("Conditions stack properly", func() {
		var s status.Inst
		fd := status.MakeCondition("Fire Debuff Attack")
		pd := status.MakeCondition("Poison Debuff Attack")
		pd2 := status.MakeCondition("Poison Debuff Attack 2")
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, 0)
		s.ApplyCondition(pd)
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, -1)
		s.ApplyCondition(fd)
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, -2)
		s.ApplyCondition(fd)
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, -2)
		s.ApplyCondition(pd)
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, -2)
		s.ApplyCondition(pd2)
		c.Expect(s.AttackBonusWith(status.Unspecified), Equals, -3)
	})

	c.Specify("Resistances work", func() {
		var s status.Inst
		fr1 := status.MakeCondition("Fire Resistance")
		fr2 := status.MakeCondition("Greater Fire Resistance")
		c.Expect(s.CorpusVs("Fire"), Equals, s.CorpusVs("Unspecified"))
		s.ApplyCondition(fr1)
		c.Expect(s.CorpusVs("Fire"), Equals, s.CorpusVs("Unspecified")+1)
		c.Expect(s.CorpusVs("Panic"), Equals, s.CorpusVs("Unspecified"))
		c.Expect(s.CorpusVs("Brutal"), Equals, s.CorpusVs("Unspecified"))
		s.ApplyCondition(fr2)
		c.Expect(s.CorpusVs("Fire"), Equals, s.CorpusVs("Unspecified")+3)
		c.Expect(s.CorpusVs("Panic"), Equals, s.CorpusVs("Unspecified"))
		c.Expect(s.CorpusVs("Brutal"), Equals, s.CorpusVs("Unspecified"))
	})

	c.Specify("Basic conditions last the appropriate amount of time", func() {
		var s status.Inst
		s.UnmarshalJSON([]byte(`
      {
        "Base": {
          "Hp_max": 100,
          "Ap_max": 10
        },
        "Dynamic": {
          "Hp": 100
        }
      }`))
		pd := status.MakeCondition("Poison Debuff Attack")
		pd2 := status.MakeCondition("Poison Debuff Attack 2")
		pd.Strength()
		pd2.Strength()
		c.Expect(s.HpCur(), Equals, 100)
		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		c.Expect(s.HpCur(), Equals, 100)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 99)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 98)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 97)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 97)

		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 96)
		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 95)
		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 94)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 93)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 92)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 92)

		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.ApplyCondition(status.MakeCondition("Poison Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 90)
		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 88)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 86)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 85)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 85)

		s.ApplyCondition(status.MakeCondition("Fire Debuff Attack"))
		s.ApplyCondition(status.MakeCondition("Poison Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 83)
		s.ApplyCondition(status.MakeCondition("Poison Debuff Attack 2"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 80)
		s.ApplyCondition(status.MakeCondition("Poison Debuff Attack"))
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 77)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 75)
		s.OnRound()
		c.Expect(s.HpCur(), Equals, 75)
	})
}
Exemplo n.º 19
0
func BlockWriteTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test that the coder writes complete values all at once.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable1{})
		tr.Register(encodable2{})
		tr.Register(encodable3([]byte{}))
		tr.Register(encodable4{})
		tr.Register(encodable5{})
		tr.Register(encodable6{})
		tr.Register(BarerImpl1{})
		tr.Complete()

		var iw = invasiveWriter{
			reg: &tr,
			buf: bytes.NewBuffer(nil),
		}

		e1 := encodable1{1, 2}
		err := tr.Encode(e1, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e2 := encodable2{14}
		err = tr.Encode(e2, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e3 := encodable3("fudgeball")
		err = tr.Encode(e3, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e4 := encodable4{
			A: e4sub1{
				B: 10,
				C: 20,
				D: e4sub2{
					E: "foo",
					F: "bar",
				},
			},
			G: 12345,
		}
		err = tr.Encode(e4, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e5 := encodable5{
			A: []byte("Monkeyball"),
		}
		err = tr.Encode(e5, &iw)
		c.Assume(err, gospec.Equals, error(nil))

		e6 := encodable6{
			Interface: BarerImpl1{10, "foo"},
		}
		err = tr.Encode(e6, &iw)
		c.Assume(err, gospec.Equals, error(nil))
	})
}
Exemplo n.º 20
0
func InterfaceTypeRegistrySpec(c gospec.Context) {
	c.Specify("Test that types containing interfaces are properly encoded.", func() {
		var tr core.TypeRegistry
		tr.Register(encodable6{})
		tr.Register(BarerImpl1{})
		tr.Register(BarerImpl2{})
		tr.Complete()

		v1 := encodable6{
			Interface: BarerImpl1{10, "foo"},
		}
		v2 := encodable6{
			Interface: BarerImpl2{3, 4},
		}

		b := bytes.NewBuffer(nil)
		err := tr.Encode(v1, b)
		c.Assume(err, gospec.Equals, error(nil))
		err = tr.Encode(v2, b)
		c.Assume(err, gospec.Equals, error(nil))

		d1, err := tr.Decode(b)
		c.Assume(err, gospec.Equals, error(nil))
		d2, err := tr.Decode(b)
		c.Assume(err, gospec.Equals, error(nil))

		c1, ok := d1.(encodable6)
		c.Assume(ok, gospec.Equals, true)
		c.Expect(c1.Interface.Bar(), gospec.Equals, "10:foo")
		c2, ok := d2.(encodable6)
		c.Assume(ok, gospec.Equals, true)
		c.Expect(c2.Interface.Bar(), gospec.Equals, "3:4")
	})
}
Exemplo n.º 21
0
func DecodersSpec(c gospec.Context) {

	msg := getTestMessage()

	c.Specify("A JsonDecoder", func() {
		var fmtString = `{"type":"%s","timestamp":%s,"logger":"%s","severity":%d,"payload":"%s","fields":%s,"env_version":"%s","metlog_pid":%d,"metlog_hostname":"%s"}`
		timestampJson, err := json.Marshal(msg.Timestamp)
		fieldsJson, err := json.Marshal(msg.Fields)
		c.Assume(err, gs.IsNil)
		jsonString := fmt.Sprintf(fmtString, msg.Type,
			timestampJson, msg.Logger, msg.Severity, msg.Payload,
			fieldsJson, msg.Env_version, msg.Pid, msg.Hostname)

		msgBytes := []byte(jsonString)
		pipelinePack := &PipelinePack{msgBytes}
		jsonDecoder := &JsonDecoder{}

		c.Specify("can decode a JSON message", func() {
			decodedMsg := jsonDecoder.Decode(pipelinePack)
			c.Expect(decodedMsg, gs.Equals, msg)
		})

		c.Specify("returns `fields` as a map", func() {
			decodedMsg := jsonDecoder.Decode(pipelinePack)
			c.Expect(decodedMsg.Fields["foo"], gs.Equals, "bar")
		})

		c.Specify("returns nil for bogus JSON", func() {
			badJson := fmt.Sprint("{{", jsonString)
			msgBytes = []byte(badJson)
			pipelinePack = &PipelinePack{msgBytes}
			decodedMsg := jsonDecoder.Decode(pipelinePack)
			c.Expect(decodedMsg, gs.IsNil)
		})
	})

	c.Specify("A GobDecoder", func() {
		buffer := &bytes.Buffer{}
		encoder := gob.NewEncoder(buffer)
		err := encoder.Encode(msg)
		c.Assume(err, gs.IsNil)
		decoder := &GobDecoder{}
		msgBytes := buffer.Bytes()
		c.Assume(err, gs.IsNil)
		pipelinePack := &PipelinePack{msgBytes}

		c.Specify("can decode a gob message", func() {
			decodedMsg := decoder.Decode(pipelinePack)
			c.Expect(decodedMsg, gs.Equals, msg)
		})

		c.Specify("returns nil for bogus gob data", func() {
			longerBytes := make([]byte, len(msgBytes)+1)
			copy([]byte{'x'}, longerBytes[0:1])
			copy(msgBytes[:], longerBytes[1:])
			pipelinePack := &PipelinePack{longerBytes}
			decodedMsg := decoder.Decode(pipelinePack)
			c.Expect(decodedMsg, gs.IsNil)
		})
	})
}
Exemplo n.º 22
0
func YedSpec(c gospec.Context) {
	c.Specify("Load a simple .xgml file.", func() {
		g, err := yed.ParseFromFile("state.xgml")
		c.Assume(err, Equals, nil)

		c.Specify("Nodes have the proper Ids", func() {
			for i := 0; i < g.Graph.NumNodes(); i++ {
				c.Expect(g.Graph.Node(i).Id(), Equals, i)
			}
		})

		red_count := 0
		green_count := 0
		for i := 0; i < g.Graph.NumEdges(); i++ {
			edge := g.Graph.Edge(i)
			r, g, _, _ := edge.RGBA()
			if r == 255 {
				red_count++
			}
			if g == 255 {
				green_count++
			}
			if r == 255 && g == 255 {
				panic("Shoudn't have found an edge that has both R and G set to 255.")
			}
		}
		c.Expect(red_count, Equals, 2)
		c.Expect(green_count, Equals, 2)

		// Check that certain nodes and edges are hooked up properly
		found := false
		for i := 0; i < g.Graph.NumNodes(); i++ {
			node := g.Graph.Node(i)
			if node.Label() == "option 1" {
				found = true
				c.Expect(node.NumInputs(), Equals, 1)
				c.Assume(node.NumOutputs(), Equals, 2)
				for i := 0; i < node.NumOutputs(); i++ {
					edge := node.Output(i)
					if edge.Line(0) == "Edge Foo" {
						c.Expect(edge.Tag("tag1"), Equals, "monkey")
						c.Expect(edge.Tag("tag2"), Equals, "chimp")
						c.Expect(edge.Dst().Label(), Equals, "option 3")
					} else if edge.Line(0) == "Edge Bar" {
						c.Expect(edge.Tag("tag3"), Equals, "walrus")
						c.Expect(edge.Dst().Label(), Equals, "option 4")
					} else {
						panic("Expected 'Edge Foo' or 'Edge Bar', found '" + edge.Line(0) + "'")
					}
				}
			}
		}
		c.Expect(found, Equals, true)

		// Check that groups are set up properly
		for i := 0; i < g.Graph.NumNodes(); i++ {
			group := g.Graph.Node(i).Group()
			if group == nil {
				continue
			}
			if group.NumLines() == 0 || group.Line(0) != "nubcake" {
				continue
			}
			c.Assume(group.NumChildren(), Equals, 2)
			var opt3, opt4 int
			for i := 0; i < group.NumChildren(); i++ {
				switch group.Child(i).Label() {
				case "option 3":
					opt3++
					opt := group.Child(i)
					c.Expect(opt.NumGroupInputs(), Equals, 3)
					c.Expect(opt.GroupInput(1).Dst().Line(0), Equals, "nubcake")
					c.Expect(opt.GroupInput(2).Dst().Line(0), Equals, "bigger")
					c.Expect(opt.NumGroupOutputs(), Equals, 2)

				case "option 4":
					opt4++
					opt := group.Child(i)
					c.Expect(opt.NumGroupInputs(), Equals, 3)
					c.Expect(opt.GroupInput(1).Dst().Line(0), Equals, "nubcake")
					c.Expect(opt.GroupInput(2).Dst().Line(0), Equals, "bigger")
					c.Expect(opt.NumGroupOutputs(), Equals, 2)

				default:
					panic("Expected 'option3' or 'option4', found '" + group.Child(i).Label() + "'")
				}
			}
			c.Expect(opt3, Equals, 1)
			c.Expect(opt4, Equals, 1)

			c.Expect(group.NumInputs(), Equals, 1)
			c.Assume(group.NumOutputs(), Equals, 2)
			c.Expect(group.Output(0).Dst().Label(), Equals, "choice")

			parent := group.Group()
			c.Assume(parent, Not(Equals), nil)
			c.Expect(parent.Line(0), Equals, "bigger")
			c.Expect(parent.Tag("tag4"), Equals, "tiger")
			c.Expect(parent.NumChildren(), Equals, 3)
		}

		var opt4 *yed.Node
		for i := 0; i < g.Graph.NumNodes(); i++ {
			if g.Graph.Node(i).Label() == "option 4" {
				opt4 = g.Graph.Node(i)
				break
			}
		}
		c.Assume(opt4, Not(Equals), nil)

	})
}
Exemplo n.º 23
0
func SimpleServerSpec(c gospec.Context) {
	c.Specify("Hook up all of the basic parts and make them talk.", func() {
		host, err := core.MakeHost("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		client0, err := core.MakeClient("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		client1, err := core.MakeClient("127.0.0.1", 1234)
		c.Assume(err, gospec.Equals, error(nil))
		fmt.Printf("%v %v %v\n", host, client0)
		registerGameForAll(&Game{}, host, client0, client1)
		registerRequestForAll(Advance{}, host, client0, client1)
		registerUpdateForAll(Advance{}, host, client0, client1)
		registerRequestForAll(Join{}, host, client0, client1)
		registerUpdateForAll(Join{}, host, client0, client1)
		game := &Game{}
		host.Start(game)
		client0.Start()
		client1.Start()

		client0.GameMutex.Lock()
		game0 := client0.Game.(*Game)
		game0.myName = "foo"
		client0.GameMutex.Unlock()
		client0.MakeRequest(Join{Name: "foo"})

		client1.GameMutex.Lock()
		game1 := client1.Game.(*Game)
		game1.myName = "bar"
		client1.GameMutex.Unlock()
		client1.MakeRequest(Join{Name: "bar"})

		time.Sleep(time.Millisecond * 100)

		client0.GameMutex.RLock()
		fmt.Printf("game0: %v\n", client0.Game)
		client0.GameMutex.RUnlock()
		client1.GameMutex.RLock()
		fmt.Printf("game1: %v\n", client1.Game)
		client1.GameMutex.RUnlock()

		incs := 10
		var wg sync.WaitGroup
		for _, client := range []*core.Client{client0, client1} {
			wg.Add(1)
			go func(client *core.Client) {
				for i := 0; i < incs; i++ {
					time.Sleep(time.Millisecond)
					client.GameMutex.RLock()
					g := client.Game.(*Game)
					if g.me == nil {
						client.GameMutex.RUnlock()
						continue
					}
					adv := Advance{g.myIndex, g.me.Pos + 1}
					fmt.Printf("Request(%d, %d): %v -> %d\n", i, g.myIndex, *g.me, adv.NewPos)
					client.GameMutex.RUnlock()
					client.MakeRequest(adv)
					client.MakeMinorUpdate(adv)
				}
				wg.Done()
			}(client)
		}
		wg.Wait()
		time.Sleep(time.Millisecond * 100)

		client0.GameMutex.RLock()
		fmt.Printf("game0: %v\n", client0.Game)
		for _, player := range client0.Game.(*Game).Players {
			fmt.Printf("Player: %v\n", *player)
		}
		client0.GameMutex.RUnlock()
		client1.GameMutex.RLock()
		fmt.Printf("game1: %v\n", client1.Game)
		for _, player := range client1.Game.(*Game).Players {
			fmt.Printf("Player: %v\n", *player)
		}
		client1.GameMutex.RUnlock()

		// // for _, client := range []*core.Client{client0, client1} {

		// // }

		// client1.GameMutex.RLock()
		// fmt.Printf("game: %v\n", client1.Game)
		// client1.GameMutex.RUnlock()

		// adv := Advance{0, 2}
		// client0.MakeRequest(adv)
		// // client0.MakeMinorUpdate(adv)
		// time.Sleep(time.Millisecond * 100)

		// client1.GameMutex.RLock()
		// fmt.Printf("game: %v\n", client1.Game)
		// client1.GameMutex.RUnlock()

		// c.Expect(true, gospec.Equals, false)
	})
}