From e17dc1bfc16042efa05bb322d2bad31a7875908d Mon Sep 17 00:00:00 2001 From: Bruno Pinto Date: Mon, 7 Dec 2015 21:39:10 +0000 Subject: [PATCH] Fish-spec plugin --- .gitignore | 2 + .../functions/assert.error_message.fish | 31 +++++++ .../functions/assert.expand_operator.fish | 10 ++ pkg/fish-spec/functions/assert.fish | 9 ++ pkg/fish-spec/functions/fish-spec.fish | 93 +++++++++++++++++++ .../spec/assert_error_message_spec.fish | 54 +++++++++++ pkg/fish-spec/spec/results_spec.fish | 43 +++++++++ 7 files changed, 242 insertions(+) create mode 100644 pkg/fish-spec/functions/assert.error_message.fish create mode 100644 pkg/fish-spec/functions/assert.expand_operator.fish create mode 100644 pkg/fish-spec/functions/assert.fish create mode 100644 pkg/fish-spec/functions/fish-spec.fish create mode 100644 pkg/fish-spec/spec/assert_error_message_spec.fish create mode 100644 pkg/fish-spec/spec/results_spec.fish diff --git a/.gitignore b/.gitignore index 525a939..40f2289 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ pkg/** !pkg/omf !pkg/omf/** +!pkg/fish-spec +!pkg/fish-spec/** themes/** diff --git a/pkg/fish-spec/functions/assert.error_message.fish b/pkg/fish-spec/functions/assert.error_message.fish new file mode 100644 index 0000000..3b7f349 --- /dev/null +++ b/pkg/fish-spec/functions/assert.error_message.fish @@ -0,0 +1,31 @@ +function assert.error_message + set -l number_of_arguments (count $argv) + + switch $argv[1] + case ! + switch $number_of_arguments + case 3 + set operator (assert.expand_operator $argv[2]) + set expected $argv[3] + echo "Expected $expected to not be $operator" + case 4 + set expected $argv[2] + set operator "not" (assert.expand_operator $argv[3]) + set actual $argv[4] + echo "Expected $expected to $operator $actual" + case \* + return 1 + end + case \-\* + test $number_of_arguments != 2; and return 1 + set operator (assert.expand_operator $argv[1]) + set expected $argv[2] + echo "Expected $expected to be $operator" + case \* + test $number_of_arguments != 3; and return 1 + set expected $argv[1] + set operator (assert.expand_operator $argv[2]) + set actual $argv[3] + echo "Expected $expected to $operator $actual" + end +end diff --git a/pkg/fish-spec/functions/assert.expand_operator.fish b/pkg/fish-spec/functions/assert.expand_operator.fish new file mode 100644 index 0000000..2ad7d73 --- /dev/null +++ b/pkg/fish-spec/functions/assert.expand_operator.fish @@ -0,0 +1,10 @@ +function assert.expand_operator -a operator + switch $operator + case = + echo equals + case \-z + echo empty + case \* + echo $operator + end +end diff --git a/pkg/fish-spec/functions/assert.fish b/pkg/fish-spec/functions/assert.fish new file mode 100644 index 0000000..f515964 --- /dev/null +++ b/pkg/fish-spec/functions/assert.fish @@ -0,0 +1,9 @@ +function assert --wraps test + if builtin test $argv + emit assertion_success + else + set -l assertion_status $status + emit assertion_error (assert.error_message $argv) + return $assertion_status + end +end diff --git a/pkg/fish-spec/functions/fish-spec.fish b/pkg/fish-spec/functions/fish-spec.fish new file mode 100644 index 0000000..bf5ea09 --- /dev/null +++ b/pkg/fish-spec/functions/fish-spec.fish @@ -0,0 +1,93 @@ +function fish-spec + # Reset internal variables + set -e __any_spec_failed + + # Load each spec file + for spec_file in spec/*_spec.fish + source $spec_file + end + + # Load helper file + source spec/helper.fish ^/dev/null + + # Run all specs + __fish-spec.run_all_specs + + not set -q __any_spec_failed +end + +function __fish-spec.run_all_specs + for suite in (functions -n | grep describe_) + __fish-spec.run_suite $suite + functions -e $suite + end +end + +function __fish-spec.run_suite -a suite_name + # This gets the list of specs that were defined on the test suite by + # comparing the functions names before and after the evaluation of the test suite. + set -l specs (begin + functions -n | grep it_ + eval $suite_name >/dev/null + functions -n | grep it_ + end | sort | uniq -u) + + functions -q before_all; and before_all + + for spec in $specs + emit spec_init $spec + functions -q before_each; and before_each + eval $spec + functions -q after_each; and after_each + emit spec_finished $spec + end + + functions -q after_all; and after_all + + functions -e before_all before_each after_each after_all +end + +function __fish-spec.spec_init -e spec_init -a spec + set -e __current_spec_status +end + +function __fish-spec.spec_finished -e spec_finished -a spec + functions -e $spec + + switch "$__current_spec_status" + case success + emit spec_success + case error + emit spec_error + case '*' + emit spec_no_assertions + end +end + +function __fish-spec.spec_success -e spec_success + echo -n '.' +end + +function __fish-spec.spec_error -e spec_error + echo -n 'F' + set -g __any_spec_failed true +end + +function __fish-spec.spec_no_assertions -e spec_no_assertions + echo -n 'N/A' +end + +function __fish-spec_assertion_success -e assertion_success + set -q __current_spec_status; or set -g __current_spec_status success +end + +function __fish-spec_assertion_error -e assertion_error -a error_message + # Mimics output redirect inside an event handler + if set -q __fish_spec_output + set __fish_spec_output $error_message + else + echo $error_message + end + + set -g __current_spec_status error +end diff --git a/pkg/fish-spec/spec/assert_error_message_spec.fish b/pkg/fish-spec/spec/assert_error_message_spec.fish new file mode 100644 index 0000000..03660da --- /dev/null +++ b/pkg/fish-spec/spec/assert_error_message_spec.fish @@ -0,0 +1,54 @@ +function describe_assert_error_message + function before_each + set -g __fish_spec_output "initial test value" + end + + function after_each + set -e __fish_spec_output + end + + function it_has_no_output_when_the_test_succeeds + assert 1 = 1 + + # Reset test status + set -e __current_spec_status + + assert 'initial test value' = "$__fish_spec_output"; or echo $__fish_spec_output + end + + function it_supports_unary_operators + assert -z "string" + + # Reset test status + set -e __current_spec_status + + assert 'Expected string to be empty' = "$__fish_spec_output"; or echo $__fish_spec_output + end + + function it_supports_binary_operators + assert 1 = 2 + + # Reset test status + set -e __current_spec_status + + assert 'Expected 1 to equals 2' = "$__fish_spec_output"; or echo $__fish_spec_output + end + + function it_supports_inversion_on_unary_operators + assert ! -z "" + + # Reset test status + set -e __current_spec_status + + assert 'Expected to not be empty' = "$__fish_spec_output"; or echo $__fish_spec_output + end + + function it_supports_inversion_on_binary_operators + assert ! 1 = 1 + + # Reset test status + set -e __current_spec_status + + assert 'Expected 1 to not equals 1' = "$__fish_spec_output"; or echo $__fish_spec_output + end +end diff --git a/pkg/fish-spec/spec/results_spec.fish b/pkg/fish-spec/spec/results_spec.fish new file mode 100644 index 0000000..9cae882 --- /dev/null +++ b/pkg/fish-spec/spec/results_spec.fish @@ -0,0 +1,43 @@ +function describe_results + function after_each + set -e __fish_spec_quiet + end + + function it_succeeds_when_single_assertion_succeeds + assert 1 = 1 + + assert success = "$__current_spec_status" + end + + function it_succeeds_when_multiple_assertion_succeeds + assert 1 = 1 + assert 2 = 2 + + assert success = "$__current_spec_status" + end + + function it_fails_when_single_assertion_fails + set -g __fish_spec_output "quiet" + + assert 1 = 2 + set -l spec_status $__current_spec_status + + # Reset internals + set -e __current_spec_status + + assert error = "$spec_status" + end + + function it_fails_when_one_of_the_assertions_fails + set -g __fish_spec_output "quiet" + + assert 1 = 2 + assert 2 = 2 + set -l spec_status $__current_spec_status + + # Reset internals + set -e __current_spec_status + + assert error = "$spec_status" + end +end