File tree Expand file tree Collapse file tree 2 files changed +81
-0
lines changed
Expand file tree Collapse file tree 2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change @@ -82,6 +82,23 @@ export __GO_STRINGS_VALIDATION_SKIP_CALLERS=2
8282 printf -v " $2 " -- ' %s' " ${*: 3} "
8383}
8484
85+ # Trims the leading and trailing whitespace from a string
86+ #
87+ # Arguments:
88+ # var_name: Name of the caller's variable containing the string to trim
89+ @go.trim () {
90+ @go.validate_identifier_or_die ' Input/output variable name' " $1 "
91+ local __go_trim_input__=" ${! 1} "
92+
93+ if [[ " $__go_trim_input__ " =~ ^[[:space:]]+ ]]; then
94+ __go_trim_input__=" ${__go_trim_input__# ${BASH_REMATCH[0]} } "
95+ fi
96+ if [[ " $__go_trim_input__ " =~ [[:space:]]+$ ]]; then
97+ __go_trim_input__=" ${__go_trim_input__% ${BASH_REMATCH[0]} } "
98+ fi
99+ printf -v " $1 " -- ' %s' " $__go_trim_input__ "
100+ }
101+
85102# Determines the common prefix for a set of strings
86103#
87104# Will return the empty string for a single argument. This facilitates prefix
Original file line number Diff line number Diff line change 1+ #! /usr/bin/env bats
2+
3+ load ../environment
4+ load helpers
5+
6+ setup () {
7+ test_filter
8+ }
9+
10+ teardown () {
11+ @go.remove_test_go_rootdir
12+ }
13+
14+ @test " $SUITE : error if result variable name not a valid identifier" {
15+ create_strings_test_script ' @go.trim "invalid;"'
16+ run " $TEST_GO_SCRIPT "
17+ assert_failure
18+
19+ local err_msg=' ^Input/output variable name "invalid;" for @go.trim '
20+ err_msg+=' contains invalid identifier characters at:$'
21+
22+ assert_lines_match " $err_msg " \
23+ " ^ $TEST_GO_SCRIPT :[0-9] main$"
24+ }
25+
26+ @test " $SUITE : empty string" {
27+ create_strings_test_script ' declare result' \
28+ ' @go.trim "result"' \
29+ ' echo "$result"'
30+ run " $TEST_GO_SCRIPT "
31+ assert_success ' '
32+ }
33+
34+ @test " $SUITE : no leading or trailing space" {
35+ create_strings_test_script ' declare result="foo bar"' \
36+ ' @go.trim "result"' \
37+ ' echo "$result"'
38+ run " $TEST_GO_SCRIPT "
39+ assert_success ' foo bar'
40+ }
41+
42+ @test " $SUITE : trim leading space" {
43+ create_strings_test_script ' declare result=" foo bar"' \
44+ ' @go.trim "result"' \
45+ ' echo "$result"'
46+ run " $TEST_GO_SCRIPT "
47+ assert_success ' foo bar'
48+ }
49+
50+ @test " $SUITE : trim trailing space" {
51+ create_strings_test_script ' declare result="foo bar "' \
52+ ' @go.trim "result"' \
53+ ' echo "$result"'
54+ run " $TEST_GO_SCRIPT "
55+ assert_success ' foo bar'
56+ }
57+
58+ @test " $SUITE : trim leading and trailing space" {
59+ create_strings_test_script ' declare result=" foo bar "' \
60+ ' @go.trim "result"' \
61+ ' echo "$result"'
62+ run " $TEST_GO_SCRIPT "
63+ assert_success ' foo bar'
64+ }
You can’t perform that action at this time.
0 commit comments