From 73804c5b63013803d02d75ab6945b876ce14ea64 Mon Sep 17 00:00:00 2001 From: Yuwono Bangun Nagoro Date: Tue, 4 Dec 2018 18:42:48 +0700 Subject: [PATCH] restore FindJSON, still include FindAScript, add tests. To Do : Complete the guide to find single and multiple scripts --- script.go | 17 +++++-- script_test.go | 119 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 5 deletions(-) diff --git a/script.go b/script.go index f672faf..789710e 100644 --- a/script.go +++ b/script.go @@ -201,12 +201,23 @@ func (o *OurScript) FindBundledScripts(mappedScriptItem map[FileType][]string) m } // FindAScript get a script body of specified single scriptItem -func (o *OurScript) FindAScript(scriptItem ScriptItem) string { - resultScript := "" +func (o *OurScript) FindAScript(scriptItem ScriptItem) ScriptFile { + var resultScript ScriptFile if mapOfKindValue, ok := o.Map[scriptItem.Kind]; ok { if rawScriptFile, ok2 := mapOfKindValue[scriptItem.Name]; ok2 { - resultScript = rawScriptFile.Body + resultScript = rawScriptFile } } return resultScript } + +// FindJSON get a JSON string based on specified single scriptItem +func (o *OurScript) FindJSON(scriptItem ScriptItem) string { + resultJSON := "" + if mapOfKindValue, ok := o.Map[scriptItem.Kind]; ok { + if rawScriptFile, ok2 := mapOfKindValue[scriptItem.Name]; ok2 { + resultJSON = rawScriptFile.Body + } + } + return resultJSON +} diff --git a/script_test.go b/script_test.go index 697fcd9..dbd7409 100644 --- a/script_test.go +++ b/script_test.go @@ -102,7 +102,7 @@ func TestInitializeErrorPath(t *testing.T) { t.Error("Should expect error in this case") } } -func TestFindScript(t *testing.T) { +func TestFindMultipleScripts(t *testing.T) { ourScript := OurScript{ RawSlice: []ScriptFile{ { @@ -309,6 +309,121 @@ func TestFindBundledScripts(t *testing.T) { } func TestFindAScript(t *testing.T) { + ourScript := OurScript{ + RawSlice: []ScriptFile{ + { + Name: "stylereference", + FileName: "stylereference.css", + Kind: CSS, + Body: `#container{background: #AAAAAA;}`, + }, + { + Name: "awkarinstylereference", + FileName: "awkarinstylereference.css", + Kind: CSS, + Body: `#awkarincontainer{background: #000000;}`, + }, + { + Name: "weirdstylereference", + FileName: "weirdstylereference.css", + Kind: CSS, + Body: `#weirdcontainer{background: #FFFFFF;}`, + }, + { + Name: "scriptreference", + FileName: "scriptreference.js", + Kind: JS, + Body: "alert('Awkarin is back!');", + }, + { + Name: "weirdscriptreference", + FileName: "weirdscriptreference.js", + Kind: JS, + Body: "alert('Awkarin is coming....');", + }, + { + Name: "jsonreference", + FileName: "jsonreference.json", + Kind: JSON, + Body: `[{"name":"James Bond","age":44,"sex":"male"},{"name":"Awkarin","age":20,"sex":"female"}]`, + }, + }, + Map: map[FileType]map[string]ScriptFile{ + CSS: { + "stylereference": ScriptFile{ + Name: "stylereference", + FileName: "stylereference.css", + Kind: CSS, + Body: `#container{background: #AAAAAA;}`, + }, + "awkarinstylereference": ScriptFile{ + Name: "awkarinstylereference", + FileName: "awkarinstylereference.css", + Kind: CSS, + Body: `#awkarincontainer{background: #000000;}`, + }, + "weirdstylereference": ScriptFile{ + Name: "weirdstylereference", + FileName: "weirdstylereference.css", + Kind: CSS, + Body: `#weirdcontainer{background: #FFFFFF;}`, + }, + }, + JS: { + "scriptreference": ScriptFile{ + Name: "scriptreference", + FileName: "scriptreference.js", + Kind: JS, + Body: "alert('Awkarin is back!');", + }, + "weirdscriptreference": ScriptFile{ + Name: "weirdscriptreference", + FileName: "weirdscriptreference.js", + Kind: JS, + Body: "alert('Awkarin is coming....');", + }, + }, + JSON: { + "jsonreference": ScriptFile{ + Name: "jsonreference", + FileName: "jsonreference.json", + Kind: JSON, + Body: `[{"name":"James Bond","age":44,"sex":"male"},{"name":"Awkarin","age":20,"sex":"female"}]`, + }, + }, + }, + } + type testscenario struct { + scriptItem ScriptItem + resultScript ScriptFile + } + testcases := make(map[string]testscenario) + testcases["success"] = testscenario{ + scriptItem: ScriptItem{ + Name: "stylereference", + Kind: CSS, + }, + resultScript: ScriptFile{ + Name: "stylereference", + FileName: "stylereference.css", + Kind: CSS, + Body: `#container{background: #AAAAAA;}`, + }, + } + testcases["return-empty"] = testscenario{ + scriptItem: ScriptItem{ + Name: "unknownreference", + Kind: JS, + }, + resultScript: ScriptFile{}, + } + for _, vtc := range testcases { + actual := ourScript.FindAScript(vtc.scriptItem) + assert.Equal(t, vtc.resultScript, actual) + } +} + +func TestFindJSON(t *testing.T) { ourScript := OurScript{ RawSlice: []ScriptFile{ { @@ -413,7 +528,7 @@ func TestFindAScript(t *testing.T) { resultbody: ``, } for _, vtc := range testcases { - actual := ourScript.FindAScript(vtc.scriptItem) + actual := ourScript.FindJSON(vtc.scriptItem) assert.Equal(t, vtc.resultbody, actual) } }