Parsing simple arguments to a test

One of the actions you need to handle in the Setup member function is the parsing of arguments you pass to a test. The Test framework allows you to use keyed and nonkeyed arguments. For information on how to specify arguments on the command line, refer to "Providing input for a test" on page 82.

The following example shows how to parse keyed arguments that allow you to use a keyword on the command line to qualify the option. This example uses an n as the key for the value assigned to fIntegerOption, which is used in the Test member function. To pass a value of 255 to fIntegerOption, you use the following command line:

    RunTest -t TSampleObjectLengthTest SampleObjectTestLib -o -n 255
This example shows how to write the ParseKeyedArgs member function (which is not part of the Test framework) to take arguments you pass in from the command line and assign them to the appropriate variables. The Setup member function you override calls ParseKeyedArgs.

The following code interprets the command line:

      // Copyright (C) 1995 Taligent, Inc. All rights reserved.
    
    1  bool TSampleObjectLengthTest::ParseKeyedArgs(TTextArgumentDictionary& args)
    2  {
    3      bool result = true;    // set to false if parsing fails
    4  //    fIntegerOption = 0;
    5      
    6      static const TStandardText kMaxLengthKey("-n");
    7      // *** ADD CONSTANTS FOR ANY OTHER KEYS.
    8  
    9  try {
    10          if (!args.NumberAt(kMaxLengthKey, fIntegerOption, 0, 100000)) {
    11              result = false; // This argument is not optional.
    12          }
    13      } catch(const TTextArgumentDictionaryException& ) {
    14          result = false;    // Show help if value is out of range.
    15      };
    16  
    17      return result;
    18  }
Line number 6 defines -n as a key.

Line number 10 assigns the value following the key to fIntegerOption.

The following code shows the Setup function for TSampleObjectLengthTest, which calls the ParseKeyedArgs member function to use the command-line argument that specifies the value passed to the TSampleObject instance.

    // Copyright (C) 1995 Taligent, Inc. All rights reserved. void TSampleObjectLengthTest::Setup() { // *** IN REPETITIVE TESTS, Setup(), Test() and Cleanup() get called // repetitively. bool result = true; // set to true if setup is successful, false otherwise TTextArgumentDictionary args(*this); args.RemoveOnLookup(true); // Optional -- removes arguments as // they are read if (result) result = ParseKeyedArgs(args); // Parse keyed command-line arguments if (!result) { // Only set success if there is a failure in Setup. If nothing went wrong, // do not change the success value. SetSuccess(result); ShowUsage(); } }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker