From f3c8064c593d70a6423e6d6f02dd81ffc9e32a43 Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Sat, 7 Sep 2024 12:54:00 +0200 Subject: [PATCH] TESTBOX-394 #resolve new `test(), xtest(), ftest()` alias for more natuarl testing --- system/BaseSpec.cfc | 72 +++++++++++++++++++++++++++++++++++++++-- tests/specs/BDDTest.cfc | 6 ++-- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/system/BaseSpec.cfc b/system/BaseSpec.cfc index 2d9b153..314a7f8 100644 --- a/system/BaseSpec.cfc +++ b/system/BaseSpec.cfc @@ -6,7 +6,7 @@ */ component { - // Param default URL method runner. + // Param default URL method runner when it runs remotely param name="url.method" default="runRemote"; // Assertions object @@ -551,6 +551,48 @@ component { return this; } + /** + * An alias to the it() function to provide a more natural language for BDD. + * + * @title The title of this test + * @body The closure that represents the test + * @labels The list or array of labels this spec belongs to + * @skip A flag or a closure that tells TestBox to skip this spec test from testing if true. If this is a closure it must return boolean. + * @data A struct of data you would like to bind into the spec so it can be later passed into the executing body function + * @focused A flag that tells TestBox to only run this spec and no other + */ + any function test( + required string title, + required any body, + any labels = [], + any skip = false, + struct data = {}, + boolean focused = false + ){ + return this.it( argumentCollection = arguments ); + } + + /** + * A focused alias to the fit() function to provide a more natural language for BDD. + * + * @then The title of this spec + * @body The closure that represents the test + * @labels The list or array of labels this spec belongs to + * @skip A flag or a closure that tells TestBox to skip this spec test from testing if true. If this is a closure it must return boolean. + * @data A struct of data you would like to bind into the spec so it can be later passed into the executing body function + * @focused A flag that tells TestBox to only run this spec and no other + */ + any function ftest( + required string title, + required any body, + any labels = [], + any skip = false, + struct data = {}, + boolean focused = false + ){ + return this.fit( argumentCollection = arguments ); + } + /** * The then() function describes a spec or a test in TestBox and is an alias for it. The body argument is the closure that implements * the test which usually contains one or more expectations that test the state of the code under test. @@ -560,13 +602,15 @@ component { * @labels The list or array of labels this spec belongs to * @skip A flag or a closure that tells TestBox to skip this spec test from testing if true. If this is a closure it must return boolean. * @data A struct of data you would like to bind into the spec so it can be later passed into the executing body function + * @focused A flag that tells TestBox to only run this spec and no other */ any function then( required string then, required any body, any labels = [], any skip = false, - struct data = {} + struct data = {}, + boolean focused = false ){ return it( argumentCollection = arguments, title = "Then " & arguments.then ); } @@ -579,13 +623,15 @@ component { * @labels The list or array of labels this spec belongs to * @skip A flag or a closure that tells TestBox to skip this spec test from testing if true. If this is a closure it must return boolean. * @data A struct of data you would like to bind into the spec so it can be later passed into the executing body function + * @focused A flag that tells TestBox to only run this spec and no other */ any function fthen( required string then, required any body, any labels = [], any skip = false, - struct data = {} + struct data = {}, + boolean focused = false ){ return fit( argumentCollection = arguments, title = "Then " & arguments.then ); } @@ -744,6 +790,26 @@ component { return this.then( argumentCollection = arguments ); } + /** + * This is a convenience method that makes sure the test spec is skipped from execution + * + * @title The title of this spec + * @body The closure that represents the test + * @labels The list or array of labels this spec belongs to + * @data A struct of data you would like to bind into the spec so it can be later passed into the executing body function + * @focused A flag that tells TestBox to only run this spec and no other + */ + any function xtest( + required string title, + required any body, + any labels = [], + struct data = {}, + boolean focused = false + ){ + arguments.skip = true; + return this.test( argumentCollection = arguments ); + } + /** * Start an expectation expression. This returns an instance of Expectation so you can work with its matchers. * diff --git a/tests/specs/BDDTest.cfc b/tests/specs/BDDTest.cfc index a14cfdd..22f34c1 100644 --- a/tests/specs/BDDTest.cfc +++ b/tests/specs/BDDTest.cfc @@ -365,16 +365,16 @@ component extends="testbox.system.BaseSpec" { structDelete( variables, "calc" ); } ); - it( "Can have a separate beforeEach for this suite", function(){ + test( "Can have a separate beforeEach for this suite", function(){ expect( request.calc ).toBeComponent(); } ); - xit( "can add incorrectly and fail", function(){ + xtest( "can add incorrectly and fail (Skipped)", function(){ var r = calc.add( 2, 2 ); expect( r ).toBe( 5 ); } ); - it( "cannot divide by zero", function(){ + test( "cannot divide by zero", function(){ expect( function(){ request.calc.divide( 4, 0 ); } ).toThrow( regex = "zero" );