| 1 |
package uaparser |
| 2 |
|
| 3 |
import ( |
| 4 |
"io/ioutil" |
| 5 |
"log" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"gopkg.in/yaml.v2" |
| 9 |
) |
| 10 |
|
| 11 |
var testParser *Parser |
| 12 |
|
| 13 |
func init() { |
| 14 |
var err error |
| 15 |
testParser, err = New("../uap-core/regexes.yaml") |
| 16 |
if err != nil { |
| 17 |
log.Fatal(err) |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestOSParsing(t *testing.T) { |
| 22 |
var YAMLTestCases struct { |
| 23 |
Cases []struct { |
| 24 |
Input string `yaml:"user_agent_string"` |
| 25 |
Expected Os `yaml:",inline"` |
| 26 |
} `yaml:"test_cases"` |
| 27 |
} |
| 28 |
|
| 29 |
for _, filepath := range []string{"../uap-core/test_resources/additional_os_tests.yaml", "../uap-core/tests/test_os.yaml"} { |
| 30 |
unmarshalResourceTestFile(filepath, &YAMLTestCases) |
| 31 |
|
| 32 |
for _, c := range YAMLTestCases.Cases { |
| 33 |
actual := testParser.ParseOs(c.Input) |
| 34 |
if got, want := *actual, c.Expected; got != want { |
| 35 |
t.Fatalf("got %#v\n want %#v\n", got, want) |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestReadsInteralYAML(t *testing.T) { |
| 42 |
_ = NewFromSaved() // should not panic |
| 43 |
} |
| 44 |
|
| 45 |
func TestUAParsing(t *testing.T) { |
| 46 |
t.Parallel() |
| 47 |
|
| 48 |
var YAMLTestCases struct { |
| 49 |
Cases []struct { |
| 50 |
Input string `yaml:"user_agent_string"` |
| 51 |
Expected UserAgent `yaml:",inline"` |
| 52 |
} `yaml:"test_cases"` |
| 53 |
} |
| 54 |
|
| 55 |
for _, filepath := range []string{"../uap-core/test_resources/pgts_browser_list.yaml", "../uap-core/test_resources/firefox_user_agent_strings.yaml", "../uap-core/tests/test_ua.yaml"} { |
| 56 |
unmarshalResourceTestFile(filepath, &YAMLTestCases) |
| 57 |
|
| 58 |
for _, c := range YAMLTestCases.Cases { |
| 59 |
actual := testParser.ParseUserAgent(c.Input) |
| 60 |
if got, want := *actual, c.Expected; got != want { |
| 61 |
t.Fatalf("got %#v\n want %#v\n", got, want) |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestDeviceParsing(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
|
| 70 |
var YAMLTestCases struct { |
| 71 |
Cases []struct { |
| 72 |
Input string `yaml:"user_agent_string"` |
| 73 |
Expected Device `yaml:",inline"` |
| 74 |
} `yaml:"test_cases"` |
| 75 |
} |
| 76 |
|
| 77 |
for _, filepath := range []string{"../uap-core/tests/test_device.yaml"} { |
| 78 |
unmarshalResourceTestFile(filepath, &YAMLTestCases) |
| 79 |
|
| 80 |
for _, c := range YAMLTestCases.Cases { |
| 81 |
actual := testParser.ParseDevice(c.Input) |
| 82 |
if got, want := *actual, c.Expected; got != want { |
| 83 |
t.Fatalf("got %#v\n want %#v\n", got, want) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestGenericParseMethodConcurrency(t *testing.T) { // go test -race -run=Concurrency |
| 90 |
var YAMLTestCases struct { |
| 91 |
Cases []struct { |
| 92 |
Input string `yaml:"user_agent_string"` |
| 93 |
Expected Os `yaml:",inline"` |
| 94 |
} `yaml:"test_cases"` |
| 95 |
} |
| 96 |
|
| 97 |
for _, filepath := range []string{"../uap-core/tests/test_os.yaml"} { |
| 98 |
unmarshalResourceTestFile(filepath, &YAMLTestCases) |
| 99 |
|
| 100 |
for _, c := range YAMLTestCases.Cases { |
| 101 |
actual := testParser.Parse(c.Input).Os |
| 102 |
if got, want := *actual, c.Expected; got != want { |
| 103 |
t.Fatalf("got %#v\n want %#v\n", got, want) |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func unmarshalResourceTestFile(filepath string, v interface{}) { |
| 110 |
file, err := ioutil.ReadFile(filepath) |
| 111 |
if nil != err { |
| 112 |
log.Fatal(err) |
| 113 |
} |
| 114 |
|
| 115 |
if err := yaml.Unmarshal(file, v); err != nil { |
| 116 |
log.Fatal(err) |
| 117 |
} |
| 118 |
} |