Skip to content

Commit

Permalink
restore FindJSON, still include FindAScript, add tests. To Do : Compl…
Browse files Browse the repository at this point in the history
…ete the guide to find single and multiple scripts
  • Loading branch information
SurgicalSteel committed Dec 4, 2018
1 parent be56de7 commit 73804c5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
17 changes: 14 additions & 3 deletions script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
119 changes: 117 additions & 2 deletions script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 73804c5

Please sign in to comment.