Where can I find a working example of IF IN_LIST?

I am trying to create a test/assert like macro and cannot get the syntax correct
I am obviusly not writing this macro correctly and I am getting confused/stuck :frowning:

The macro I have is like this:

macro( must_be_in_list NEEDLE  HAYSTACK )
  if( "${NEEDLE}" IN_LIST HAYSTACK )
    return()
    # all is well
  elseif( ${NEEDLE} STREQUAL ${HAYSTACK} )
    # All is well
    # All is well
  else()
    message( FATAL_ERROR "Needle: ${NEEDLE} not found in ${HAYSTACK}")
  endif()
    
endmacro()

I want to use it sort of like this in numerous cases (I am refactoring a large cmake solution) and trying to make some common test cases or asserts to simplify the CMakeLists.txt

set( n "dog" )   
list( APPEND L "cat" "dog")
must_be_in_list( "${n}" "${L}" )

NOTE that sometimes the list is a single word - hence the “STREQUAL” case
Example:

    set( n  "dog" )
    set( validpets  "dog" )  # In this case 
    must_be_in_list( "${n}" "${validpets}" )

You want a function, not a macro. Macro arguments are not real CMake variables, and if(a IN_LIST b) requires b to be the name of a variable.