From 61656460cd65164a72b69a1bdbd6d0504a3488f5 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Sat, 30 Mar 2024 12:58:53 +0800 Subject: [PATCH] update tests --- func_test.go | 56 ------------------------------------------ xpath_function_test.go | 21 ++++++++++++++++ xpath_test.go | 22 +++++++++++++++++ 3 files changed, 43 insertions(+), 56 deletions(-) delete mode 100644 func_test.go diff --git a/func_test.go b/func_test.go deleted file mode 100644 index aafcea4..0000000 --- a/func_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package xpath - -import "testing" - -type testQuery string - -func (t testQuery) Select(_ iterator) NodeNavigator { - panic("implement me") -} - -func (t testQuery) Clone() query { - return t -} - -func (t testQuery) Evaluate(_ iterator) interface{} { - return string(t) -} - -func (t testQuery) ValueType() resultType { - return xpathResultType.Any -} - -func (t testQuery) Properties() queryProp { - return queryProps.None -} - -const strForNormalization = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g " -const expectedStrAfterNormalization = `loooooooonnnnnnngggggggg tes t strin g` - -func Test_NormalizeSpaceFunc(t *testing.T) { - result := normalizespaceFunc(testQuery(strForNormalization), nil).(string) - if expectedStrAfterNormalization != result { - t.Fatalf("unexpected result '%s'", result) - } -} - -func Test_ConcatFunc(t *testing.T) { - result := concatFunc(testQuery("a"), testQuery("b"))(nil, nil).(string) - if "ab" != result { - t.Fatalf("unexpected result '%s'", result) - } -} - -func Benchmark_NormalizeSpaceFunc(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = normalizespaceFunc(testQuery(strForNormalization), nil) - } -} - -func Benchmark_ConcatFunc(b *testing.B) { - b.ReportAllocs() - for i := 0; i < b.N; i++ { - _ = concatFunc(testQuery("a"), testQuery("b"))(nil, nil) - } -} diff --git a/xpath_function_test.go b/xpath_function_test.go index f446c44..aed0824 100644 --- a/xpath_function_test.go +++ b/xpath_function_test.go @@ -43,6 +43,8 @@ func Test_func_concat(t *testing.T) { test_xpath_eval(t, empty_example, `concat("1", "2", "3")`, "123") //test_xpath_eval(t, empty_example, `concat("Ciao!", ())`, "Ciao!") test_xpath_eval(t, book_example, `concat(//book[1]/title, ", ", //book[1]/year)`, "Everyday Italian, 2005") + result := concatFunc(testQuery("a"), testQuery("b"))(nil, nil).(string) + assertEqual(t, result, "ab") } func Test_func_contains(t *testing.T) { @@ -220,7 +222,26 @@ func Test_func_namespace_uri(t *testing.T) { } func Test_func_normalize_space(t *testing.T) { + const testStr = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g " + const expectedStr = `loooooooonnnnnnngggggggg tes t strin g` + test_xpath_eval(t, employee_example, `normalize-space("`+testStr+`")`, expectedStr) + test_xpath_eval(t, empty_example, `normalize-space(' abc ')`, "abc") test_xpath_eval(t, book_example, `normalize-space(//book/title)`, "Everyday Italian") test_xpath_eval(t, book_example, `normalize-space(//book[1]/title)`, "Everyday Italian") } + +func Benchmark_NormalizeSpaceFunc(b *testing.B) { + b.ReportAllocs() + const strForNormalization = "\t \rloooooooonnnnnnngggggggg \r \n tes \u00a0 t strin \n\n \r g " + for i := 0; i < b.N; i++ { + _ = normalizespaceFunc(testQuery(strForNormalization), nil) + } +} + +func Benchmark_ConcatFunc(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _ = concatFunc(testQuery("a"), testQuery("b"))(nil, nil) + } +} diff --git a/xpath_test.go b/xpath_test.go index f89fa42..99b2873 100644 --- a/xpath_test.go +++ b/xpath_test.go @@ -15,6 +15,28 @@ var ( mybook_example = createMyBookExample() ) +type testQuery string + +func (t testQuery) Select(_ iterator) NodeNavigator { + panic("implement me") +} + +func (t testQuery) Clone() query { + return t +} + +func (t testQuery) Evaluate(_ iterator) interface{} { + return string(t) +} + +func (t testQuery) ValueType() resultType { + return xpathResultType.Any +} + +func (t testQuery) Properties() queryProp { + return queryProps.None +} + func test_xpath_elements(t *testing.T, root *TNode, expr string, expected ...int) { result := selectNodes(root, expr) assertEqual(t, len(expected), len(result))