func TestUrlEncoderForm(t *testing.T) { path := "/cars/UHPHs2-Q6Q7Ey1gJ/comments/flame" query := Values{} query.Add("favourites", "true") query.Add("1fake", "11") route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} sentence, err := lang.NewSentence(route, speaker) if err != nil { t.Fatal() } if sentence.Verb != "Flame" && sentence.Noun != "comments" { t.Fatal() } urle := lang.NewURLEncoder(route, sentence) form := urle.Form("whatever-action") if form.KeyPrefix != "1" { t.Fatal(form.KeyPrefix) } if len(form.FilterFields) != 2 || form.FilterFields["favourites"] != "true" || form.FilterFields["1fake"] != "11" { t.Fatal() } if form.ActionPath != "cars/UHPHs2-Q6Q7Ey1gJ/comments/whatever-action" { t.Fatal() } }
func TestURLEncoderUrlNonGet(t *testing.T) { path := "/cars/ignite" query := Values{} query.Add("favourites", "true") query.Add("1fake", "11") route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} sentence, err := lang.NewSentence(route, speaker) if err != nil { t.Fatal() } if sentence.Verb != "Ignite" { t.Fatal() } urle := lang.NewURLEncoder(route, sentence) u1 := Values{} u1.Add("color", "red") u1.Add("color", "blue") u1.Add("quality", "very high") path, merged := urle.Url("paint", u1) if path != "cars/paint" { t.Fatal(path) } if merged["favourites"] != "true" || merged["1color"].([]interface{})[0] != "red" || merged["1color"].([]interface{})[1] != "blue" || merged["1quality"] != "very high" { t.Fatal(merged) } }
func Identify(path string, nouns map[string]interface{}, inp map[string]interface{}) (*Descriptor, error) { desc := &Descriptor{} r, err := lang.NewRoute(path, inp) if err != nil { return nil, err } desc.Route = r speaker := speaker.New(moduleHasVerb, nouns) sentence, err := lang.NewSentence(r, speaker) if err != nil { return nil, err } desc.Sentence = sentence nounOpt, has := nouns[sentence.Noun].(map[string]interface{}) if !has { return nil, fmt.Errorf("Noun opt is missing or not a map.") } desc.nounOpt = nounOpt loc := speaker.VerbLocation(sentence.Noun, sentence.Verb) if loc == "" { return nil, fmt.Errorf("Verb %v in noun %v is not defined.", sentence.Verb, sentence.Noun) } desc.VerbLocation = loc return desc, nil }
func TestSentence1(t *testing.T) { path := "/cars/not-existing-verb" query := Values{} route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} _, err = lang.NewSentence(route, speaker) if err == nil { t.Fatal() } }
func TestSentence(t *testing.T) { path := "/cars/ignite" query := Values{} route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} sentence, err := lang.NewSentence(route, speaker) if err != nil { t.Fatal(err) } if sentence.Noun != "cars" || sentence.Verb != "Ignite" || sentence.Redundant != "" { t.Fatal() } }
func TestSingle(t *testing.T) { path := "/cars/UHPHs2-Q6Q7Ey1gJ" query := Values{} route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} sentence, err := lang.NewSentence(route, speaker) if err != nil { t.Fatal() } if sentence.Verb != "GetSingle" { t.Fatal() } }
func TestUrlEncoderForm1(t *testing.T) { path := "/cars/ignite" query := Values{} route, err := lang.NewRoute(path, query) if err != nil { t.Fatal() } speaker := MockSpeaker{} sentence, err := lang.NewSentence(route, speaker) if err != nil { t.Fatal() } urle := lang.NewURLEncoder(route, sentence) form := urle.Form("whatever-action") if form.KeyPrefix != "1" { t.Fatal(form.KeyPrefix) } }