Skip to content

Commit 2c8f277

Browse files
authored
Merge pull request #23 from mluis7/issue_18
issue_18 Performance and logic improvements
2 parents b661b9e + 52f3e03 commit 2c8f277

File tree

8 files changed

+93
-76
lines changed

8 files changed

+93
-76
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,5 @@ To test the generated man page use:
517517
`MANPATH="./man" man man/xml2xpath.sh.1`
518518

519519
## Known issues
520-
* No one! ... that I know of :-p but [performance](#performance) with big documents can always be an issue.
520+
* `-p` might give inconsistent namespace prefixes on documents with multiple namespaces.
521+
[Performance](#performance) with big documents can always be an issue.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.12.2
1+
0.12.3

tests/test-html-base.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ test_result "$?"
3838

3939
test_opts=(-a -r)
4040
print_test_descr "TC05"
41-
dup_cnt=$(test_run_count)
41+
dup_cnt=$(test_run_basic_count)
4242
test_opts=(-a)
43-
uniq_cnt=$(test_run_count)
43+
uniq_cnt=$(test_run_basic_count)
4444
#[ ! "$dup_cnt" -gt "$uniq_cnt" ] && echo "duplicates: $dup_cnt -gt $uniq_cnt $?"
4545
[ ! "$dup_cnt" -gt "$uniq_cnt" ]
4646
test_result "$?"

tests/test-lib-src.sh

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ echo_with_pid(){
1313
#---------------------------------------------------------------------------------------
1414
# Verify test case result and return result and description
1515
#---------------------------------------------------------------------------------------
16-
function test_result(){
16+
test_result(){
1717
retval=1
1818
[ -n "$2" ] && retval="$2"
1919

@@ -27,13 +27,13 @@ function test_result(){
2727

2828
}
2929

30-
function show_color(){
30+
show_color(){
3131
while read -r line; do
3232
echo -e "\e[01;31m$line\e[0m"
3333
done
3434
}
3535

36-
function quote_opts(){
36+
quote_opts(){
3737
local str=''
3838
for o in "$@"; do
3939
if grep -q "^[-][a-z]" <<<"$o"; then
@@ -48,18 +48,19 @@ function quote_opts(){
4848
#---------------------------------------------------------------------------------------
4949
# Run test case
5050
#---------------------------------------------------------------------------------------
51-
function show_errors(){
51+
show_errors(){
52+
err_pattern='XPath error|No xpath found|Boolean : false'
5253
if [ "$DBG" -eq 1 ]; then
53-
tee /dev/stderr 2> >(show_color) | grep -Eq 'XPath error|No xpath found'
54+
tee /dev/stderr 2> >(show_color) | grep -Eq "$err_pattern"
5455
else
55-
grep -Eq 'XPath error|No xpath found'
56+
grep -Eq "$err_pattern"
5657
fi
5758
}
5859

5960
#---------------------------------------------------------------------------------------
60-
# Run test case
61+
# Print test case description
6162
#---------------------------------------------------------------------------------------
62-
function print_test_descr(){
63+
print_test_descr(){
6364
descr="$1 : ${!1}"
6465
echo_with_pid "$descr"
6566
if [ "$DBG" -eq 1 ]; then
@@ -71,19 +72,42 @@ function print_test_descr(){
7172
#---------------------------------------------------------------------------------------
7273
# Run test case
7374
#---------------------------------------------------------------------------------------
74-
function test_run(){
75+
test_run(){
7576
if [ ! -f "${test_type_opts[${#test_type_opts[@]} - 1]}" ]; then
7677
echo_with_pid "ERROR file not found: ${test_type_opts[${#test_type_opts[@]} - 1]}" | show_color
7778
exit 1
7879
fi
79-
print_test_descr "$1"
80-
print_test_descr "$1" >>"$TRACE_FILE"
80+
print_test_descr "$1" | tee -a "$TRACE_FILE"
8181
../xml2xpath.sh "${test_opts[@]}" "${test_type_opts[@]}" 2>&1 1>>"$TRACE_FILE" | show_errors
8282
}
8383

84+
#---------------------------------------------------------------------------------------
85+
# Run count test case
86+
#---------------------------------------------------------------------------------------
87+
test_run_count(){
88+
if [ ! -f "${test_type_opts[${#test_type_opts[@]} - 1]}" ]; then
89+
echo_with_pid "ERROR file not found: ${test_type_opts[${#test_type_opts[@]} - 1]}" | show_color
90+
exit 1
91+
fi
92+
print_test_descr "$1" | tee -a "$TRACE_FILE"
93+
result=$(../xml2xpath.sh "${test_opts[@]}" "${test_type_opts[@]}")
94+
gawk 'BEGIN{RS="\n\n";FS="\n"}
95+
{
96+
if(NR == 1) printf "%s\n","setrootns"
97+
for(i=1; i<=NF; i++) {
98+
if($i == "") continue
99+
if($0 ~ /^[^=\/]+=/){
100+
printf "%s\n", "setns " $i
101+
} else {
102+
printf "%s\n", "xpath count(" $i ") > 0"
103+
}
104+
}
105+
}END{ printf "bye\n" }' <<<"$result" | xmllint --shell "${test_type_opts[@]}" 2>&1 | show_errors
106+
}
107+
84108
#---------------------------------------------------------------------------------------
85109
# Run test case for duplicates count
86110
#---------------------------------------------------------------------------------------
87-
function test_run_count(){
111+
test_run_basic_count(){
88112
../xml2xpath.sh "${test_opts[@]}" "${test_type_opts[@]}" | wc -l
89113
}

tests/test-xml-ns-01.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ script_name=$(basename "$0")
1010
source test-lib-src.sh
1111
xml_file="resources/HL7.xml"
1212
test_opts=()
13-
test_type_opts=(-x "$xml_file")
13+
test_type_opts=("$xml_file")
1414
rel_xpath='/defaultns:ClinicalDocument/defaultns:recordTarget'
1515

1616
echo_with_pid "*** XML tests - namespaces on root element ($script_name) ***"
@@ -19,6 +19,7 @@ TC01="Basic test (-x)"
1919
TC02="Replace default namespace definition (-o), relative path (-s)"
2020
TC03="Find nodes using namespaces (-n)"
2121
TC04="Find nodes by absolute xpath using namespaces (-a -n)"
22+
TC05="Count nodes with replaced default namespace definition (-o), relative path (-s)"
2223

2324
test_run "TC01"
2425
test_result "$?"
@@ -40,3 +41,8 @@ test_result "$?"
4041
test_opts=(-a -n -s "${rel_xpath}")
4142
test_run "TC04"
4243
test_result "$?"
44+
45+
test_opts=('-q' '-n' '-o' 'defns=urn:hl7-org:v3' '-s' '//defns:addr')
46+
#xml2xpath.sh "${test_opts[@]}" -x "$xml_file" | grep -q 'XPath error'
47+
test_run_count "TC05"
48+
test_result "$?"

tests/test-xml-ns-02.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ script_name=$(basename "$0")
1313
source test-lib-src.sh
1414
xml_file="resources/soap.xml"
1515
test_opts=()
16-
test_type_opts=(-x "$xml_file")
16+
test_type_opts=("$xml_file")
1717
rel_xpath='//incident'
1818

1919
echo_with_pid "*** XML tests - Namespaces on root element and body ($script_name) ***"
2020
# Test case descriptions
2121
TC01="Basic test (-x)"
2222
TC02="Replace default namespace definition (-o), relative path (-s)"
23+
TC04="Test count of nodes found 'TC02' is greater than 0 for all found expressions"
2324

2425
test_run "TC01"
2526
test_result "$?"
@@ -28,3 +29,9 @@ test_result "$?"
2829
test_opts=(-o 'defns=http://example.com/ns1' -s "//defns:incident")
2930
test_run "TC02"
3031
test_result "$?"
32+
33+
#test_type_opts=("resources/ns-with-default.xml")
34+
#test_opts=( '-q' '-s' '//*[local-name()="x"]' )
35+
test_opts=('-q' '-n' '-o' 'defns=http://example.com/ns1' -s "//defns:incident")
36+
test_run_count "TC04"
37+
test_result "$?"

tests/test-xml-ns-03.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ script_name=$(basename "$0")
1717
source test-lib-src.sh
1818
xml_file="resources/html5.html"
1919
test_opts=()
20-
test_type_opts=(-x "$xml_file")
20+
test_type_opts=( "$xml_file" )
2121

2222
echo_with_pid "*** XHTML tests - Namespaces on root element and body. Multiple default namespaces across document. ($script_name) ***"
2323
# Test case descriptions
2424
TC01="Basic test (-x)"
2525
TC02="Replace 'defaultns' prefix (-p)"
2626
TC03="Replace default namespace definition (-o), relative path (-s)"
27-
27+
TC04="Test count of nodes TC03 is greater than 0 for all found expressions"
2828

2929
test_run "TC01"
3030
test_result "$?"
@@ -40,3 +40,6 @@ test_opts=(-o 'dft01=http://www.w3.org/1998/Math/MathML' -s '//dft01:math/dft01:
4040
test_run "TC03"
4141
test_result "$?"
4242

43+
test_opts=("-q" '-n' "-o" 'dft01=http://www.w3.org/1998/Math/MathML' -s '//dft01:math/dft01:mrow/dft01:mn')
44+
test_run_count "TC04"
45+
test_result "$?"

0 commit comments

Comments
 (0)