summaryrefslogtreecommitdiff
blob: 78e08bf7b858aa0372b477da0118160c3e1d6e10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bash

# shellcheck disable=SC2034
STARLARK_RUST_EXPECTED_functions=(
	any
	dir
	fail
	getattr
	hasattr
	hash
	len
	max
	min
	print
	range
	repr
	reversed
	sorted
	type
	zip
)

# shellcheck disable=SC2034
STARLARK_RUST_EXPECTED_types=(
	bool
	int
	list
	str
	tuple
	dict
)

# shellcheck disable=SC2034
STARLARK_RUST_EXPECTED_dict_methods=(
	clear
	get
	items
	keys
	pop
	popitem
	setdefault
	update
	values
)

# shellcheck disable=SC2034
STARLARK_RUST_EXPECTED_list_methods=(
	append
	clear
	extend
	index
	insert
	pop
	remove
)

# shellcheck disable=SC2034
STARLARK_RUST_EXPECTED_str_methods=(
	capitalize
	count
	elems
	endswith
	find
	format
	index
	isalnum
	isalpha
	isdigit
	islower
	isspace
	istitle
	isupper
	join
	lower
	lstrip
	partition
	replace
	rfind
	rindex
	rpartition
	rsplit
	rstrip
	split
	splitlines
	startswith
	strip
	title
	upper
)

_test-features_execute-test() {
	local error_msg test_title=$1 test_stdin=$2 exp_stdout=$3 exp_stderr=$4 exp_exitcode=$5
	ebegin "$test_title"
	error_msg=$(
		# Redirect stderr to stdin because print goes to stderr since this commit:
		# https://github.com/facebookexperimental/starlark-rust/commit/cdd68fa752aa8b6cae602297de1e43658b0a63fd
		test_stdout=$("$starlark_binary" -i --json <<< "$test_stdin" 2>&1)
		test_exitcode=$?

		if (( test_exitcode != exp_exitcode )); then
			echo "unexpected exit code \"$test_exitcode\", expected exit code \"$exp_exitcode\" for test_stdin: $test_stdin"
			exit 1
		elif [[ "$test_stdout" != "$exp_stdout" ]]; then
			echo "unexpected stdout \"$test_stdout\", expected stdout \"$exp_stdout\" for test_stdin: $test_stdin"
			exit 1
		fi
		exit 0
	)
	eend $? "$error_msg"

	# shellcheck disable=SC2015
	[[ $error_msg ]] && failures+=("$error_msg") || (( successes += 1 ))
}

test-features_main() {
	local starlark_binary=$1
	local failures=() successes=0
	local banner_width=45

	local attr attr_type test_case
	for attr_type in function type; do
		printf -- '\n\n' >&2
		printf -- '%s\n' "Checking for existence of expected ${attr_type}s" >&2
		eval "printf -- '=%.0s' {1..${banner_width}}" >&2
		echo >&2
		while read -r attr; do
			test_case=(
				"$attr"
				"$attr and print('$attr exists')"
				"$attr exists"
				""
				0
			)
			_test-features_execute-test "${test_case[@]}"
		done < <(eval "printf -- '%s\n' \"\${STARLARK_RUST_EXPECTED_${attr_type}s[@]}\"")
	done

	local attr attr_type test_case type_literal
	for attr_type in dict list str; do
		printf -- '\n\n' >&2
		printf -- '%s\n' "Checking ${attr_type} built-in methods" >&2
		eval "printf -- '=%.0s' {1..${banner_width}}" >&2
		echo >&2

		case $attr_type in
			str)
				type_literal='""'
				;;
			*)
				type_literal="$attr_type()"
				;;
		esac

		while read -r attr; do
			test_case=(
				"$attr_type.$attr"
				"hasattr($type_literal, \"$attr\") and print('$attr method exists')"
				"$attr method exists"
				""
				0
			)
			_test-features_execute-test "${test_case[@]}"
		done < <(eval "printf -- '%s\n' \"\${STARLARK_RUST_EXPECTED_${attr_type}_methods[@]}\"")
	done

	printf -- '\n\n' >&2
	printf -- '%s\n' "Checking for miscellaneous starlark features" >&2
	eval "printf -- '=%.0s' {1..${banner_width}}" >&2
	printf -- '\n\n' >&2

	test_case=(
		'list comprehension'
		'[print("output from list comprehension") for i in range(0, 1) if (i == 0 and True) or not False]'
		'output from list comprehension'
		""
		0
	)
	_test-features_execute-test "${test_case[@]}"
	printf -- '\n\n' >&2

	if (( ${#failures[@]} > 0 )); then
		echo "${#failures[@]} test (s) failed" >&2
		return 1
	elif (( successes == 0 )); then
		echo "no tests ran" >&2
		return 1
	fi
}