aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/gamedata/item_stats_test.go (1418B)


      1 package gamedata_test
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"slices"
      6 	"strings"
      7 	"testing"
      8 
      9 	"go.yaml.in/yaml/v3"
     10 )
     11 
     12 func TestItemYAMLNamesPreserveStatOrderAndValues(t *testing.T) {
     13 	original := gamedata.Item{Stats: [][2]uint32{{74, 200}, {0, 2214593057}, {74, 300}, {99999, 4294967295}}}
     14 
     15 	encoded, err := yaml.Marshal(original)
     16 	if err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	var decoded gamedata.Item
     20 	err = yaml.Unmarshal(encoded, &decoded)
     21 
     22 	if err != nil || !slices.Equal(original.Stats, decoded.Stats) {
     23 		t.Fatalf("named YAML changed item data: %v, %+v", err, decoded)
     24 	}
     25 	if !strings.Contains(string(encoded), "- [price, 200]\n") ||
     26 		!strings.Contains(string(encoded), "- [flags, 2214593057]\n") ||
     27 		!strings.Contains(string(encoded), "- [price, 300]\n") ||
     28 		!strings.Contains(string(encoded), "- [99999, 4294967295]\n") {
     29 		t.Fatalf("missing named pairs or unknown numeric ID: %s", encoded)
     30 	}
     31 }
     32 
     33 func TestItemYAMLRejectsInvalidNamedStats(t *testing.T) {
     34 	for _, input := range []string{
     35 		"stats: [[prcie, 200]]",
     36 		"stats: [[price]]",
     37 		"stats: [[price, 200, 300]]",
     38 		"stats: [[price, -1]]",
     39 		"stats: [[price, 4294967296]]",
     40 		"stats: [[price, null]]",
     41 		"stats: [[price, 1.5]]",
     42 		"stats: [[null, 200]]",
     43 		"stats: {price: 200}",
     44 	} {
     45 		t.Run(input, func(t *testing.T) {
     46 			var item gamedata.Item
     47 
     48 			err := yaml.Unmarshal([]byte(input), &item)
     49 
     50 			if err == nil {
     51 				t.Fatal("invalid stat accepted")
     52 			}
     53 		})
     54 	}
     55 }