blob: ed6046359a870f3107b440b4415c08ddddb29920 [file] [log] [blame]
aPiecek266ca762023-03-22 15:04:59 +01001package require tcltest
2namespace import ::tcltest::test ::tcltest::cleanupTests
3
4if { ![info exists ::env(TESTS_DIR)] } {
5 # the script is not run via 'ctest' so paths must be set
6 set ::env(TESTS_DIR) "../"
7 set ::env(YANG_MODULES_DIR) "../modules"
8 set ::env(YANGLINT) "../../../../build/yanglint"
aPiecek5bbdc022023-03-27 13:15:46 +02009 ::tcltest::testConstraint ctest false
10} else {
11 ::tcltest::testConstraint ctest true
aPiecek266ca762023-03-22 15:04:59 +010012}
13
14# prompt of error message
15set error_prompt ">>>"
16# the beginning of error message
17set error_head "$error_prompt Check-failed"
18
19namespace eval uti {
20 namespace export *
21}
22
23# Iterate through the items in the list 'lst' and return a new list where
24# the items will have the form: <prefix><item><suffix>.
25# Parameter 'index' determines at which index it will start wrapping.
26# Parameter 'step' specifies how far the iterator must move to wrap the next item.
27proc uti::wrap_list_items {lst {prefix ""} {suffix ""} {index 0} {step 1}} {
28 # counter to track when to insert wrapper
29 set cnt $step
30 set len [llength $lst]
31
32 if {$index > 0} {
33 # copy list from interval <0;$index)
34 set ret [lrange $lst 0 [expr {$index - 1}]]
35 } else {
36 set ret {}
37 }
38
39 for {set i $index} {$i < $len} {incr i} {
40 incr cnt
41 set item [lindex $lst $i]
42 if {$cnt >= $step} {
43 # insert wrapper for item
44 set cnt 0
45 lappend ret [string cat $prefix $item $suffix]
46 } else {
47 # just copy item
48 lappend ret $item
49 }
50 }
51
52 return $ret
53}
54
55# Wrap list items with xml tags.
56# The element format is: <tag>value</tag>
57# Parameter 'values' is list of values.
58# Parameter 'tag' is the name of the searched tag.
59proc uti::wrap_to_xml {values tag {index 0} {step 1}} {
60 return [wrap_list_items $values "<$tag>" "</$tag>" $index $step]
61}
62
63# Wrap list items with json attributes.
64# The pair format is: "attribute": "value"
65# Parameter 'values' is list of values.
66# Parameter 'attribute' is the name of the searched attribute.
67proc uti::wrap_to_json {values attribute {index 0} {step 1}} {
68 return [wrap_list_items $values "\"$attribute\": \"" "\"" $index $step]
69}
70
71# Convert list to a regex (which is just a string) so that 'delim' is between items,
72# 'begin' is at the beginning of the expression and 'end' is at the end.
73proc uti::list_to_regex {lst {delim ".*"} {begin ".*"} {end ".*"}} {
74 return [string cat $begin [join $lst $delim] $end]
75}
76
77# Merge two lists into one such that the nth items are merged into one separated by a delimiter.
78# Returns a list that is the same length as 'lst1' and 'lst2'
79proc uti::blend_lists {lst1 lst2 {delim ".*"}} {
80 return [lmap a $lst1 b $lst2 {string cat $a $delim $b}]
81}
82
83# Create regex to find xml elements.
84# The element format is: <tag>value</tag>
85# Parameter 'values' is list of values.
86# Parameter 'tag' is the name of the searched tag.
87# The resulting expression looks like: ".*<tag>value1</tag>.*<tag>value2</tag>.*..."
88proc uti::regex_xml_elements {values tag} {
89 return [list_to_regex [wrap_to_xml $values $tag]]
90}
91
92# Create regex to find json pairs.
93# The pair format is: "attribute": "value"
94# Parameter 'values' is list of values.
95# Parameter 'attribute' is the name of the searched attribute.
96# The resulting expression looks like: ".*\"attribute\": \"value1\".*\"attribute\": \"value2\".*..."
97proc uti::regex_json_pairs {values attribute} {
98 return [list_to_regex [wrap_to_json $values $attribute]]
99}