github.com/arienmalec/alexa-go@v0.0.0-20181025212142-975687393e90/request_test.go (about) 1 package alexa 2 3 import ( 4 "path/filepath" 5 "io/ioutil" 6 "testing" 7 "encoding/json" 8 "fmt" 9 "strconv" 10 ) 11 12 func Test_unmarshalling(t *testing.T) { 13 file := loadTestFile("alexa_request.json", func() { 14 t.Fatal("Unable to read testdata file") 15 }) 16 17 var request Request 18 if e := json.Unmarshal(file, &request); e!= nil { 19 t.Error("Unable to parse json") 20 } 21 22 resolutions := request.Body.Intent.Slots["SLOT_NAME"].Resolutions.ResolutionPerAuthority 23 values := resolutions[0].Values 24 25 if len(values) != 2 { 26 t.Errorf("Number of values expected 2, but was %d", len(values)) 27 } 28 29 checkValue := func(index int) { 30 expectedValue := fmt.Sprintf(`SLOT_VALUE_%s`, strconv.Itoa(index)) 31 32 if values[index].Value.Name != expectedValue { 33 t.Errorf("Expected `%s` but was `%s` \n", expectedValue, values[index].Value.Name) 34 } 35 36 expectedId := strconv.Itoa(index) 37 if values[index].Value.Id != expectedId { 38 t.Errorf("Expected `%s` but was `%s` \n", expectedId, values[index].Value.Id) 39 } 40 } 41 42 checkValue(0) 43 checkValue(1) 44 } 45 46 func loadTestFile(name string, failureHandler func()) []byte { 47 path := filepath.Join("testdata", name) 48 bytes, e := ioutil.ReadFile(path) 49 if e != nil { 50 failureHandler() 51 } 52 53 return bytes 54 }