CLI and configuration library built especially for the parallelcoin pod
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validators_test.go 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. package tri
  2. import (
  3. "time"
  4. "testing"
  5. )
  6. func MakeTestHandler() func(*Tri) int {
  7. return func(*Tri) int { return 0 }
  8. }
  9. func TestBrief(t *testing.T) {
  10. // one item only
  11. tb1 := Brief{
  12. "item1", "item2",
  13. }
  14. e := tb1.Validate()
  15. if e == nil {
  16. t.Error("validator allowed more than one")
  17. }
  18. // string typed item
  19. tb2 := Brief{
  20. 1,
  21. }
  22. e = tb2.Validate()
  23. if e == nil {
  24. t.Error("validator permitted other than a string")
  25. }
  26. // string length < 80
  27. tb3 := Brief{
  28. "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
  29. }
  30. e = tb3.Validate()
  31. if e == nil {
  32. t.Error("validator permitted over 80 characters")
  33. }
  34. // no control characters
  35. tb4 := Brief{
  36. "this should not have a cr at the end\n",
  37. }
  38. e = tb4.Validate()
  39. if e == nil {
  40. t.Error("validator permitted over 80 characters")
  41. }
  42. tb5 := Brief{
  43. "this should not have a cr at the end\n",
  44. }
  45. e = tb5.Validate()
  46. if e == nil {
  47. t.Error("validator permitted over 80 characters")
  48. }
  49. tb6 := Brief{
  50. "this is ok",
  51. }
  52. e = tb6.Validate()
  53. if e != nil {
  54. t.Error("validator rejected correct input")
  55. }
  56. }
  57. func TestCommand(t *testing.T) {
  58. // not empty
  59. tc0 := Command{}
  60. if e := tc0.Validate(); e == nil {
  61. t.Error("validator accepted empty Command")
  62. }
  63. //string in index 0
  64. tc1 := Command{1}
  65. if e := tc1.Validate(); e == nil {
  66. t.Error("validator accepted non-string name")
  67. }
  68. //string is valid name (letters only)
  69. tc2 := Command{"!"}
  70. if e := tc2.Validate(); e == nil {
  71. t.Error("validator accepted invalid name")
  72. }
  73. //more than one brief not allowed
  74. tc3 := Command{"name", Brief{""}, Brief{""}}
  75. if e := tc3.Validate(); e == nil {
  76. t.Error("validator accepted more than one Brief")
  77. }
  78. // brief is invalid
  79. tc4 := Command{"name", Brief{}}
  80. if e := tc4.Validate(); e == nil {
  81. t.Error("validator accepted invalid Brief")
  82. }
  83. //more than one handler not allowed
  84. tc5 := Command{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
  85. if e := tc5.Validate(); e == nil {
  86. t.Error("validator accepted more than one handler")
  87. }
  88. // Handler not nil
  89. isnil := MakeTestHandler()
  90. _ = isnil
  91. isnil = nil
  92. tc6 := Command{"name", isnil}
  93. if e := tc6.Validate(); e == nil {
  94. t.Error("validator accepted nil MakeTestHandler()")
  95. }
  96. // no more than one Short
  97. tc7 := Command{"name", Short{'a'}, Short{'b'}}
  98. if e := tc7.Validate(); e == nil {
  99. t.Error("validator accepted more than one Short")
  100. }
  101. // invalid Short
  102. tc8 := Command{"name", Short{}}
  103. if e := tc8.Validate(); e == nil {
  104. t.Error("validator accepted invalid Short")
  105. }
  106. // no more than one Usage
  107. tc9 := Command{"name", Usage{""}, Usage{""}}
  108. if e := tc9.Validate(); e == nil {
  109. t.Error("validator accepted more than one Short")
  110. }
  111. // invalid Usage
  112. tc10 := Command{"name", Usage{}}
  113. if e := tc10.Validate(); e == nil {
  114. t.Error("validator invalid Usage")
  115. }
  116. // no more than one Help
  117. tc11 := Command{"name", Help{""}, Help{""}}
  118. if e := tc11.Validate(); e == nil {
  119. t.Error("validator accepted more than one Short")
  120. }
  121. // invalid Help
  122. tc12 := Command{"name", Help{}}
  123. if e := tc12.Validate(); e == nil {
  124. t.Error("validator invalid Usage")
  125. }
  126. // no more than one Examples
  127. tc13 := Command{"name", Examples{"", ""}, Examples{"", ""}}
  128. if e := tc13.Validate(); e == nil {
  129. t.Error("validator accepted more than one Short")
  130. }
  131. // invalid Examples
  132. tc14 := Command{"name", Examples{}}
  133. if e := tc14.Validate(); e == nil {
  134. t.Error("validator invalid Usage")
  135. }
  136. //invalid Var
  137. tc15 := Command{"name", Var{}}
  138. if e := tc15.Validate(); e == nil {
  139. t.Error("validator accepted invalid Var")
  140. }
  141. //invalid Trigger
  142. tc16 := Command{"name", Trigger{}}
  143. if e := tc16.Validate(); e == nil {
  144. t.Error("validator accepted invalid Trigger")
  145. }
  146. //Brief field present
  147. tc17 := Command{"name", MakeTestHandler(), Help{"aaaaa"}}
  148. // t.Log(spew.Sdump(tc17), tc17.Validate())
  149. if e := tc17.Validate(); e == nil {
  150. t.Error("validator accepted Command without a Brief")
  151. }
  152. // handler present
  153. tc18 := Command{"name", Brief{""}, Help{"aaaa"}}
  154. if e := tc18.Validate(); e == nil {
  155. t.Error("validator accepted Command without a handler")
  156. }
  157. //invalid typed element
  158. tc19 := Command{"name", Brief{""}, MakeTestHandler(), 1}
  159. if e := tc19.Validate(); e == nil {
  160. t.Error("validator accepted Command with a invalid typed eleement")
  161. }
  162. // no errors!
  163. tc20 := Command{"name", Brief{""}, MakeTestHandler()}
  164. if e := tc20.Validate(); e != nil {
  165. t.Error("validator rejected valid Command")
  166. }
  167. }
  168. func TestCommands(t *testing.T) {
  169. tcc1 := Commands{Command{"name", Brief{""}, MakeTestHandler(), 1}}
  170. if e := tcc1.Validate(); e == nil {
  171. t.Error("validator accepted Commands with invalid element")
  172. }
  173. tcc2 := Commands{Command{"name", Brief{""}, MakeTestHandler()}}
  174. if e := tcc2.Validate(); e != nil {
  175. t.Error("validator rejected valid Commands")
  176. }
  177. }
  178. func TestDefault(t *testing.T) {
  179. // only one item
  180. td1 := Default{
  181. "item1", "item2",
  182. }
  183. e := td1.Validate()
  184. if e == nil {
  185. t.Error("validator allowed more than one")
  186. }
  187. // no error!
  188. td2 := Default{1}
  189. e = td2.Validate()
  190. if e != nil {
  191. t.Error("validator rejected valid Default")
  192. }
  193. }
  194. func TestDefaultCommand(t *testing.T) {
  195. // only one item
  196. tdc1 := DefaultCommand{
  197. "item1", "item2",
  198. }
  199. e := tdc1.Validate()
  200. if e == nil {
  201. t.Error("validator allowed more than one")
  202. }
  203. // item is string
  204. tdc2 := DefaultCommand{
  205. 1,
  206. }
  207. e = tdc2.Validate()
  208. if e == nil {
  209. t.Error("validator permitted other than a string")
  210. }
  211. // item is a ValidName
  212. tdc3 := DefaultCommand{
  213. "abc123",
  214. }
  215. e = tdc3.Validate()
  216. if e == nil {
  217. t.Error("validator permitted an invalid name")
  218. }
  219. // item is a valid
  220. tdc4 := DefaultCommand{
  221. "abc",
  222. }
  223. e = tdc4.Validate()
  224. if e != nil {
  225. t.Error("validator rejected a valid name")
  226. }
  227. }
  228. func TestDefaultOn(t *testing.T) {
  229. // must be empty
  230. tdo1 := DefaultOn{1}
  231. e := tdo1.Validate()
  232. if e == nil {
  233. t.Error("validator permitted content in DefaultOn")
  234. }
  235. // is empty
  236. tdo2 := DefaultOn{}
  237. e = tdo2.Validate()
  238. if e != nil {
  239. t.Error("validator rejected valid empty DefaultOn")
  240. }
  241. }
  242. func TestExamples(t *testing.T) {
  243. // must not be empty
  244. te1 := Examples{}
  245. e := te1.Validate()
  246. if e == nil {
  247. t.Error("validator invalid empty Examples")
  248. }
  249. // must have pairs of elements
  250. te2 := Examples{"1", 2, 3.0}
  251. e = te2.Validate()
  252. if e == nil {
  253. t.Error("validator permitted odd number of items in Examples")
  254. }
  255. // elements must be strings
  256. te3 := Examples{"1", 2}
  257. e = te3.Validate()
  258. if e == nil {
  259. t.Error("validator permitted non-string in Examples")
  260. }
  261. // string length < 80
  262. te4 := Examples{
  263. "--max=0", "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
  264. }
  265. e = te4.Validate()
  266. if e == nil {
  267. t.Error("validator permitted over 80 characters")
  268. }
  269. // string length < 80
  270. te5 := Examples{
  271. "a1234567890123456789012345678901234567890", "123",
  272. }
  273. e = te5.Validate()
  274. if e == nil {
  275. t.Error("validator permitted over 80 characters")
  276. }
  277. // even numbered (first in pair) elements have no control characters
  278. te6 := Examples{
  279. "--max=0", "aaa\n",
  280. }
  281. e = te6.Validate()
  282. if e == nil {
  283. t.Error("validator permitted control character in explainer")
  284. }
  285. // no error
  286. te7 := Examples{
  287. "--max=0", "aaaaaaaa",
  288. }
  289. e = te7.Validate()
  290. if e != nil {
  291. t.Error("validator rejected valid example")
  292. }
  293. }
  294. func TestGroup(t *testing.T) {
  295. // contains only one element
  296. tg1 := Group{1, 1}
  297. tg2 := Group{}
  298. e := tg1.Validate()
  299. if e == nil {
  300. t.Error("validator accepted more than one")
  301. }
  302. e = tg2.Validate()
  303. if e == nil {
  304. t.Error("validator accepted no elements")
  305. }
  306. // element is a string
  307. tg3 := Group{1}
  308. e = tg3.Validate()
  309. if e == nil {
  310. t.Error("validator accepted non string elemeent")
  311. }
  312. // string is a ValidName
  313. tg4 := Group{"abc123"}
  314. e = tg4.Validate()
  315. if e == nil {
  316. t.Error("validator accepted invalid name")
  317. }
  318. // no error!
  319. tg5 := Group{"abc"}
  320. e = tg5.Validate()
  321. if e != nil {
  322. t.Error("validator rejected valid name")
  323. }
  324. }
  325. func TestHelp(t *testing.T) {
  326. // contains only one element
  327. th1 := Help{1, 1}
  328. e := th1.Validate()
  329. if e == nil {
  330. t.Error("validator accepted more than one")
  331. }
  332. th2 := Help{}
  333. e = th2.Validate()
  334. if e == nil {
  335. t.Error("validator accepted no elements")
  336. }
  337. // element is a string
  338. th3 := Help{1.0}
  339. e = th3.Validate()
  340. if e == nil {
  341. t.Error("validator accepted non-string")
  342. }
  343. // no error!
  344. th4 := Help{"this is a test with cr\nand other things"}
  345. e = th4.Validate()
  346. if e != nil {
  347. t.Error("validator rejected valid element")
  348. }
  349. }
  350. func TestRunAfter(t *testing.T) {
  351. // may not contain anything
  352. tra1 := RunAfter{
  353. "",
  354. }
  355. e := tra1.Validate()
  356. if e == nil {
  357. t.Error("validator accepted content in RunAfter")
  358. }
  359. // no error
  360. tra2 := RunAfter{}
  361. e = tra2.Validate()
  362. if e != nil {
  363. t.Error("validator rejected valid RunAfter")
  364. }
  365. }
  366. func TestShort(t *testing.T) {
  367. // contains only one element
  368. ts1 := Short{'1', 2}
  369. e := ts1.Validate()
  370. if e == nil {
  371. t.Error("validator accepted more than one item")
  372. }
  373. // element is a rune (single character/unicode point)
  374. ts2 := Short{1}
  375. e = ts2.Validate()
  376. if e == nil {
  377. t.Error("validator accepted a non-rune element")
  378. }
  379. // element is a letter or number
  380. ts3 := Short{'!'}
  381. e = ts3.Validate()
  382. if e == nil {
  383. t.Error("validator accepted non alphanumeric element")
  384. }
  385. // no error!
  386. ts4 := Short{'a'}
  387. e = ts4.Validate()
  388. if e != nil {
  389. t.Error("validator rejected a valid short element")
  390. }
  391. }
  392. func TestSlot(t *testing.T) {
  393. // slots are all the same type (pointer to said type)
  394. a := 1
  395. b := "string"
  396. ts1 := Slot{&a, &b}
  397. e := ts1.Validate()
  398. if e == nil {
  399. t.Error("validator accepted heteregenous types")
  400. }
  401. // slots are all pointers
  402. c := 2
  403. ts2 := Slot{a, c}
  404. e = ts2.Validate()
  405. if e == nil {
  406. t.Error("validator accepted heteregenous types")
  407. }
  408. // no error!
  409. ts3 := Slot{&a, &c}
  410. e = ts3.Validate()
  411. if e != nil {
  412. t.Error("validator rejected valid contents")
  413. }
  414. }
  415. func TestTerminates(t *testing.T) {
  416. // must be empty
  417. tt1 := Terminates{1}
  418. e := tt1.Validate()
  419. if e == nil {
  420. t.Error("validator permitted content")
  421. }
  422. // is empty
  423. tt2 := Terminates{}
  424. e = tt2.Validate()
  425. if e != nil {
  426. t.Error("validator rejected valid empty Terminates")
  427. }
  428. }
  429. func TestTri(t *testing.T) {
  430. // contains at least 3 elements
  431. ttr1 := Tri{1, 1}
  432. if e := ttr1.Validate(); e == nil {
  433. t.Error("Trigger must contain at least 3 elements")
  434. }
  435. // first element is a string
  436. ttr2 := Tri{1, 1, 1}
  437. if e := ttr2.Validate(); e == nil {
  438. t.Error("first element must be a string")
  439. }
  440. // string is a ValidName
  441. ttr3 := Tri{"a ", 1, 1}
  442. if e := ttr3.Validate(); e == nil {
  443. t.Error("validator accepted invalid name")
  444. }
  445. // contains (only) one Brief
  446. ttr4 := Tri{"aaaa", Brief{""}, Brief{""}}
  447. if e := ttr4.Validate(); e == nil {
  448. t.Error("validator accepted more than one Brief")
  449. }
  450. // contains (only) one Version
  451. ttr5 := Tri{"aaaa", Version{1, 1, 1}, Version{1, 1, 1}, Brief{"aaaa"}}
  452. if e := ttr5.Validate(); e == nil {
  453. t.Error("validator accepted more than one Version")
  454. }
  455. // contains (only) one Commands
  456. ttr6 := Tri{"aaaa", Commands{}, Commands{}, Brief{"aaaa"}}
  457. if e := ttr6.Validate(); e == nil {
  458. t.Error("validator accepted more than one Commands")
  459. }
  460. // contains no more than one DefaultCommand
  461. ttr7 := Tri{"aaaa", DefaultCommand{"commname"}, DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"commname"}}}
  462. if e := ttr7.Validate(); e == nil {
  463. t.Error("validator accepted more than one DefaultCommand")
  464. }
  465. // DefaultCommand with no Commands array
  466. ttr8 := Tri{"aaaa", DefaultCommand{"aaaa"}, Brief{"aaaa"}}
  467. if e := ttr8.Validate(); e == nil {
  468. t.Error("validator accepted DefaultCommand with no Commands present")
  469. }
  470. // DefaultCommand's name appears in also present Commands array
  471. ttr9 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"aaaa"}, Commands{Command{"NOTcommname"}}}
  472. if e := ttr9.Validate(); e == nil {
  473. t.Error("validator accepted more than one DefaultCommand")
  474. }
  475. // contains invalid Var
  476. ttr10 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Var{}}
  477. if e := ttr10.Validate(); e == nil {
  478. t.Error("validator accepted invalid Var")
  479. }
  480. // contains invalid Trigger
  481. ttr11 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Trigger{}}
  482. if e := ttr11.Validate(); e == nil {
  483. t.Error("validator accepted invalid Trigger")
  484. }
  485. // contains invalid DefaultCommand
  486. ttr12 := Tri{"aaaa", Version{1, 1, 1}, DefaultCommand{}, Brief{"aaaa"}, Commands{Command{"commname"}}}
  487. if e := ttr12.Validate(); e == nil {
  488. t.Error("validator accepted invalid DefaultCommand")
  489. }
  490. // contains invalid Command in Commands
  491. ttr13 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, Commands{Command{}}}
  492. if e := ttr13.Validate(); e == nil {
  493. t.Error("validator accepted invalid Command element in Commands")
  494. }
  495. // only contains element from set of possible elements
  496. ttr14 := Tri{"aaaa", Version{1, 1, 1}, Brief{"aaaa"}, 1}
  497. if e := ttr14.Validate(); e == nil {
  498. t.Error("validator accepted invalid element in Tri")
  499. }
  500. // contains invalid Brief
  501. ttr15 := Tri{"aaaa", Version{1, 1, 1}, Brief{1}}
  502. if e := ttr15.Validate(); e == nil {
  503. t.Error("validator accepted invalid Brief")
  504. }
  505. // contains invalid Version
  506. ttr16 := Tri{"aaaa", Version{1, 1, 1, 1}, Brief{"valid brief"}}
  507. if e := ttr16.Validate(); e == nil {
  508. t.Error("validator accepted invalid Version")
  509. }
  510. // Brief is missing
  511. ttr17 := Tri{"aaaa", DefaultCommand{"commname"}, Version{1, 1, 1}, Commands{Command{"commname", Brief{"valid brief"}, MakeTestHandler()}}}
  512. if e := ttr17.Validate(); e == nil {
  513. t.Error("validator accepted missing Brief")
  514. }
  515. // Version is missing
  516. ttr18 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
  517. Commands{
  518. Command{
  519. "commname",
  520. Brief{"valid brief"},
  521. MakeTestHandler(),
  522. },
  523. },
  524. }
  525. if e := ttr18.Validate(); e == nil {
  526. t.Error("validator accepted missing Version")
  527. }
  528. // no error!
  529. ttr21 := Tri{"aaaa", DefaultCommand{"commname"}, Brief{"valid brief"},
  530. Commands{
  531. Command{
  532. "commname",
  533. Brief{"valid brief"},
  534. MakeTestHandler(),
  535. },
  536. },
  537. Version{1, 1, 1},
  538. }
  539. if e := ttr21.Validate(); e != nil {
  540. t.Error("validator rejected valid Tri")
  541. }
  542. }
  543. func TestTrigger(t *testing.T) {
  544. // contains at least 3 elements
  545. tt1 := Trigger{1, 1}
  546. if e := tt1.Validate(); e == nil {
  547. t.Error("Trigger must contain at least 3 elements")
  548. }
  549. // first is string
  550. tt2 := Trigger{1, 1, 1}
  551. if e := tt2.Validate(); e == nil {
  552. t.Error("first element must be a string")
  553. }
  554. // name is ValidName
  555. tt3 := Trigger{"a ", 1, 1}
  556. if e := tt3.Validate(); e == nil {
  557. t.Error("validator accepted invalid name")
  558. }
  559. // has only one Brief
  560. tt4 := Trigger{"aaaa", Brief{""}, Brief{""}}
  561. if e := tt4.Validate(); e == nil {
  562. t.Error("validator accepted more than one Brief")
  563. }
  564. // has only one Short
  565. tt5 := Trigger{"aaaa", Short{'a'}, Short{'a'}}
  566. if e := tt5.Validate(); e == nil {
  567. t.Error("validator allowed more than one Short")
  568. }
  569. // has only one Usage
  570. tt6 := Trigger{"aaaa", Usage{""}, Usage{""}}
  571. if e := tt6.Validate(); e == nil {
  572. t.Error("validator allowed more than one Usage")
  573. }
  574. // has only one Help
  575. tt7 := Trigger{"aaaa", Help{""}, Help{""}}
  576. if e := tt7.Validate(); e == nil {
  577. t.Error("validator allowed more than one Help")
  578. }
  579. // has only one handler
  580. tt8 := Trigger{"name", Brief{""}, MakeTestHandler(), MakeTestHandler()}
  581. if e := tt8.Validate(); e == nil {
  582. t.Error("validator accepted more than one MakeTestHandler()")
  583. }
  584. // has only one DefaultOn
  585. tt9 := Trigger{"aaaa", DefaultOn{}, DefaultOn{}}
  586. if e := tt9.Validate(); e == nil {
  587. t.Error("validator allowed more than one DefaultOn")
  588. }
  589. // has only one RunAfter
  590. tt10 := Trigger{"aaaa", RunAfter{}, RunAfter{}}
  591. if e := tt10.Validate(); e == nil {
  592. t.Error("validator allowed more than one RunAfter")
  593. }
  594. // has only one Terminates
  595. tt11 := Trigger{"aaaa", Terminates{}, Terminates{}}
  596. if e := tt11.Validate(); e == nil {
  597. t.Error("validator allowed more than one Terminates")
  598. }
  599. // has invalid Brief
  600. tt12 := Trigger{"aaaa", Short{'a'}, Brief{1}}
  601. if e := tt12.Validate(); e == nil {
  602. t.Error("validator allowed invalid Brief")
  603. }
  604. // has invalid Short
  605. tt13 := Trigger{"aaaa", Brief{"aaaa"}, Short{1}}
  606. if e := tt13.Validate(); e == nil {
  607. t.Error("validator allowed invalid Short")
  608. }
  609. // has invalid Usage
  610. tt14 := Trigger{"aaaa", Brief{"aaaa"}, Usage{1}}
  611. if e := tt14.Validate(); e == nil {
  612. t.Error("validator allowed invalid Usage")
  613. }
  614. // has invalid Help
  615. tt15 := Trigger{"aaaa", Brief{"aaaa"}, Help{1}}
  616. if e := tt15.Validate(); e == nil {
  617. t.Error("validator allowed invalid Help")
  618. }
  619. // has invalid MakeTestHandler()
  620. handle := MakeTestHandler()
  621. _ = handle
  622. handle = nil
  623. tt16 := Trigger{"aaaa", Brief{"aaaa"}, handle}
  624. if e := tt16.Validate(); e == nil {
  625. t.Error("validator allowed invalid MakeTestHandler()")
  626. }
  627. // has invalid DefaultOn
  628. tt17 := Trigger{"aaaa", Brief{"aaaa"}, DefaultOn{1}}
  629. if e := tt17.Validate(); e == nil {
  630. t.Error("validator allowed invalid DefaultOn")
  631. }
  632. // has invalid RunAfter
  633. tt18 := Trigger{"aaaa", Brief{"aaaa"}, RunAfter{1}}
  634. if e := tt18.Validate(); e == nil {
  635. t.Error("validator allowed invalid RunAfter")
  636. }
  637. // has invalid Terminates
  638. tt19 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{1}}
  639. if e := tt19.Validate(); e == nil {
  640. t.Error("validator allowed invalid Terminates")
  641. }
  642. // has one each of Brief and handler
  643. tt20 := Trigger{"aaaa", Brief{"aaaa"}, Terminates{}}
  644. if e := tt20.Validate(); e == nil {
  645. t.Error("validator allowed Trigger without handler")
  646. }
  647. tt21 := Trigger{"aaaa", MakeTestHandler(), Terminates{}}
  648. if e := tt21.Validate(); e == nil {
  649. t.Error("validator allowed Trigger without Brief")
  650. }
  651. // has no other type than those foregoing
  652. tt22 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), 3}
  653. if e := tt22.Validate(); e == nil {
  654. t.Error("validator allowed invalid element")
  655. }
  656. // has only one Group
  657. tt23 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{"aaaa"}, Group{"aaaa"}}
  658. if e := tt23.Validate(); e == nil {
  659. t.Error("validator allowed more than one DefaultOn")
  660. }
  661. // has invalid Group
  662. tt24 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Group{1}}
  663. if e := tt24.Validate(); e == nil {
  664. t.Error("validator allowed invalid DefaultOn")
  665. }
  666. // no error!
  667. tt25 := Trigger{"aaaa", Brief{"aaaa"}, MakeTestHandler(), Terminates{}}
  668. if e := tt25.Validate(); e != nil {
  669. t.Error("validator rejected valid Trigger")
  670. }
  671. }
  672. func TestUsage(t *testing.T) {
  673. // only one element
  674. tu1 := Usage{}
  675. tu2 := Usage{1, 2}
  676. e := tu1.Validate()
  677. if e == nil {
  678. t.Error("validator permitted empty Usage")
  679. }
  680. e = tu2.Validate()
  681. if e == nil {
  682. t.Error("validator permitted more than one element in Usage")
  683. }
  684. // element is string
  685. tu3 := Usage{0.1}
  686. e = tu3.Validate()
  687. if e == nil {
  688. t.Error("validator permitted element that is not a string")
  689. }
  690. // string is no more than 80 chars long
  691. tu4 := Usage{
  692. "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
  693. }
  694. e = tu4.Validate()
  695. if e == nil {
  696. t.Error("validator permitted over 80 characters")
  697. }
  698. // string contains no control characters
  699. tu5 := Usage{
  700. "aaa\n",
  701. }
  702. e = tu5.Validate()
  703. if e == nil {
  704. t.Error("validator permitted control character in explainer")
  705. }
  706. // no error!
  707. tu6 := Usage{
  708. "aaaaaaa",
  709. }
  710. e = tu6.Validate()
  711. if e != nil {
  712. t.Error("validator rejected valid input")
  713. }
  714. }
  715. func TestValidName(t *testing.T) {
  716. // name is 3 or more characters long
  717. tvn1 := "ab"
  718. e := ValidName(tvn1)
  719. if e == nil {
  720. t.Error("validator accepted string under 3 characters length")
  721. }
  722. // name is only composed of letters
  723. tvn2 := "ab3"
  724. e = ValidName(tvn2)
  725. if e == nil {
  726. t.Error("validator accepted non-letter characters")
  727. }
  728. // no error!
  729. tvn3 := "proper"
  730. e = ValidName(tvn3)
  731. if e != nil {
  732. t.Error("validator rejected valid namme")
  733. }
  734. }
  735. func TestVar(t *testing.T) {
  736. // contains at least 3 elements
  737. tv1 := Var{1, 1}
  738. if e := tv1.Validate(); e == nil {
  739. t.Error("Var must contain at least 3 elements")
  740. }
  741. // first is string
  742. tv2 := Var{1, 1, 1}
  743. if e := tv2.Validate(); e == nil {
  744. t.Error("first element must be a string")
  745. }
  746. // name is ValidName
  747. tv3 := Var{"a ", 1, 1}
  748. if e := tv3.Validate(); e == nil {
  749. t.Error("validator accepted invalid name")
  750. }
  751. // has only one Brief
  752. tv4 := Var{"aaaa", Brief{""}, Brief{""}}
  753. if e := tv4.Validate(); e == nil {
  754. t.Error("validator accepted more than one Brief")
  755. }
  756. // has only one Short
  757. tv5 := Var{"aaaa", Short{'a'}, Short{'a'}}
  758. if e := tv5.Validate(); e == nil {
  759. t.Error("validator allowed more than one Short")
  760. }
  761. // has only one Usage
  762. tv6 := Var{"aaaa", Usage{""}, Usage{""}}
  763. if e := tv6.Validate(); e == nil {
  764. t.Error("validator allowed more than one Usage")
  765. }
  766. // has only one Help
  767. tv7 := Var{"aaaa", Help{""}, Help{""}}
  768. if e := tv7.Validate(); e == nil {
  769. t.Error("validator allowed more than one Help")
  770. }
  771. // has only one Default
  772. tv8 := Var{"aaaa", Default{"aaa"}, Default{"aaa"}}
  773. if e := tv8.Validate(); e == nil {
  774. t.Error("validator allowed more than one Default")
  775. }
  776. // has only one Slot
  777. tstring := "valid string"
  778. tv9 := Var{"aaaa", Slot{&tstring}, Slot{&tstring}}
  779. if e := tv9.Validate(); e == nil {
  780. t.Error("validator allowed more than one Slot")
  781. }
  782. // has invalid Brief
  783. tv10 := Var{"aaaa", Brief{}, Short{""}}
  784. if e := tv10.Validate(); e == nil {
  785. t.Error("validator accepted invalid Brief")
  786. }
  787. // has invalid Short
  788. tv11 := Var{"aaaa", Brief{"aaaa"}, Short{1}}
  789. if e := tv11.Validate(); e == nil {
  790. t.Error("validator allowed invalid Short")
  791. }
  792. // has invalid Usage
  793. tv12 := Var{"aaaa", Brief{"aaaa"}, Usage{0.1}}
  794. if e := tv12.Validate(); e == nil {
  795. t.Error("validator allowed invalid Usage")
  796. }
  797. // has invalid Help
  798. tv13 := Var{"aaaa", Brief{""}, Help{"aaa", 1}}
  799. if e := tv13.Validate(); e == nil {
  800. t.Error("validator allowed invalid Help")
  801. }
  802. // has invalid Default
  803. tv14 := Var{"aaaa", Brief{"aaa"}, Default{1, 3}}
  804. if e := tv14.Validate(); e == nil {
  805. t.Error("validator allowed invalid Default")
  806. }
  807. // has invalid Slot
  808. tv15 := Var{"aaaa", Brief{tstring}, Slot{tstring}}
  809. if e := tv15.Validate(); e == nil {
  810. t.Error("validator allowed invalid Slot")
  811. }
  812. // has one each of Brief and Slot
  813. tv16 := Var{"aaaa", Brief{"aaa"}, Default{"aa"}}
  814. if e := tv16.Validate(); e == nil {
  815. t.Error("validator allowed absence of Brief or Slot")
  816. }
  817. // has no other type than those foregoing
  818. tv17 := Var{"aaaa", Brief{tstring}, Slot{&tstring}, 1, MakeTestHandler()}
  819. if e := tv17.Validate(); e == nil {
  820. t.Error("validator rejected valid Var")
  821. }
  822. // has only one Group
  823. tv18 := Var{"aaaa", Brief{tstring}, Slot{&tstring},
  824. Group{"aaaa"}, Group{"aaaa"},
  825. }
  826. if e := tv18.Validate(); e == nil {
  827. t.Error("validator accepted more than one Group")
  828. }
  829. // has invalid Group
  830. tv19 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Group{""}}
  831. if e := tv19.Validate(); e == nil {
  832. t.Error("validator allowed invalid Group")
  833. }
  834. // Default value is assignable to dereferenced Slot pointer
  835. tv20 := Var{"aaaa", Brief{"aaaa"}, Slot{&tstring}, Default{5}}
  836. if e := tv20.Validate(); e == nil {
  837. t.Error("validator allowed default that can't be assigned to Slot")
  838. }
  839. var tint int
  840. tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tint}, Default{" "}}
  841. if e := tv20.Validate(); e == nil {
  842. t.Error("validator allowed default that can't be assigned to Slot")
  843. }
  844. var tuint32 uint32
  845. tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tuint32}, Default{0.1}}
  846. if e := tv20.Validate(); e == nil {
  847. t.Error("validator allowed default that can't be assigned to Slot")
  848. }
  849. var tfloat64 float64
  850. tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tfloat64}, Default{5}}
  851. if e := tv20.Validate(); e == nil {
  852. t.Error("validator allowed default that can't be assigned to Slot")
  853. }
  854. var tstringslice []string
  855. tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tstringslice}, Default{"aaa"}}
  856. if e := tv20.Validate(); e == nil {
  857. t.Error("validator allowed default that can't be assigned to Slot")
  858. }
  859. var tduration time.Duration
  860. tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tduration}, Default{5}}
  861. if e := tv20.Validate(); e == nil {
  862. t.Error("validator allowed default that can't be assigned to Slot")
  863. }
  864. // no error!}
  865. tv21 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
  866. if e := tv21.Validate(); e != nil {
  867. t.Error("validator rejected valid Var")
  868. }
  869. }
  870. func TestVersion(t *testing.T) {
  871. // has no more than 4 fields
  872. tv1 := Version{1, 2, 3, 4, 5}
  873. e := tv1.Validate()
  874. if e == nil {
  875. t.Error("validator accepted more than three items")
  876. }
  877. // has at least 3 fields
  878. tv2 := Version{4, 5}
  879. e = tv2.Validate()
  880. if e == nil {
  881. t.Error("validator accepted less than three items")
  882. }
  883. // first three are integers
  884. tv3 := Version{1.1, 2, 3, 4}
  885. e = tv3.Validate()
  886. if e == nil {
  887. t.Error("validator accepted non-integer version numbers")
  888. }
  889. // integers are under 100
  890. tv4 := Version{100, 2, 3, 4}
  891. e = tv4.Validate()
  892. if e == nil {
  893. t.Error("validator accepted a version number over 100")
  894. }
  895. // 4th field is a string
  896. tv5 := Version{10, 2, 3, 4}
  897. e = tv5.Validate()
  898. if e == nil {
  899. t.Error("validator accepted a 4th field that is not a string")
  900. }
  901. // string contains only letters and numbers
  902. tv6 := Version{10, 2, 3, "alpha3! "}
  903. e = tv6.Validate()
  904. if e == nil {
  905. t.Error(
  906. "validator accepted a 4th field that contains other than letters and numbers")
  907. }
  908. // no error!
  909. tv7 := Version{10, 2, 3, "alpha3"}
  910. e = tv7.Validate()
  911. if e != nil {
  912. t.Error(
  913. "validator accepted a 4th field that contains other than letters and numbers")
  914. }
  915. }