blob: 0cebd0dcfb00caa902dbdf8ee413a0ef768154e5 [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"
aPiecek83e89f22023-05-30 15:47:14 +02008 set ::env(YANGLINT) "../../../../build"
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
aPiecek83e89f22023-05-30 15:47:14 +020014# Complete the path for yanglint. For example, on Windows, yanglint can be located in the Debug or Release subdirectory.
15# Note that Release build takes precedence over Debug.
16set conftypes {{} Release Debug E}
17foreach i $conftypes {
18 if { [file executable "$::env(YANGLINT)/$i/yanglint"] || [file executable "$::env(YANGLINT)/$i/yanglint.exe"] } {
19 append ::env(YANGLINT) "/$i/yanglint"
20 break
21 }
22}
23if { $i == "E" } {
24 error "yanglint executable not found"
25}
26
aPiecek266ca762023-03-22 15:04:59 +010027# prompt of error message
28set error_prompt ">>>"
29# the beginning of error message
30set error_head "$error_prompt Check-failed"
31
32namespace eval uti {
33 namespace export *
34}
35
36# Iterate through the items in the list 'lst' and return a new list where
37# the items will have the form: <prefix><item><suffix>.
38# Parameter 'index' determines at which index it will start wrapping.
39# Parameter 'step' specifies how far the iterator must move to wrap the next item.
40proc uti::wrap_list_items {lst {prefix ""} {suffix ""} {index 0} {step 1}} {
41 # counter to track when to insert wrapper
42 set cnt $step
43 set len [llength $lst]
44
45 if {$index > 0} {
46 # copy list from interval <0;$index)
47 set ret [lrange $lst 0 [expr {$index - 1}]]
48 } else {
49 set ret {}
50 }
51
52 for {set i $index} {$i < $len} {incr i} {
53 incr cnt
54 set item [lindex $lst $i]
55 if {$cnt >= $step} {
56 # insert wrapper for item
57 set cnt 0
58 lappend ret [string cat $prefix $item $suffix]
59 } else {
60 # just copy item
61 lappend ret $item
62 }
63 }
64
65 return $ret
66}
67
68# Wrap list items with xml tags.
69# The element format is: <tag>value</tag>
70# Parameter 'values' is list of values.
71# Parameter 'tag' is the name of the searched tag.
72proc uti::wrap_to_xml {values tag {index 0} {step 1}} {
73 return [wrap_list_items $values "<$tag>" "</$tag>" $index $step]
74}
75
76# Wrap list items with json attributes.
77# The pair format is: "attribute": "value"
78# Parameter 'values' is list of values.
79# Parameter 'attribute' is the name of the searched attribute.
80proc uti::wrap_to_json {values attribute {index 0} {step 1}} {
81 return [wrap_list_items $values "\"$attribute\": \"" "\"" $index $step]
82}
83
84# Convert list to a regex (which is just a string) so that 'delim' is between items,
85# 'begin' is at the beginning of the expression and 'end' is at the end.
86proc uti::list_to_regex {lst {delim ".*"} {begin ".*"} {end ".*"}} {
87 return [string cat $begin [join $lst $delim] $end]
88}
89
90# Merge two lists into one such that the nth items are merged into one separated by a delimiter.
91# Returns a list that is the same length as 'lst1' and 'lst2'
92proc uti::blend_lists {lst1 lst2 {delim ".*"}} {
93 return [lmap a $lst1 b $lst2 {string cat $a $delim $b}]
94}
95
96# Create regex to find xml elements.
97# The element format is: <tag>value</tag>
98# Parameter 'values' is list of values.
99# Parameter 'tag' is the name of the searched tag.
100# The resulting expression looks like: ".*<tag>value1</tag>.*<tag>value2</tag>.*..."
101proc uti::regex_xml_elements {values tag} {
102 return [list_to_regex [wrap_to_xml $values $tag]]
103}
104
105# Create regex to find json pairs.
106# The pair format is: "attribute": "value"
107# Parameter 'values' is list of values.
108# Parameter 'attribute' is the name of the searched attribute.
109# The resulting expression looks like: ".*\"attribute\": \"value1\".*\"attribute\": \"value2\".*..."
110proc uti::regex_json_pairs {values attribute} {
111 return [list_to_regex [wrap_to_json $values $attribute]]
112}