/** @file test_cmd.cpp — 命令框架全覆盖 */
#include "myCmd.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
static int P=0,F=0,g=0;
#define T(n) do{printf("  %s ... ",n);}while(0)
#define OK() do{printf("OK\n");P++;}while(0)
#define FL(m) do{printf("FAIL: %s\n",m);F++;return;}while(0)
#define CK(c,m) do{if(!(c))FL(m);}while(0)

static void cb(int ac,char**av){(void)ac;(void)av;g++;}

static void t1(){T("add+find");g=0;cmd_manager_add_command("test_c",cb,"desc",NULL);stru_cmd*c=cmd_find("test_c");CK(c,"find");CK(!strcmp(c->name,"test_c"),"n");CK(!strcmp(c->desc,"desc"),"d");CK(c->func,"f");OK();}
static void t2(){T("find nonexistent");CK(!cmd_find("no_xyz"),"null");OK();}
static void t3(){T("execute");g=0;stru_cmd*c=cmd_find("test_c");c->func(0,NULL);CK(g==1,"called");OK();}
static void t4(){T("get_commands");unsigned n=0;stru_cmd*cs=cmd_manager_get_commands(&n);CK(n>0&&cs,"arr");OK();}
static void t5(){T("cmd_help");char*a[]={(char*)"help",NULL};cmd_help(1,a);OK();}
static void t6(){T("sub_complete");const char*s[]={"start","stop","status"};char**c=NULL;int n=0;cmd_sub_complete("st",&c,&n,s,3);CK(n>=2,"n");for(int i=0;i<n;i++)free(c[i]);free(c);OK();}
static void t7(){T("linenoise hist");linenoiseHistoryAdd("c1");linenoiseHistoryAdd("c2");linenoiseHistoryFree();linenoiseHistoryAdd("c3");OK();}

int main(){printf("\n=== libcmd ===\n\n");t1();t2();t3();t4();t5();t6();t7();printf("\n========\n  %d pass, %d fail\n========\n",P,F);return F>0?1:0;}
