blob: d186282aa6fda333c18a717c790fcb1744baf17b [file] [log] [blame]
aPiecek0ebc35b2023-06-22 14:11:58 +02001# @brief Common functions and variables for yanglint-interactive and yanglint-non-interactive.
2#
3# The script requires variables:
4# ::env(TESTS_DIR) - Main test directory. Must be set if the script is run via ctest.
5#
6# The script sets the variables:
7# ::env(TESTS_DIR) - Main test directory. It is set by default if not defined.
8# ::env(YANG_MODULES_DIR) - Directory of YANG modules.
9# TUT_PATH - Assumed absolute path to the directory in which the TUT is located.
10# TUT_NAME - TUT name (without path).
11# ::tcltest::testConstraint ctest - A tcltest variable that is set to true if the script is run via ctest. Causes tests
12# to be a skipped.
13
aPiecek266ca762023-03-22 15:04:59 +010014package require tcltest
15namespace import ::tcltest::test ::tcltest::cleanupTests
16
aPiecek0ebc35b2023-06-22 14:11:58 +020017# Set directory paths for testing yanglint.
aPiecek266ca762023-03-22 15:04:59 +010018if { ![info exists ::env(TESTS_DIR)] } {
19 # the script is not run via 'ctest' so paths must be set
20 set ::env(TESTS_DIR) "../"
21 set ::env(YANG_MODULES_DIR) "../modules"
aPiecek0ebc35b2023-06-22 14:11:58 +020022 set TUT_PATH "../../../build"
aPiecek5bbdc022023-03-27 13:15:46 +020023 ::tcltest::testConstraint ctest false
24} else {
aPiecek0ebc35b2023-06-22 14:11:58 +020025 # cmake (ctest) already sets ::env variables
26 set TUT_PATH $::env(YANGLINT)
aPiecek5bbdc022023-03-27 13:15:46 +020027 ::tcltest::testConstraint ctest true
aPiecek266ca762023-03-22 15:04:59 +010028}
29
aPiecek0ebc35b2023-06-22 14:11:58 +020030set TUT_NAME "yanglint"
aPiecek83e89f22023-05-30 15:47:14 +020031
aPiecek0ebc35b2023-06-22 14:11:58 +020032# The script continues by defining functions specific to the yanglint tool.
aPiecek266ca762023-03-22 15:04:59 +010033
34namespace eval uti {
35 namespace export *
36}
37
38# Iterate through the items in the list 'lst' and return a new list where
39# the items will have the form: <prefix><item><suffix>.
40# Parameter 'index' determines at which index it will start wrapping.
41# Parameter 'step' specifies how far the iterator must move to wrap the next item.
42proc uti::wrap_list_items {lst {prefix ""} {suffix ""} {index 0} {step 1}} {
43 # counter to track when to insert wrapper
44 set cnt $step
45 set len [llength $lst]
46
47 if {$index > 0} {
48 # copy list from interval <0;$index)
49 set ret [lrange $lst 0 [expr {$index - 1}]]
50 } else {
51 set ret {}
52 }
53
54 for {set i $index} {$i < $len} {incr i} {
55 incr cnt
56 set item [lindex $lst $i]
57 if {$cnt >= $step} {
58 # insert wrapper for item
59 set cnt 0
60 lappend ret [string cat $prefix $item $suffix]
61 } else {
62 # just copy item
63 lappend ret $item
64 }
65 }
66
67 return $ret
68}
69
70# Wrap list items with xml tags.
71# The element format is: <tag>value</tag>
72# Parameter 'values' is list of values.
73# Parameter 'tag' is the name of the searched tag.
74proc uti::wrap_to_xml {values tag {index 0} {step 1}} {
75 return [wrap_list_items $values "<$tag>" "</$tag>" $index $step]
76}
77
78# Wrap list items with json attributes.
79# The pair format is: "attribute": "value"
80# Parameter 'values' is list of values.
81# Parameter 'attribute' is the name of the searched attribute.
82proc uti::wrap_to_json {values attribute {index 0} {step 1}} {
83 return [wrap_list_items $values "\"$attribute\": \"" "\"" $index $step]
84}
85
86# Convert list to a regex (which is just a string) so that 'delim' is between items,
87# 'begin' is at the beginning of the expression and 'end' is at the end.
88proc uti::list_to_regex {lst {delim ".*"} {begin ".*"} {end ".*"}} {
89 return [string cat $begin [join $lst $delim] $end]
90}
91
92# Merge two lists into one such that the nth items are merged into one separated by a delimiter.
93# Returns a list that is the same length as 'lst1' and 'lst2'
94proc uti::blend_lists {lst1 lst2 {delim ".*"}} {
95 return [lmap a $lst1 b $lst2 {string cat $a $delim $b}]
96}
97
98# Create regex to find xml elements.
99# The element format is: <tag>value</tag>
100# Parameter 'values' is list of values.
101# Parameter 'tag' is the name of the searched tag.
102# The resulting expression looks like: ".*<tag>value1</tag>.*<tag>value2</tag>.*..."
103proc uti::regex_xml_elements {values tag} {
104 return [list_to_regex [wrap_to_xml $values $tag]]
105}
106
107# Create regex to find json pairs.
108# The pair format is: "attribute": "value"
109# Parameter 'values' is list of values.
110# Parameter 'attribute' is the name of the searched attribute.
111# The resulting expression looks like: ".*\"attribute\": \"value1\".*\"attribute\": \"value2\".*..."
112proc uti::regex_json_pairs {values attribute} {
113 return [list_to_regex [wrap_to_json $values $attribute]]
114}