inifile.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # -*- tcl -*-
  2. # Tests for module 'inifile'
  3. # -------------------------------------------------------------------------
  4. source [file join \
  5. [file dirname [file dirname [file join [pwd] [info script]]]] \
  6. devtools testutilities.tcl]
  7. testsNeedTcl 8.2
  8. testsNeedTcltest 1.0
  9. testing {
  10. useLocal ini.tcl inifile
  11. }
  12. #---------------------------------------------------------------------
  13. set inifile [localPath ini.tcl]
  14. set testini [localPath test.ini]
  15. set sampini [localPath sample.ini]
  16. #---------------------------------------------------------------------
  17. test inifile-1.1 {ini::open} {
  18. set res [ini::open $testini r]
  19. ini::close $res
  20. set res
  21. } {ini0}
  22. test inifile-1.2 {ini::sections} {
  23. set hdl [ini::open $testini r]
  24. set res [ini::sections $hdl]
  25. ini::close $hdl
  26. set res
  27. } {emptysection section1 \{test section2}
  28. test inifile-1.3 {ini::keys} {
  29. set hdl [ini::open $testini r]
  30. set res [ini::keys $hdl section1]
  31. ini::close $hdl
  32. set res
  33. } {testkey key}
  34. test inifile-1.4 {ini::keys} {
  35. set hdl [ini::open $testini r]
  36. set res [ini::keys $hdl \{test]
  37. ini::close $hdl
  38. set res
  39. } {\}key}
  40. test inifile-1.5 {ini::get} {
  41. set hdl [ini::open $testini r]
  42. set res [ini::get $hdl section1]
  43. ini::close $hdl
  44. set res
  45. } {testkey hi key value}
  46. test inifile-1.6 {ini::get} {
  47. set hdl [ini::open $testini r]
  48. set res [ini::get $hdl \{test]
  49. ini::close $hdl
  50. set res
  51. } {\}key {$blah}}
  52. test inifile-1.7 {ini::value} {
  53. set hdl [ini::open $testini r]
  54. set res [ini::value $hdl section1 key]
  55. ini::close $hdl
  56. set res
  57. } {value}
  58. test inifile-1.8 {ini::value} {
  59. set hdl [ini::open $testini r]
  60. set res [ini::value $hdl \{test \}key]
  61. ini::close $hdl
  62. set res
  63. } {$blah}
  64. test inifile-1.9 {ini::exists} {
  65. set hdl [ini::open $testini r]
  66. set res [ini::exists $hdl section1]
  67. ini::close $hdl
  68. set res
  69. } {1}
  70. test inifile-1.10 {ini::exists} {
  71. set hdl [ini::open $testini r]
  72. set res [ini::exists $hdl section]
  73. ini::close $hdl
  74. set res
  75. } {0}
  76. test inifile-1.11 {ini::exists} {
  77. set hdl [ini::open $testini r]
  78. set res [ini::exists $hdl section1 testkey]
  79. ini::close $hdl
  80. set res
  81. } {1}
  82. test inifile-1.12 {ini:::exists} {
  83. set hdl [ini::open $testini r]
  84. set res [ini::exists $hdl section1 blah]
  85. ini::close $hdl
  86. set res
  87. } {0}
  88. test inifile-1.13 {ini:::exists} {
  89. set hdl [ini::open $testini r]
  90. set res [ini::exists $hdl \{test]
  91. ini::close $hdl
  92. set res
  93. } {1}
  94. test inifile-1.14 {ini:::exists} {
  95. set hdl [ini::open $testini r]
  96. set res [ini::exists $hdl \{test \}key]
  97. ini::close $hdl
  98. set res
  99. } {1}
  100. #---------------------------------------------------------------------
  101. # Tests for bug #1281136 --
  102. set N 0
  103. foreach name {nexthandle commentchar} {
  104. test inifile-2.$N {bug 1281136 - collision with global variable names} {
  105. set script {list [catch {
  106. array set ::%var {}
  107. source %file
  108. } err] $err}
  109. regsub {%file} $script $inifile script
  110. regsub {%var} $script $name script
  111. interp create slave0
  112. set r [slave0 eval $script]
  113. interp delete slave0
  114. set r
  115. } {0 {}}
  116. incr N
  117. }
  118. foreach name {data comments sections} {
  119. test inifile-2.$N {bug 1281136 - collision with global variable names} {
  120. set script {list [catch {
  121. ::set ::%var 0
  122. source %file
  123. set res [ini::open %testini r]
  124. ini::close $res
  125. } err] $err}
  126. foreach {s v} [list %file $inifile %var $name %testini $testini] {
  127. regsub $s $script $v script
  128. }
  129. interp create slave0
  130. set r [slave0 eval $script]
  131. interp delete slave0
  132. set r
  133. } {0 {}}
  134. incr N
  135. }
  136. #---------------------------------------------------------------------
  137. test inifile-3.0 {bug 3612465, leading & trailing spaces} {
  138. set fh [ini::open $sampini]
  139. set res [ini::sections $fh]
  140. ini::close $fh
  141. unset fh
  142. set res
  143. } General
  144. test inifile-3.1 {bug 3612465, leading & trailing spaces} {
  145. set fh [ini::open $sampini]
  146. #set res [llength [ini::sections $fh]]
  147. set res [lsort -dict [ini::keys $fh General]]
  148. ini::close $fh
  149. unset fh
  150. set res
  151. } {key key2}
  152. #---------------------------------------------------------------------
  153. test inifile-4.0 {bug c4b8162da5 - ini::open} {
  154. set res [ini::open $testini -encoding unicode r]
  155. ini::close $res
  156. set res
  157. } {ini16}
  158. # Test various error conditions.
  159. test inifile-4.1 {bug c4b8162da5 - ini::open - invalid encoding} {
  160. catch {
  161. ini::open $testini -encoding foo r
  162. } res
  163. set res
  164. } {unknown encoding "foo"}
  165. test inifile-4.2 {bug c4b8162da5 - ini::open - invalid option} {
  166. catch {
  167. ini::open $testini -bogus foo r
  168. } res
  169. set res
  170. } {Invalid option -bogus, expected -encoding}
  171. test inifile-4.3 {bug c4b8162da5 - ini::open - invalid mode} {
  172. catch {
  173. ini::open $testini x
  174. } res
  175. set res
  176. } {x is not a valid access mode}
  177. test inifile-4.4 {bug c4b8162da5 - ini::open - invalid mode} {
  178. catch {
  179. set res [ini::open $testini w-]
  180. } res
  181. set res
  182. } {w- is not a valid access mode}
  183. #---------------------------------------------------------------------
  184. # Clean up
  185. testsuiteCleanup
  186. return