// SPDX-FileCopyrightText: Copyright (c) 2018 Babiv Sergey // // SPDX-License-Identifier: MIT package xml2map_test import ( "strings" "testing" "codeberg.org/readeck/readeck/pkg/xml2map" ) func BenchmarkDecoder(b *testing.B) { for n := 0; n < b.N; n++ { _, err := xml2map.NewDecoder(strings.NewReader(` CDA035B6-D453-4A17-B090-84295AE2DEC5 moritz 7 1293 1255 1257 1634C644-975F-4302-8336-1EF1366EC6A4 oliver 12 white NY `)).Decode() if err != nil { b.Fatal(err) } } } func TestStartAttrs(t *testing.T) { tests := []string{ ` white `, ` white `, ` white `, } for _, s := range tests { _, err := xml2map.NewDecoder(strings.NewReader(s)).Decode() if err == nil { t.Fail() } } } func TestNs(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader( ``)).Decode() if err != nil { t.Errorf("m: %v, err: %v\n", m, err) } } func TestPars(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader( ` 1 2 3 `)).Decode() if err != nil { t.Errorf("m: %v, err: %v\n", m, err) } } func TestErrDecoder(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader( ` Smith John>
1310 Villa Street Mountain View CA 94041
`)).Decode() if m == nil && err != nil { t.Logf("result: %v err: %v\n", m, err) } else { t.Errorf("err %v\n", err) } } func TestEmpty(t *testing.T) { tests := []string{"", " ", " ", ``, ` `, "\n"} for _, s := range tests { _, err := xml2map.NewDecoder(strings.NewReader(s)).Decode() if err != xml2map.ErrInvalidDocument { t.Fail() } } } func TestSpaces(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader(` data `)).Decode() if err != nil { t.Errorf("err %v\n", err) } else if m["note"] != "data" { t.Errorf("data not found") } } func TestInvalidStartIndex(t *testing.T) { _, err := xml2map.NewDecoder(strings.NewReader(`d data `)).Decode() if err == nil || err.Error() != "data at the root level is invalid" { t.Fail() } } func TestDecode(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader( ` CDA035B6-D453-4A17-B090-84295AE2DEC5 moritz 7 1293 1255 1257 1634C644-975F-4302-8336-1EF1366EC6A4 oliver 12 1293 1255 1257 hello white NY `)).Decode() if err != nil { t.Errorf("err: %v", err) } container := m["container"].(map[string]any) if container["@uid"] != "FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" && container["lifetime"] != "2019-10-10T18:00:11" { t.Errorf("container attrs not exists") } else { cats := container["cats"].(map[string]any) catsItems := cats["cat"].([]map[string]any) if len(catsItems) != 2 { t.Errorf("cats slice != 2") } dog := cats["dog"].(map[string]any) if dog["@color"] != "gray" || dog["#text"] != "hello" { t.Errorf("bad value or attr dog") } if container["color"] != "white" || container["city"] != "NY" { t.Errorf("bad value color") } cat := catsItems[0] if cat["id"] != "" && cat["name"] != "" && cat["age"] != "" { items := cat["items"].(map[string]any)["n"].([]string) if len(items) != 3 { t.Errorf("items len %v", len(items)) } } } } func TestWithPrefix(t *testing.T) { m, err := xml2map.NewDecoderWithPrefix(strings.NewReader( ` 1 2 3 `), "$", "#").Decode() if err != nil { t.Errorf("m: %v, err: %v\n", m, err) } customer := m["customer"].(map[string]any) if customer["$id"] != "FA6666D9-EC9F-4DA3-9C3D-4B2460A4E1F6" && customer["$lifetime"] != "2019-10-10T18:00:11" { t.Errorf("customer tag attr not found") } else { items := customer["items"].(map[string]any) if items["$id"] != "100" || items["$count"] != "3" { t.Errorf("items tag attr not found") } else { list := items["n"].([]map[string]any) if len(list) != 3 { t.Errorf("list len %v", len(items)) } else if list[1]["$id"] != "20" && list[1]["%"] != "2" { t.Errorf("invalid parse n element attr or text") } } } } func TestWithNameSpace(t *testing.T) { m, err := xml2map.NewDecoder(strings.NewReader( ` example.com RSS https://www.example.com/ A cool website Atom Title Cool Article https://www.example.com/cool-article https://www.example.com/cool-article Sun, 10 Dec 2017 05:00:00 GMT My cool article description `)).Decode() if err != nil { t.Errorf("m: %v, err: %v\n", m, err) } rss := m["rss"].(map[string]any)["channel"].(map[string]any) if rss["atom:title"] != "Atom Title" { t.Errorf("invalid value for namespace node") } }