From f732eebbe5b8eb44880df69ae47692c4b808e775 Mon Sep 17 00:00:00 2001 From: Noppanit Charassinvichai Date: Wed, 6 Jul 2011 16:08:00 +0100 Subject: [PATCH] [ToyC] #11 Added test cases --- framework/starter.go | 35 +++++++++++++++++++++++++++-------- framework/starter_test.go | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/framework/starter.go b/framework/starter.go index 3faf99f..a7cfd24 100644 --- a/framework/starter.go +++ b/framework/starter.go @@ -1,6 +1,5 @@ package starter -import "fmt" import "web" import "reflect" import "mustache" @@ -50,20 +49,42 @@ func internalGet(context gon.WebContext, val string) { return } -func getViewAndControllerFromURL(value string) (string, string) { +func getViewAndControllerFromURL(url string) (string, string) { pointerOfMappings := reflect.ValueOf(mapping.URL) valueOfMappings := reflect.Indirect(pointerOfMappings) - fmt.Println("Test",valueOfMappings.String()) - // numField := valueOfMappings.NumField() - // fmt.Println(numField) - return "", "" + numField := valueOfMappings.NumField() + var realMappings map[string]string + for i := 0; i < numField; i++ { + maps := valueOfMappings.Field(i).Interface() + realMaps := maps.(map[string]string) + if realMaps["url"] == url { + realMappings = realMaps + } else { + realMappings = nil + } + } + controllerName := "" + viewName := "" + + if realMappings != nil { + controllerName = realMappings["controller"] + viewName = realMappings["view"] + } + return controllerName, viewName } func splitControllerAndAction(value string) (string,string) { + controllerAndActionName := strings.Split(value,"/",2) controllerName := "" actionName := "" + controllerName, actionName = getViewAndControllerFromURL("/"+value) + if controllerName != "" && actionName != ""{ + return controllerName, actionName + } + + if len(controllerAndActionName) == 2 { controllerName,actionName = controllerAndActionName[0],controllerAndActionName[1] if actionName == "" { @@ -74,8 +95,6 @@ func splitControllerAndAction(value string) (string,string) { actionName = "index" } - - return controllerName, actionName } diff --git a/framework/starter_test.go b/framework/starter_test.go index 75c0ae3..2a46170 100644 --- a/framework/starter_test.go +++ b/framework/starter_test.go @@ -51,7 +51,7 @@ func TestFindMethodAndInvoke(test *testing.T) { } func TestGetViewAndControllerFromURL(test *testing.T){ - controllerName, viewName := getViewAndControllerFromURL("test"); + controllerName, viewName := getViewAndControllerFromURL("/test"); assert.Equal(test, controllerName, "hello") assert.Equal(test, viewName, "index") } \ No newline at end of file