From 07251a8ea5322f957adfcbb2fffb6c5ca121418c Mon Sep 17 00:00:00 2001 From: Petr Kmoch Date: Sat, 29 Sep 2012 15:41:29 +0200 Subject: Consolidate list() argument count testing Move test for list() argument count >= 2 to InitialPass(). --- Source/cmListCommand.cxx | 36 +++++++------------------- Tests/CMakeTests/List-Invalid-Subcommand.cmake | 2 +- Tests/CMakeTests/ListTest.cmake.in | 30 +++++++++++++-------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/Source/cmListCommand.cxx b/Source/cmListCommand.cxx index 7848424..df64695 100644 --- a/Source/cmListCommand.cxx +++ b/Source/cmListCommand.cxx @@ -15,13 +15,14 @@ #include // required for atoi #include +#include //---------------------------------------------------------------------------- bool cmListCommand ::InitialPass(std::vector const& args, cmExecutionStatus &) { - if(args.size() < 1) + if(args.size() < 2) { - this->SetError("must be called with at least one argument."); + this->SetError("must be called with at least two arguments."); return false; } @@ -243,11 +244,7 @@ bool cmListCommand::HandleGetCommand(std::vector const& args) //---------------------------------------------------------------------------- bool cmListCommand::HandleAppendCommand(std::vector const& args) { - if(args.size() < 2) - { - this->SetError("sub-command APPEND requires at least one argument."); - return false; - } + assert(args.size() >= 2); // Skip if nothing to append. if(args.size() < 3) @@ -424,12 +421,8 @@ bool cmListCommand bool cmListCommand ::HandleReverseCommand(std::vector const& args) { - if(args.size() < 2) - { - this->SetError("sub-command REVERSE requires a list as an argument."); - return false; - } - else if(args.size() > 2) + assert(args.size() >= 2); + if(args.size() > 2) { this->SetError( "sub-command REVERSE only takes one argument."); @@ -463,13 +456,8 @@ bool cmListCommand bool cmListCommand ::HandleRemoveDuplicatesCommand(std::vector const& args) { - if(args.size() < 2) - { - this->SetError( - "sub-command REMOVE_DUPLICATES requires a list as an argument."); - return false; - } - else if(args.size() > 2) + assert(args.size() >= 2); + if(args.size() > 2) { this->SetError( "sub-command REMOVE_DUPLICATES only takes one argument."); @@ -513,12 +501,8 @@ bool cmListCommand bool cmListCommand ::HandleSortCommand(std::vector const& args) { - if(args.size() < 2) - { - this->SetError("sub-command SORT requires a list as an argument."); - return false; - } - else if(args.size() > 2) + assert(args.size() >= 2); + if(args.size() > 2) { this->SetError( "sub-command SORT only takes one argument."); diff --git a/Tests/CMakeTests/List-Invalid-Subcommand.cmake b/Tests/CMakeTests/List-Invalid-Subcommand.cmake index 40f2345..f35a118 100644 --- a/Tests/CMakeTests/List-Invalid-Subcommand.cmake +++ b/Tests/CMakeTests/List-Invalid-Subcommand.cmake @@ -1 +1 @@ -list(NO_SUCH_SUBCOMMAND) +list(NO_SUCH_SUBCOMMAND mylist) diff --git a/Tests/CMakeTests/ListTest.cmake.in b/Tests/CMakeTests/ListTest.cmake.in index b11a295..53559eb 100644 --- a/Tests/CMakeTests/ListTest.cmake.in +++ b/Tests/CMakeTests/ListTest.cmake.in @@ -101,26 +101,34 @@ list(SORT result) TEST("SORT empty result" "") set(No-Arguments-RESULT 1) -set(No-Arguments-STDERR ".*CMake Error at (@CMAKE_CURRENT_SOURCE_DIR@/)?List-No-Arguments.cmake:1 \\(list\\):.*list must be called with at least one argument.*") - -set(Get-No-Arguments-STDERR ".*CMake Error at List-Get-No-Arguments.cmake:1 \\(list\\):.*list sub-command GET requires at least three arguments.*") -set(Append-No-Arguments-STDERR ".*CMake Error at List-Append-No-Arguments.cmake:1 \\(list\\):.*list sub-command APPEND requires at least one argument.*") -set(Find-No-Arguments-STDERR ".*CMake Error at List-Find-No-Arguments.cmake:1 \\(list\\):.*list sub-command FIND requires three arguments.*") -set(Insert-No-Arguments-STDERR ".*CMake Error at List-Insert-No-Arguments.cmake:1 \\(list\\):.*list sub-command INSERT requires at least three arguments.*") -set(Remove_Item-No-Arguments-STDERR ".*CMake Error at List-Remove_Item-No-Arguments.cmake:1 \\(list\\):.*list sub-command REMOVE_ITEM requires two or more arguments.*") -set(Reverse-No-Arguments-STDERR ".*CMake Error at List-Reverse-No-Arguments.cmake:1 \\(list\\):.*list sub-command REVERSE requires a list as an argument.*") -set(Remove_Duplicates-No-Arguments-STDERR ".*CMake Error at List-Remove_Duplicates-No-Arguments.cmake:1 \\(list\\):.*list sub-command REMOVE_DUPLICATES requires a list as an argument.*") -set(Sort-No-Arguments-STDERR ".*CMake Error at List-Sort-No-Arguments.cmake:1 \\(list\\):.*list sub-command SORT requires a list as an argument.*") -set(Remove_At-No-Arguments-STDERR ".*CMake Error at List-Remove_At-No-Arguments.cmake:1 \\(list\\):.*list sub-command REMOVE_AT requires at least two arguments.*") +set(No-Arguments-STDERR ".*CMake Error at (@CMAKE_CURRENT_SOURCE_DIR@/)?List-No-Arguments.cmake:1 \\(list\\):.*list must be called with at least two arguments.*") +# these trigger top-level condition foreach(cmd IN ITEMS Append Find Get Insert Length Reverse Remove_At Remove_Duplicates Remove_Item Sort) set(${cmd}-No-Arguments-RESULT 1) + set(${cmd}-No-Arguments-STDERR ".*CMake Error at List-${cmd}-No-Arguments.cmake:1 \\(list\\):.*list must be called with at least two arguments.*") string(TOUPPER ${cmd} cmd_upper) set(_test_file_name "${CMAKE_CURRENT_BINARY_DIR}/List-${cmd}-No-Arguments.cmake") file(WRITE "${_test_file_name}" "list(${cmd_upper})\n") check_cmake_test_single(List "${cmd}-No-Arguments" "${_test_file_name}") endforeach() +set(Get-List-Only-STDERR "at least three") +set(Find-List-Only-STDERR "three") +set(Insert-List-Only-STDERR "at least three") +set(Length-List-Only-STDERR "two") +set(Remove_At-List-Only-STDERR "at least two") +set(Remove_Item-List-Only-STDERR "two or more") + +foreach(cmd IN ITEMS Find Get Insert Length Remove_At Remove_Item) + string(TOUPPER ${cmd} cmd_upper) + set(${cmd}-List-Only-RESULT 1) + set(${cmd}-List-Only-STDERR ".*CMake Error at List-${cmd}-List-Only.cmake:1 \\(list\\):.*list sub-command ${cmd_upper} requires ${${cmd}-List-Only-STDERR} arguments.*") + set(_test_file_name "${CMAKE_CURRENT_BINARY_DIR}/List-${cmd}-List-Only.cmake") + file(WRITE "${_test_file_name}" "list(${cmd_upper} mylist)\n") + check_cmake_test_single(List "${cmd}-List-Only" "${_test_file_name}") +endforeach() + set(Length-Too-Many-Arguments-RESULT 1) set(Length-Too-Many-Arguments-STDERR ".*CMake Error at (@CMAKE_CURRENT_SOURCE_DIR@/)?List-Length-Too-Many-Arguments.cmake:1 \\(list\\):.*list sub-command LENGTH requires two arguments.*") -- cgit v0.12