]> git.mdlowis.com Git - projs/opts.git/commitdiff
Added test for GetOptConfig
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 2 Jun 2012 17:28:49 +0000 (13:28 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 2 Jun 2012 17:28:49 +0000 (13:28 -0400)
source/opts.c
source/opts.h
tests/test_opts.cpp

index f3f3fd9ad246f2fdfacbb603d7cfc375921fb0f5..db41d0fe60ef08f41de791639b393c5472f549a8 100644 (file)
@@ -22,16 +22,16 @@ Result_T* OPTS_ParseOptions( OptionConfig_T* opts, int argc, char** argv )
         // If we have an option
         if ( '-' == ctx.current )
         {
-            // And its a long one
-            if( '-' == OPTS_NextCharacter( &ctx ) )
-            {
-                (void)OPTS_ParseLongOption( &ctx );
-            }
-            // or a short one
-            else
-            {
-                (void)OPTS_ParseShortOption( &ctx );
-            }
+            OPTS_ParseShortOption( &ctx );
+            //// And its a long one
+            //if( '-' == OPTS_NextCharacter( &ctx ) )
+            //{
+            //    (void)OPTS_ParseLongOption( &ctx );
+            //}
+            //// or a short one
+            //else
+            //{
+            //}
         }
         // Or we have a floating argument
         else
@@ -64,6 +64,24 @@ void OPTS_InitContext( StreamContext_T* ctx, int argc, char** argv )
 
 void OPTS_ParseShortOption( StreamContext_T* ctx )
 {
+    // Get Config
+    // if( current != ' ' )
+    // {
+    //     Get Config
+    //     if( config != NULL )
+    //     {
+    //         Save option
+    //         if( has arg )
+    //         {
+    //             Save arg
+    //         }
+    //         Add option to list
+    //     }
+    //     else
+    //     {
+    //         Exit: "Unknown option: %c\n"
+    //     }
+    // }
 }
 
 void OPTS_ParseLongOption( StreamContext_T* ctx )
@@ -74,6 +92,22 @@ void OPTS_ParseArgument( StreamContext_T* ctx )
 {
 }
 
+OptionConfig_T* OPTS_GetOptConfig( OptionConfig_T* opts, OptionType_T typ, char* name )
+{
+    OptionConfig_T* cfg = NULL;
+    int i = 0;
+    while( opts[i].type != END )
+    {
+        if( (opts[i].type == typ) && (0 == strcmp(opts[i].name, name)) )
+        {
+            cfg = &(opts[i]);
+            break;
+        }
+        i++;
+    }
+    return cfg;
+}
+
 char* OPTS_NextToken( StreamContext_T* ctx )
 {
     char* tok = NULL;
index 6329c208fa5d9df24383ee793a4e82e5c4091eac..50c6bed20831c7239e800a9932b4ae0736e70f4e 100644 (file)
@@ -61,6 +61,8 @@ void OPTS_ParseLongOption( StreamContext_T* ctx );
 
 void OPTS_ParseArgument( StreamContext_T* ctx );
 
+OptionConfig_T* OPTS_GetOptConfig( OptionConfig_T* opts, OptionType_T typ, char* name );
+
 char* OPTS_NextToken( StreamContext_T* ctx );
 
 char OPTS_NextCharacter( StreamContext_T* ctx );
index 163cb87835b0c1143fa52619b7ae85f109f3ba50..47dc2099090d78198a27f224cc9caf8921f4acc4 100644 (file)
@@ -104,6 +104,41 @@ namespace {
     //    free(results);
     //}
 
+
+    //-------------------------------------------------------------------------
+    // Test GetOptConfig Function
+    //-------------------------------------------------------------------------
+    TEST(Verify_GetOptConfig_Can_Retrieve_A_Short_Config_By_Name_And_Type)
+    {
+        OptionConfig_T* result = NULL;
+        result = OPTS_GetOptConfig( Options_Config, SHORT, "a" );
+        CHECK( result == &(Options_Config[0]) );
+    }
+
+    TEST(Verify_GetOptConfig_Can_Retrieve_A_Long_Config_By_Name_And_Type)
+    {
+        OptionConfig_T* result = NULL;
+        result = OPTS_GetOptConfig( Options_Config, LONG, "foo" );
+        CHECK( result == &(Options_Config[2]) );
+    }
+
+    TEST(Verify_GetOptConfig_Returns_Null_If_No_Config_Found)
+    {
+        OptionConfig_T* result = NULL;
+        result = OPTS_GetOptConfig( Options_Config, LONG, "baz" );
+        CHECK( result == NULL );
+    }
+
+    TEST(Verify_GetOptConfig_Returns_Null_If_Config_List_Empty)
+    {
+        OptionConfig_T config[] = {
+            { END,   NULL,  NULL,     0, NULL }
+        };
+        OptionConfig_T* result = NULL;
+        result = OPTS_GetOptConfig( config, LONG, "foo" );
+        CHECK( result == NULL );
+    }
+
     //-------------------------------------------------------------------------
     // Test NextToken Function
     //-------------------------------------------------------------------------