From 5838d397d19d0159be47c3fed717923d90006e34 Mon Sep 17 00:00:00 2001 From: Argonui Date: Sat, 5 Nov 2022 01:05:22 -0500 Subject: [PATCH] Set ordering for subobjects And add tests which should have caught this before --- objects/objects.go | 3 +- objects/objects_test.go | 148 +++++++++++++++++++++++++++++++--------- 2 files changed, 118 insertions(+), 33 deletions(-) diff --git a/objects/objects.go b/objects/objects.go index 35510df..ef824d9 100644 --- a/objects/objects.go +++ b/objects/objects.go @@ -200,7 +200,8 @@ func (o *objConfig) printToFile(filepath string, l file.LuaWriter, j file.JSONWr } o.data["ContainedObjects_path"] = subdirName o.subObjDir = subdirName - for _, subo := range o.subObj { + for i, subo := range o.subObj { + subo.order = int64(i) err = subo.printToFile(path.Join(filepath, subdirName), l, j, dir) if err != nil { return fmt.Errorf("printing file %s: %v", path.Join(filepath, subdirName), err) diff --git a/objects/objects_test.go b/objects/objects_test.go index 20e819f..f930769 100644 --- a/objects/objects_test.go +++ b/objects/objects_test.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "path" "testing" "github.com/google/go-cmp/cmp" @@ -42,13 +41,14 @@ func (f *fakeFiles) EncodeToFile(script, file string) error { return nil } func (f *fakeFiles) CreateDir(a, b string) (string, error) { - return a + b, nil + // return the "chosen" directory name for next folder + return b, nil } -func TestObjPrintToFile(t *testing.T){ +func TestObjPrintToFile(t *testing.T) { for _, tc := range []struct { - o *objConfig - want j + o *objConfig + want j wantFilename string }{ { @@ -60,15 +60,16 @@ func TestObjPrintToFile(t *testing.T){ }, wantFilename: "123456.json", want: j{ - "GUID": "123456", + "GUID": "123456", + "tts_mod_order": int64(0), }, }, } { ff := &fakeFiles{ - fs: map[string]string{}, + fs: map[string]string{}, data: map[string]j{}, } - err := tc.o.printToFile("path/to/",ff,ff,ff ) + err := tc.o.printToFile("path/to/", ff, ff, ff) if err != nil { t.Errorf("printing %v, got %v", tc.o, err) } @@ -151,11 +152,15 @@ func TestObjPrintingToFile(t *testing.T) { type fileContent struct { file, content string } + type jsonContent struct { + file string + content j + } for _, tc := range []struct { - o *objConfig - folder string - want j - wantLSS fileContent + o *objConfig + folder string + wantObjs []jsonContent + wantLSS fileContent }{ { o: &objConfig{ @@ -165,11 +170,77 @@ func TestObjPrintingToFile(t *testing.T) { }, }, folder: "foo", - want: j{ - "GUID": "123456", - "tts_mod_order": int64(0), + wantObjs: []jsonContent{ + { + file: "foo/123456.json", + content: j{ + "GUID": "123456", + "tts_mod_order": int64(0), + }, + }, }, - }, { + }, + { + o: &objConfig{ + guid: "123777", + data: j{ + "GUID": "123777", + }, + subObj: []*objConfig{ + &objConfig{ + guid: "1237770", + data: j{ + "GUID": "1237770", + }, + }, + &objConfig{ + guid: "1237771", + data: j{ + "GUID": "1237771", + }, + }, + &objConfig{ + guid: "1237772", + data: j{ + "GUID": "1237772", + }, + }, + }, + }, + folder: "bar", + wantObjs: []jsonContent{ + { + file: "bar/123777.json", + content: j{ + "GUID": "123777", + "tts_mod_order": int64(0), + "ContainedObjects_path": "123777", + }, + }, + { + file: "bar/123777/1237770.json", + content: j{ + "GUID": "1237770", + "tts_mod_order": int64(0), + }, + }, + { + file: "bar/123777/1237771.json", + content: j{ + "GUID": "1237771", + "tts_mod_order": int64(1), + }, + }, + { + file: "bar/123777/1237772.json", + content: j{ + "GUID": "1237772", + "tts_mod_order": int64(2), + }, + }, + }, + }, + { o: &objConfig{ guid: "123456", data: j{ @@ -178,13 +249,19 @@ func TestObjPrintingToFile(t *testing.T) { }, }, folder: "foo", - want: j{ - "GUID": "123456", - "LuaScriptState": "fav color = green", - "tts_mod_order": int64(0), + wantObjs: []jsonContent{ + { + file: "foo/123456.json", + content: j{ + "GUID": "123456", + "LuaScriptState": "fav color = green", + "tts_mod_order": int64(0), + }, + }, }, // want no LSS file because it's short - }, { + }, + { o: &objConfig{ guid: "123456", data: j{ @@ -193,10 +270,15 @@ func TestObjPrintingToFile(t *testing.T) { }, }, folder: "foo", - want: j{ - "GUID": "123456", - "LuaScriptState_path": "foo/123456.luascriptstate", - "tts_mod_order": int64(0), + wantObjs: []jsonContent{ + { + file: "foo/123456.json", + content: j{ + "GUID": "123456", + "LuaScriptState_path": "foo/123456.luascriptstate", + "tts_mod_order": int64(0), + }, + }, }, wantLSS: fileContent{ file: "foo/123456.luascriptstate", @@ -212,14 +294,16 @@ func TestObjPrintingToFile(t *testing.T) { if err != nil { t.Errorf("printing %v, got %v", tc.o, err) } - got, ok := ff.data[path.Join(tc.folder, tc.o.getAGoodFileName()+".json")] - if !ok { - log.Printf("%v\n", ff.data) - t.Fatalf("data not found in fake files as expected") - } - if diff := cmp.Diff(tc.want, got); diff != "" { - t.Errorf("want != got:\n%v\n", diff) + for _, wantFJ := range tc.wantObjs { + got, ok := ff.data[wantFJ.file] + if !ok { + log.Printf("%v\n", ff.data) + t.Fatalf("Wanted file %s, not found", wantFJ.file) + } + if diff := cmp.Diff(wantFJ.content, got); diff != "" { + t.Errorf("want != got:\n%v\n", diff) + } } // compare lua script state