Skip to content

Commit

Permalink
Add tests/cli_test.sh for unit test cli command.
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee committed Apr 5, 2024
1 parent c47a49a commit 099f89f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ jobs:
key: ${{ matrix.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Build
run: cargo build
- name: Tests
run: cargo test
- name: Test Cli
- name: Test
run: make test
lint:
name: Lint
Expand Down
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
test:
cargo test
cargo run . tests/fixtures/pest.expected.pest
cat tests/fixtures/json.actual.pest | cargo run . --stdin
sh ./tests/cli_test.sh
update_grammar:
curl -sSL https://github.com/pest-parser/pest/raw/master/meta/src/grammar.pest > src/grammar.pest
patch src/grammar.pest src/grammar.patch
55 changes: 55 additions & 0 deletions tests/cli_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
echo 'Running tests for `pestfmt --stdin`...'

RED='\033[0;31m'
GREEN='\033[0;32m'
NC="\033[0m"

trim() {
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}

assert_format_stdin() {
local input=$1
local expected=$2

# If input is a file, read it
if [ -f "$input" ]; then
input=$(cat $input)
fi
if [ -f "$expected" ]; then
expected=$(cat $expected)
fi

# Perform pestfmt --stdin test
local acctual=$(cargo run . --stdin <<< "$input" | trim)
if [ "$acctual" != "$expected" ]; then
echo ""
echo "Test failed"
echo "---------------------------------------------------"
echo "${RED}Expected:${NC}\n\n$expected\n"
echo "${GREEN}Acctual:${NC}\n\n$acctual\n"
echo "---------------------------------------------------"
exit 1
fi
}

assert_format() {
local input=$1
local expected=$2
local acctual=$(cargo run . $input | trim)
if [ "$acctual" != "$expected" ]; then
echo ""
echo "Test failed"
echo "---------------------------------------------------"
echo "${RED}Expected:${NC}\n\n$expected\n"
echo "${GREEN}Acctual:${NC}\n\n$acctual\n"
echo "---------------------------------------------------"
exit 1
fi
}

assert_format_stdin 'item={"a"}' 'item = { "a2" }'
assert_format_stdin "tests/fixtures/json.actual.pest" "tests/fixtures/json.expected.pest"
assert_format "tests/fixtures/pest.expected.pest" "Formatted 0 files"

echo 'All `--stdin` tests passed.'

0 comments on commit 099f89f

Please sign in to comment.