#!/bin/bash
cd $(dirname "$0")

source util.sh
PATH=".." source irc.sh

# Because the presence or absence of newlines is important to the operation of
# this function, and because $() strips trailing newlines, we'll encode things
# with "hexdump -c" before comparing them.

# ^P0 should be unquoted to \0 but there's no way to store that in an
# environment variable, so let's not even bother testing.

# Items without any quoted characters ar left unmolested.
output=$(printf "hello" | irc_enquote | hexdump -c)
assert_equal "$output" "$(printf "hello" | hexdump -c)"

# \n is quoted to ^Pn
output=$(printf $'hello\nworld' | irc_enquote | hexdump -c)
assert_equal "$output" "$(printf $'hello\020nworld' | hexdump -c)"

# \r is quoted to ^Pr
output=$(printf $'hello\rworld' | irc_enquote | hexdump -c)
assert_equal "$output" "$(printf $'hello\020rworld' | hexdump -c)"

# ^P is quoted to ^P^P
output=$(printf $'hello\020world' | irc_enquote | hexdump -c)
assert_equal "$output" "$(printf $'hello\020\020world' | hexdump -c)"

# Trailing newline should be left as-is, because other commands expect each
# line of a stream to be newline-terminated.
output=$(printf "hello\n" | irc_enquote | hexdump -c)
assert_equal "$output" "$(echo "hello" | hexdump -c)"
