Example #1
0
func TestMobSpawn(t *testing.T) {
	tests := []testCase{
		{
			"pig",
			func(writer *bytes.Buffer) os.Error {
				m := NewPig().(*Pig)
				m.PointObject.Init(&types.AbsXyz{11, 70, -172}, &types.AbsVelocity{0, 0, 0})
				m.Mob.EntityId = 0x1234
				m.SetBurning(true)
				m.SetBurning(false)
				m.SetLook(types.LookDegrees{10, 20})
				return m.SendSpawn(writer)
			},
			te.InOrder(
				// packetIdEntitySpawn
				te.LiteralString("\x18"+ // Packet ID
					"\x00\x00\x12\x34"+ // EntityId
					"Z"+ // EntityMobType
					"\x00\x00\x01`\x00\x00\b\xc0\xff\xff\xea\x80"+ // X, Y, Z
					"\a\x0e", // Yaw, Pitch
				),
				te.AnyOrder(
					te.LiteralString("\x00\x00"), // burning=false
					te.LiteralString("\x10\x00"), // 16=0 (?)
				),
				te.LiteralString("\x7f"), // 127 = end of metadata
				// packetIdEntityVelocity
				te.LiteralString("\x1c\x00\x00\x12\x34\x00\x00\x00\x00\x00\x00"),
			),
		},
		{
			"creeper",
			func(writer *bytes.Buffer) os.Error {
				// Bogus position, changing below.
				m := NewCreeper().(*Creeper)
				m.PointObject.Init(&types.AbsXyz{11, 70, -172}, &types.AbsVelocity{})
				m.Mob.EntityId = 0x5678
				m.CreeperSetBlueAura()
				m.SetBurning(true)
				m.SetLook(types.LookDegrees{0, 199})
				return m.SendSpawn(writer)
			},
			te.InOrder(
				// packetIdEntitySpawn
				te.LiteralString("\x18"+ // Packet ID
					"\x00\x00\x56\x78"+ // EntityId
					"2"+ // EntityMobType
					"\x00\x00\x01\x60\x00\x00\x08\xc0\xff\xff\xea\x80"+ // X, Y, Z
					"\x00\x8d", // Yaw, Pitch
				),
				te.AnyOrder(
					te.LiteralString("\x00\x01"), // burning=true
					te.LiteralString("\x10\xff"), // 16=255 (?)
					te.LiteralString("\x11\x01"), // blue aura=true
				),
				te.LiteralString("\x7f"), // 127 = end of metadata
				// packetIdEntityVelocity
				te.LiteralString("\x1c\x00\x00\x56\x78\x00\x00\x00\x00\x00\x00"),
			),
		},
	}
	for _, x := range tests {
		buf := new(bytes.Buffer)
		want, err := x.want, x.result(buf)
		if err != nil {
			t.Errorf("Error when writing in test %s: %v", x.name, err)
			continue
		}
		result := buf.Bytes()
		if err = te.Matches(want, result); err != nil {
			t.Errorf("Resulting raw data mismatch for %s spawn: %v\nGot bytes: %x", x.name, err, result)
		}
	}
}
Example #2
0
func TestSerialization(t *testing.T) {
	tests := []Test{
		{te.LiteralString("\x01"), &Byte{1}},
		{te.LiteralString("\x10\x20"), &Short{0x1020}},
		{te.LiteralString("\x10\x20\x30\x40"), &Int{0x10203040}},
		{te.LiteralString("\x10\x20\x30\x40\x50\x60\x70\x80"), &Long{0x1020304050607080}},
		{te.LiteralString("\x3f\x80\x00\x00"), &Float{1.0}},
		{te.LiteralString("\x3f\xf0\x00\x00\x00\x00\x00\x00"), &Double{1.0}},
		{te.LiteralString("\x00\x00\x00\x04\x00\x01\x02\x03"), &ByteArray{[]byte{0, 1, 2, 3}}},
		{te.LiteralString("\x00\x03foo"), &String{"foo"}},
		{te.LiteralString("\x01\x00\x00\x00\x02\x01\x02"), &List{TagByte, []ITag{&Byte{1}, &Byte{2}}}},
		{te.LiteralString("\x03\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02"), &List{TagInt, []ITag{&Int{1}, &Int{2}}}},

		{
			// Single item Compound.
			te.InOrder(
				te.LiteralString("\x01\x00\x03foo\x01"), // NamedTag "foo" Byte{1}
				te.LiteralString("\x00"),                // End
			),
			Compound{
				"foo": &Byte{1},
			},
		},
		{
			// Multiple item Compound.
			te.InOrder(
				te.AnyOrder(
					// NamedTag "Byte" Byte{1}
					te.LiteralString("\x01\x00\x04Byte\x01"),
					// NamedTag "Short" Short{2}
					te.LiteralString("\x02\x00\x05Short\x00\x02"),
					// NamedTag "Int" Int{3}
					te.LiteralString("\x03\x00\x03Int\x00\x00\x00\x03"),
					// NamedTag "Long" Long{4}
					te.LiteralString("\x04\x00\x04Long\x00\x00\x00\x00\x00\x00\x00\x04"),
					// NamedTag "Float" Float{5}
					te.LiteralString("\x05\x00\x05Float\x40\xa0\x00\x00"),
					// NamedTag "Double" Double{6}
					te.LiteralString("\x06\x00\x06Double\x40\x18\x00\x00\x00\x00\x00\x00"),
					// NamedTag "String" String{"foo"}
					te.LiteralString("\x08\x00\x06String\x00\x03foo"),
					// NamedTag "List" List{Byte{1}, Byte{2}}
					te.LiteralString("\x09\x00\x04List\x01\x00\x00\x00\x02\x01\x02"),
				),
				te.LiteralString("\x00"), // End
			),
			Compound{
				"Byte":   &Byte{1},
				"Short":  &Short{2},
				"Int":    &Int{3},
				"Long":   &Long{4},
				"Float":  &Float{5},
				"Double": &Double{6},
				"String": &String{"foo"},
				"List":   &List{TagByte, []ITag{&Byte{1}, &Byte{2}}},
			},
		},
	}

	for i := range tests {
		tests[i].testRead(t)
		tests[i].testWrite(t)
	}
}