Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterIttner committed Apr 4, 2024
1 parent 0f53b95 commit 20bc07e
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
with:
dotnet-version: 8.0.x
- name: Test
run: ./tests.sh
run: bash ./tests.sh

deploy:
if: startsWith(github.ref, 'refs/tags/v')
Expand Down
23 changes: 22 additions & 1 deletion Exceptions/InvalidParametersException.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace b64.Exceptions
{
class InvalidParametersException : Exception
{
public override string Message => "Invalid parameters detected.";
private readonly List<string> _arguments;

public InvalidParametersException(List<string> arguments)
{
_arguments = arguments;
}
public override string Message
{
get
{
if (_arguments == null)
{
return "Invalid parameters detected";
}
else
{
return "Invalid parameters detected: " + string.Join(", ", _arguments.Select(x => "{" + x + "}")) + " parameter-count: " + _arguments.Count;
}
}
}
}
}
13 changes: 10 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ class Program
{
static void Main(string[] args)
{
var arguments = args.ToList();
var arguments = args
.Where(arg => !string.IsNullOrWhiteSpace(arg))
.Where(arg => !string.IsNullOrEmpty(arg))
.ToList();

if (Console.IsInputRedirected)
{
using (Stream pipestream = Console.OpenStandardInput())
{
var reader = new StreamReader(pipestream);
arguments.Add(reader.ReadToEnd());
var arg = reader.ReadToEnd();
if(!string.IsNullOrEmpty(arg) && !string.IsNullOrWhiteSpace(arg))
{
arguments.Add(arg);
}
}
}

Expand All @@ -30,7 +37,7 @@ static void Main(string[] args)
}
else
{
throw new InvalidParametersException();
throw new InvalidParametersException(arguments);
}
}
catch (Exception e)
Expand Down
290 changes: 163 additions & 127 deletions tests.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,127 +1,163 @@
#!/bin/bash

set -e

dotnet build -c Debug
exe="./bin/Debug/net8.0/b64.exe"

########################################################################
# Test encoding decoding of a simple string
########################################################################
test_name="Test 1"
value=test
expectedEncoded=dGVzdA==

encoded=`$exe encode $value`
if [ "$encoded" != "$expectedEncoded" ]; then
echo "$test_name: Expected encoded value: $expectedEncoded, but have $encoded"
exit 1
fi
decoded=`$exe decode $encoded`
if [ "$decoded" != "$value" ]; then
echo "$test_name: Expected decoded value: $expected, but have $decoded"
exit 1
fi

########################################################################
# Test encoding decoding from a file with -f
########################################################################
test_name="Test 2"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test.txt
encoded=`$exe encode -f test.txt`
if [ "$encoded" != "$expectedEncoded" ]; then
echo "$test_name: Expected encoded value: $expectedEncoded, but have $encoded"
exit 1
fi
`$exe encode -f test.txt > test_encoded.txt`
decoded=`$exe decode -f test_encoded.txt`
if [ "$decoded" != "$value" ]; then
echo "$test_name: Expected decoded value: $expected, but have $decoded"
exit 1
fi

rm test.txt
rm test_encoded.txt

########################################################################
# Test encoding decoding from a file with <
########################################################################
test_name="Test 3"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test.txt
encoded=`$exe encode < test.txt`
if [ "$encoded" != "$expectedEncoded" ]; then
echo "$test_name: Expected encoded value: $expectedEncoded, but have $encoded"
exit 1
fi
`$exe encode -f test.txt > test_encoded.txt`
decoded=`$exe decode < test_encoded.txt`
if [ "$decoded" != "$value" ]; then
echo "$test_name: Expected decoded value: $expected, but have $decoded"
exit 1
fi

rm test.txt
rm test_encoded.txt

########################################################################
# Test encoding decoding from a file with pipe
########################################################################
test_name="Test 4"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test.txt
encoded=`cat test.txt | $exe encode`
if [ "$encoded" != "$expectedEncoded" ]; then
echo "$test_name: Expected encoded value: $expectedEncoded, but have $encoded"
exit 1
fi
`$exe encode -f test.txt > test_encoded.txt`
decoded=`cat test_encoded.txt | $exe decode`
if [ "$decoded" != "$value" ]; then
echo "$test_name: Expected decoded value: $expected, but have $decoded"
exit 1
fi

rm test.txt
rm test_encoded.txt

########################################################################
# Test url encoding
########################################################################
test_name="Test 5"
value="https://Hello?a=c&c=d_arg"
expectedEncoded=aHR0cHM6Ly9IZWxsbz9hPWMmYz1kX2FyZw
echo -n $value > test.txt
encoded=`cat test.txt | $exe encode --url`
if [ "$encoded" != "$expectedEncoded" ]; then
echo "$test_name: Expected encoded value: $expectedEncoded, but have $encoded"
exit 1
fi
`$exe encode -f test.txt > test_encoded.txt`
decoded=`cat test_encoded.txt | $exe decode --url`
if [ "$decoded" != "$value" ]; then
echo "$test_name: Expected decoded value: $expected, but have $decoded"
exit 1
fi

rm test.txt
rm test_encoded.txt

########################################################################
# Test url encoding back and forth with pipe
########################################################################
test_name="Test 6"
value="https://Hello?a=c&c=d_arg"
expected=`$exe encode --url $value | $exe decode --url`

if [ "$expected" != "$value" ]; then
echo "$test_name: Expected encoded value: $expected, but have $value"
exit 1
fi


echo "Tests passed!"
#!/bin/bash

set -e
#set -o xtrace

dotnet build -c Debug

unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) machine=Linux;;
Darwin*) machine=Mac;;
CYGWIN*) machine=Cygwin;;
MINGW*) machine=MinGw;;
MSYS_NT*) machine=Git;;
*) machine="UNKNOWN:${unameOut}"
esac

echo "Running current tests on $machine"

exe="./bin/Debug/net8.0/b64.exe"
if [ "$machine" == "Linux" ] || [ "$machine" == "Mac" ]; then
exe="./bin/Debug/net8.0/b64"
else
exe="./bin/Debug/net8.0/b64.exe"
fi

function assert() {

testname=$1
expected=$2
actual=$3

if [ "$expected" != "$actual" ]; then

echo "$testname: Expected value: $expected, but have $actual"

echo " _ _ __ _ _ _ ";
echo "| |_ ___ ___| |_ ___ / _| __ _(_) | ___ __| |";
echo "| __/ _ \/ __| __/ __| | |_ / _\` | | |/ _ \/ _\` |";
echo "| || __/\__ \ |_\__ \ | _| (_| | | | __/ (_| |";
echo " \__\___||___/\__|___/ |_| \__,_|_|_|\___|\__,_|";
echo " ";


exit 1
else
echo "$testname succeeded"
fi
}

function success() {
echo "All tests passed"
echo
echo " _ _ _ _ ";
echo "| |_ ___ ___| |_ ___ ___ _ _ ___ ___ ___ ___ __| | ___ __| |";
echo "| __/ _ \/ __| __/ __| / __| | | |/ __/ __/ _ \/ _ \/ _\` |/ _ \/ _\` |";
echo "| || __/\__ \ |_\__ \ \__ \ |_| | (_| (_| __/ __/ (_| | __/ (_| |";
echo " \__\___||___/\__|___/ |___/\__,_|\___\___\___|\___|\__,_|\___|\__,_|";
echo " ";
}


########################################################################
# Test encoding decoding of a simple string
########################################################################
test_name="Test 1"
value=test_value
expectedEncoded=dGVzdF92YWx1ZQ==

encoded=`$exe encode $value`
assert "$test_name" "$encoded" "$expectedEncoded"

decoded=`$exe decode $encoded`
assert "$test_name" "$decoded" "$value"

########################################################################
# Test encoding decoding from a file with -f
########################################################################
test_name="Test 2"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test_input.txt
encoded=`$exe encode -f test_input.txt`
assert "$test_name" "$encoded" "$expectedEncoded"

`$exe encode -f test_input.txt > test_encoded.txt`
decoded=`$exe decode -f test_encoded.txt`
assert "$test_name" "$decoded" "$value"

rm test_input.txt
rm test_encoded.txt

########################################################################
# Test encoding decoding from a file with <
########################################################################
test_name="Test 3"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test_input.txt
encoded=`$exe encode < test_input.txt`
assert "$test_name" "$encoded" "$expectedEncoded"

`$exe encode -f test_input.txt > test_encoded.txt`
decoded=`$exe decode < test_encoded.txt`
assert "$test_name" "$decoded" "$value"
rm test_input.txt
rm test_encoded.txt

########################################################################
# Test encoding decoding from a file with pipe
########################################################################
test_name="Test 4"
value="hello world"
expectedEncoded=aGVsbG8gd29ybGQ=
echo -n $value > test_input.txt
encoded=`cat test_input.txt | $exe encode`
assert "$test_name" "$encoded" "$expectedEncoded"

`$exe encode -f test_input.txt > test_encoded.txt`
decoded=`cat test_encoded.txt | $exe decode`
assert "$test_name" "$decoded" "$value"

rm test_input.txt
rm test_encoded.txt

########################################################################
# Test url encoding
########################################################################
test_name="Test 5"
value="https://Hello?a=c&c=d_arg"
expectedEncoded=aHR0cHM6Ly9IZWxsbz9hPWMmYz1kX2FyZw
echo -n $value > test_input.txt
encoded=`cat test_input.txt | $exe encode --url`
assert "$test_name" "$encoded" "$expectedEncoded"

`$exe encode -f test_input.txt > test_encoded.txt`
decoded=`cat test_encoded.txt | $exe decode --url`
assert "$test_name" "$decoded" "$value"

rm test_input.txt
rm test_encoded.txt

########################################################################
# Test url encoding back and forth with pipe
########################################################################
test_name="Test 6"
value="https://Hello?a=c&c=d_arg"
expected=`$exe encode --url $value | $exe decode --url`

assert "$test_name" "$expected" "$value"

########################################################################
# Test encoding with whitespace
########################################################################
test_name="Test 7"
value="https://Hello?a=c&c=d_arg"
expected=`$exe encode --url $value | $exe decode --url `

assert "$test_name" "$expected" "$value"


success

0 comments on commit 20bc07e

Please sign in to comment.