Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion objects/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
148 changes: 116 additions & 32 deletions objects/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"path"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -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
}{
{
Expand All @@ -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)
}
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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
Expand Down