// Copyright 2016 Alexander Palaistras. All rights reserved. // Use of this source code is governed by the MIT license that can be found in // the LICENSE file. package farsight import ( // Standard library. "fmt" "io" "reflect" "strings" "testing" // Internal packages. "github.com/deuill/farsight/source" ) type TestSource struct { data map[string]TestCase } func (t *TestSource) Fetch(src string) (io.Reader, error) { if _, exists := t.data[src]; !exists { return nil, fmt.Errorf("Unknown source data requested") } return strings.NewReader(t.data[src].Content), nil } type TestCase struct { Content string Actual interface{} Expected interface{} } // Test cases for `farsight.Fetch` function. var fetchTests = map[string]TestCase{ "html://string": { `
Hello World
`, &struct { Text string `farsight:"#hello"` }{}, &struct { Text string `farsight:"#hello"` }{ "Hello World", }, }, "html://int": { `
42
`, &struct { Num int64 `farsight:"#numberwang"` }{}, &struct { Num int64 `farsight:"#numberwang"` }{ 42, }, }, "html://float": { `
3.14159
`, &struct { Pi float64 `farsight:"#pi"` }{}, &struct { Pi float64 `farsight:"#pi"` }{ 3.14159, }, }, "html://slice": { ``, &struct { List []string `farsight:"#g li"` }{}, &struct { List []string `farsight:"#g li"` }{ []string{"Hello", "World"}, }, }, "html://attr": { `
Here!
`, &struct { Link string `farsight:"div a/href"` }{}, &struct { Link string `farsight:"div a/href"` }{ "http://deuill.org", }, }, "html://subdoc": { ``, &struct { Items []struct { Text string `farsight:"i"` } `farsight:"ul li"` }{}, &struct { Items []struct { Text string `farsight:"i"` } `farsight:"ul li"` }{ []struct { Text string `farsight:"i"` }{ {"Eenie"}, {"Meenie"}, }, }, }, } func TestFetch(t *testing.T) { // Register mock source. source.Register("html", &TestSource{data: fetchTests}) // Execute tests sequentially. for k, v := range fetchTests { if err := Fetch(k, v.Actual, "html"); err != nil { t.Errorf("Fetch failed for '%s': %s", k, err) } if reflect.DeepEqual(v.Actual, v.Expected) == false { t.Errorf("Testing '%s' failed: expected '%v', actual '%v'\n", k, v.Expected, v.Actual) } } }