Skip to content

Commit

Permalink
TESTBOX-402 #resolve
Browse files Browse the repository at this point in the history
New matcher: toHaveKeyWithCase()

TESTBOX-403 #resolve
Assertions: key() and notKey() now have a CaseSensitive boolean argument
  • Loading branch information
lmajano committed Sep 19, 2024
1 parent dbe163f commit db87ca3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
25 changes: 17 additions & 8 deletions system/Assertion.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,13 @@ component {
* @target The target object/struct
* @key The key to check for existence
* @message The message to send in the failure
* @caseSensitive If the key check is case sensitive
*/
function key(
required any target,
required string key,
message = ""
message = "",
boolean caseSensitive = false
){
arguments.target = normalizeToStruct( arguments.target );

Expand All @@ -428,7 +430,11 @@ component {
arguments.key
.listToArray()
.filter( function( thisKey ){
return structKeyExists( target, arguments.thisKey );
if( caseSensitive ){
return target.keyList().find( arguments.thisKey );
} else {
return structKeyExists( target, arguments.thisKey );
}
} )
.len() != listLen( arguments.key )
) {
Expand All @@ -443,32 +449,35 @@ component {
* @target The target object/struct
* @key The key to check for existence
* @message The message to send in the failure
* @caseSensitive If the key check is case sensitive
*/
function notKey(
required any target,
required string key,
message = ""
message = "",
boolean caseSensitive = false
){
arguments.target = normalizeToStruct( arguments.target );
arguments.message = (
len( arguments.message ) ? arguments.message : "The key [#arguments.key#] exists in the target object. Found keys are [#structKeyArray( arguments.target ).toString()#]"
);

if ( !structKeyExists( arguments.target, arguments.key ) ) {
return this;
}

// Inflate Key and process
if (
arguments.key
.listToArray()
.filter( function( thisKey ){
return structKeyExists( target, arguments.thisKey );
if( caseSensitive ){
return target.keyList().find( arguments.thisKey );
} else {
return structKeyExists( target, arguments.thisKey );
}
} )
.len() > 0
) {
fail( arguments.message );
}
return this;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions system/Expectation.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,22 @@ component accessors="true" {
return this;
}

/**
* Assert that a given key exists in the passed in struct/object with case sensitivity
*
* @key A key or a list of keys to check that the structure MUST contain
* @message The message to send in the failure
*/
function toHaveKeyWithCase( required string key, message = "" ){
arguments.target = this.actual;
if ( this.isNot ) {
variables.assert.notKey( argumentCollection = arguments );
} else {
variables.assert.key( argumentCollection = arguments );
}
return this;
}

/**
* Assert that a given key exists in the passed in struct by searching the entire nested structure
*
Expand Down

0 comments on commit db87ca3

Please sign in to comment.