xref: /freebsd/usr.bin/asa/tests/asa_test.sh (revision b1879975794772ee51f0b4865753364c7d7626c3)
1#
2# Copyright (c) 2023 Klara, Inc.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7a="The magic words are"
8b="Squeamish Ossifrage"
9
10atf_check_asa() {
11	atf_check -o file:"$2" asa "$1"
12	atf_check -o file:"$2" asa <"$1"
13	atf_check -o file:"$2" asa - <"$1"
14}
15
16atf_test_case space
17space_head() {
18	atf_set descr "First character on line is ' '"
19}
20space_body() {
21	printf " %s\n %s\n" "$a" "$b" >infile
22	printf "%s\n%s\n" "$a" "$b" >outfile
23	atf_check_asa infile outfile
24}
25
26atf_test_case zero
27zero_head() {
28	atf_set descr "First character on line is '0'"
29}
30zero_body() {
31	printf " %s\n0%s\n" "$a" "$b" >infile
32	printf "%s\n\n%s\n" "$a" "$b" >outfile
33	atf_check_asa infile outfile
34}
35
36atf_test_case one
37one_head() {
38	atf_set descr "First character on line is '1'"
39}
40one_body() {
41	printf "1%s\n1%s\n" "$a" "$b" >infile
42	printf "\f%s\n\f%s\n" "$a" "$b" >outfile
43	atf_check_asa infile outfile
44}
45
46atf_test_case plus
47plus_head() {
48	atf_set descr "First character on line is '+'"
49}
50plus_body() {
51	printf " %s\n+%s\n" "$a" "$b" >infile
52	printf "%s\r%s\n" "$a" "$b" >outfile
53	atf_check_asa infile outfile
54}
55
56atf_test_case plus_top
57plus_top_head() {
58	atf_set descr "First character in input is '+'"
59}
60plus_top_body() {
61	printf "+%s\n+%s\n" "$a" "$b" >infile
62	printf "%s\r%s\n" "$a" "$b" >outfile
63	atf_check_asa infile outfile
64}
65
66atf_test_case stdout
67stdout_head() {
68	atf_set descr "Failure to write to stdout"
69}
70stdout_body() {
71	(
72		trap "" PIPE
73		sleep 1
74		echo " $a $b" | asa 2>stderr
75		echo $? >result
76	) | true
77	atf_check -o inline:"1\n" cat result
78	atf_check -o match:"stdout" cat stderr
79}
80
81atf_test_case dashdash
82dashdash_head() {
83	atf_set descr "Use -- to end options"
84}
85dashdash_body() {
86	echo " $a $b" >-infile
87	atf_check -s not-exit:0 -e match:"illegal option" asa -infile
88	atf_check -o inline:"$a $b\n" asa -- -infile
89}
90
91atf_test_case unterminated
92unterminated_head() {
93	atf_set descr "Unterminated input"
94}
95unterminated_body() {
96	printf " %s\n %s" "$a" "$b" >infile
97	printf "%s\n%s" "$a" "$b" >outfile
98	atf_check_asa infile outfile
99}
100
101atf_init_test_cases()
102{
103	atf_add_test_case space
104	atf_add_test_case zero
105	atf_add_test_case one
106	atf_add_test_case plus
107	atf_add_test_case plus_top
108	atf_add_test_case stdout
109	atf_add_test_case dashdash
110	atf_add_test_case unterminated
111}
112