From 24dd2bf145472f606bfbe8d5519757403a1ab534 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Thu, 11 Aug 2022 18:57:14 +0000 Subject: [PATCH] added css filter tests --- scraping_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scraping_test.go b/scraping_test.go index 13f0140..dd1c97a 100644 --- a/scraping_test.go +++ b/scraping_test.go @@ -60,6 +60,36 @@ func TestFilterXPath(t *testing.T) { } } +func TestFilterCSS(t *testing.T) { + var tests = []struct { + Query string + Want []string + }{ + {"title", []string{"title"}}, + {".product-table tr td:last-child", []string{`100`, `200`, `300`, `400`}}, + {".price", []string{`100`, `200`, `300`, `400`}}, + {".product-table tr td:nth-child(2)", []string{`10`, `20`, `30`, `40`}}, + {".stock", []string{`10`, `20`, `30`, `40`}}, + } + + for _, test := range tests { + testname := fmt.Sprintf("%s", test.Query) + t.Run(testname, func(t *testing.T) { + want := []string{} + getFilterResultCSS( + HTML_STRING, + &Filter{ + From: test.Query, + }, + &want, + ) + if !reflect.DeepEqual(test.Want, want) { + t.Errorf("Got %s, want %s", want, test.Want) + } + }) + } +} + func TestFilterSubstring(t *testing.T) { var tests = []struct { Input string