From ab7a7fd534603c56ffa8f271f11f75323f51eb14 Mon Sep 17 00:00:00 2001 From: BroodjeAap Date: Thu, 11 Aug 2022 18:28:11 +0000 Subject: [PATCH] added test for XPath filter --- scraping_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/scraping_test.go b/scraping_test.go index 7ac0cfc..80f8bca 100644 --- a/scraping_test.go +++ b/scraping_test.go @@ -2,9 +2,61 @@ package main import ( "fmt" + "reflect" "testing" ) +const HTML_STRING = ` + + title + + + + + + + + + + + + + + + + + +
product-table-caption
NameStockPrice
product110100
product220200
product330300
product440400
+ +` + +func TestFilterXPath(t *testing.T) { + var tests = []struct { + Query string + Want []string + }{ + {"//title", []string{"title"}}, + {"//table[@id='product-table']//tr//td[last()]", []string{"100", "200", "300", "400"}}, + } + + for _, test := range tests { + testname := fmt.Sprintf("%s", test.Query) + t.Run(testname, func(t *testing.T) { + want := []string{} + getFilterResultXPath( + 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