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
11 changes: 9 additions & 2 deletions mod/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ func (r *Reverser) Write(raw map[string]interface{}) error {
if err != nil {
return fmt.Errorf("mismatch expectations in key %s : %v", objKey, err)
}

if objKey == "SnapPoints" {
smoothed, err := objects.SmoothSnapPoints(arr)
if err != nil {
return fmt.Errorf("SmoothSnapPoints(): %v", err)
}
arr = smoothed
}
// decide if creating a separate file is worth it
if len(fmt.Sprint(arr)) < 200 {
raw[objKey] = arr
continue
}

Expand Down Expand Up @@ -129,7 +136,7 @@ func convertToObjArray(v interface{}) ([]map[string]interface{}, error) {

rawArr, ok := v.([]interface{})
if !ok {
return nil, fmt.Errorf("%v is not an array", v)
return nil, fmt.Errorf("%v is not an array, is %T", v, v)
}

for _, rv := range rawArr {
Expand Down
161 changes: 161 additions & 0 deletions mod/reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package mod

import (
"ModCreator/tests"
"ModCreator/types"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestReverse(t *testing.T) {
for _, tc := range []struct {
name string
input map[string]interface{}
wantRootConfig map[string]interface{}
wantModSettings map[string]types.J
wantObjs map[string]types.J
wantObjTexts map[string]string
}{
{
name: "SnapPoints",
input: map[string]interface{}{
"SnapPoints": []interface{}{
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
},
},
wantRootConfig: map[string]interface{}{
"SnapPoints": []interface{}{
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
},
},
wantModSettings: map[string]types.J{},
},
{
name: "SnapPointsOwnFile",
input: map[string]interface{}{
"SnapPoints": []interface{}{
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
map[string]interface{}{
"Position": map[string]interface{}{
"x": float64(12.123456),
"y": float64(22.123456),
"z": float64(32.123456),
},
},
},
},
wantRootConfig: map[string]interface{}{
"SnapPoints_path": "SnapPoints.json",
},
wantModSettings: map[string]types.J{
"SnapPoints.json": types.J{
"testarray": []map[string]interface{}{ // implementation detail of fake files
map[string]interface{}{
"Position": types.J{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
map[string]interface{}{
"Position": types.J{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
map[string]interface{}{
"Position": types.J{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
map[string]interface{}{
"Position": types.J{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
map[string]interface{}{
"Position": types.J{
"x": float64(12.123),
"y": float64(22.123),
"z": float64(32.123),
},
},
},
},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
finalOutput := tests.NewFF()
modsettings := tests.NewFF()
objsAndLua := tests.NewFF()
r := Reverser{
ModSettingsWriter: modsettings,
LuaWriter: objsAndLua,
ObjWriter: objsAndLua,
ObjDirCreeator: objsAndLua,
RootWrite: finalOutput,
}
err := r.Write(tc.input)
if err != nil {
t.Fatalf("Error reversing : %v", err)
}
got, err := finalOutput.ReadObj("config.json")
if err != nil {
t.Fatalf("Error reading final config.json : %v", err)
}
if diff := cmp.Diff(tc.wantRootConfig, got); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
if diff := cmp.Diff(tc.wantModSettings, modsettings.Data); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
})
}

}
95 changes: 86 additions & 9 deletions objects/functional_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package objects

import (
"ModCreator/tests"
"ModCreator/types"
"testing"

Expand Down Expand Up @@ -77,9 +78,9 @@ func TestFileToJson(t *testing.T) {
},
},
} {
ff := &fakeFiles{
fs: tc.txtfiles,
data: tc.jsonfiles,
ff := &tests.FakeFiles{
Fs: tc.txtfiles,
Data: tc.jsonfiles,
}
o := objConfig{}
err := o.parseFromFile("foo/cool.123456.json", ff)
Expand Down Expand Up @@ -116,6 +117,85 @@ func TestJsonToFiles(t *testing.T) {
},
},
wantTxtfiles: map[string]string{},
}, {
relpath: "objrounding",
input: types.J{
"GUID": "123",
"foobar": "baz",
"AltLookAngle": types.J{
"x": 0.123456789,
"y": -0.123456789,
"z": float64(370),
},
"AttachedSnapPoints": []map[string]interface{}{
{
"Position": types.J{
"x": -1.82239926,
"y": 0.100342259,
"z": 0.6163123,
},
"Rotation": types.J{
"x": 3.36023078e-7,
"y": 0.008230378,
"z": -2.29263165e-7,
},
},
{
"Rotation": types.J{
"x": 3.36023078e-7,
"y": 0.008230378,
"z": -2.29263165e-7,
},
},
{
"Position": types.J{
"x": -1.82239926,
"y": 0.100342259,
"z": 0.6163123,
},
},
},
},
wantJsonfiles: map[string]types.J{
"objrounding/123.json": types.J{
"GUID": "123",
"foobar": "baz",
"AltLookAngle": types.J{
"x": float64(0),
"y": float64(0),
"z": float64(10),
},
"AttachedSnapPoints": []map[string]interface{}{
{
"Position": types.J{
"x": float64(-1.822),
"y": float64(0.100),
"z": float64(0.616),
},
"Rotation": types.J{
"x": float64(0),
"y": float64(0),
"z": float64(0),
},
},
{
"Rotation": types.J{
"x": float64(0),
"y": float64(0),
"z": float64(0),
},
},
{
"Position": types.J{
"x": -1.822,
"y": 0.100,
"z": 0.616,
},
},
},
},
},
wantTxtfiles: map[string]string{},
}, {
relpath: "foo2",
input: types.J{
Expand Down Expand Up @@ -168,10 +248,7 @@ func TestJsonToFiles(t *testing.T) {
wantTxtfiles: map[string]string{},
},
} {
ff := &fakeFiles{
fs: map[string]string{},
data: map[string]types.J{},
}
ff := tests.NewFF()
o := objConfig{}
err := o.parseFromJSON(tc.input)
if err != nil {
Expand All @@ -181,10 +258,10 @@ func TestJsonToFiles(t *testing.T) {
if err != nil {
t.Fatalf("printToFile(%s): %v", o.getAGoodFileName(), err)
}
if diff := cmp.Diff(tc.wantJsonfiles, ff.data); diff != "" {
if diff := cmp.Diff(tc.wantJsonfiles, ff.Data); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
if diff := cmp.Diff(tc.wantTxtfiles, ff.fs); diff != "" {
if diff := cmp.Diff(tc.wantTxtfiles, ff.Fs); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
}
Expand Down
Loading