testargcmd.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 2013-2019 Tom G. Huang
  5. * <tomghuang@gmail.com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #include <string.h>
  31. #include <time.h>
  32. #include <stdio.h>
  33. #include "CuTest.h"
  34. #include "argtable3.h"
  35. #if defined(_MSC_VER)
  36. #pragma warning(push)
  37. #pragma warning(disable : 4204)
  38. #endif
  39. int cmd1_proc(int argc, char* argv[], arg_dstr_t res) {
  40. if (argc == 0) {
  41. arg_dstr_catf(res, "cmd1 fail");
  42. return 1;
  43. }
  44. arg_dstr_catf(res, "%d %s", argc, argv[0]);
  45. return 0;
  46. }
  47. void test_argcmd_basic_001(CuTest* tc) {
  48. arg_cmd_init();
  49. CuAssertIntEquals(tc, 0, arg_cmd_count());
  50. arg_cmd_register("cmd1", cmd1_proc, "description of cmd1");
  51. CuAssertIntEquals(tc, 1, arg_cmd_count());
  52. char* argv[] = {
  53. "cmd1",
  54. "-o",
  55. "file1",
  56. };
  57. int argc = 3;
  58. CuAssertTrue(tc, strcmp(argv[0], "cmd1") == 0);
  59. CuAssertTrue(tc, strcmp(argv[1], "-o") == 0);
  60. CuAssertTrue(tc, strcmp(argv[2], "file1") == 0);
  61. arg_dstr_t res = arg_dstr_create();
  62. int err = arg_cmd_dispatch("cmd1", argc, argv, res);
  63. CuAssertIntEquals(tc, 0, err);
  64. CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "3 cmd1") == 0);
  65. arg_dstr_reset(res);
  66. err = arg_cmd_dispatch("cmd1", 0, NULL, res);
  67. CuAssertIntEquals(tc, 1, err);
  68. CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "cmd1 fail") == 0);
  69. arg_cmd_uninit();
  70. }
  71. CuSuite* get_argcmd_testsuite() {
  72. CuSuite* suite = CuSuiteNew();
  73. SUITE_ADD_TEST(suite, test_argcmd_basic_001);
  74. return suite;
  75. }
  76. #if defined(_MSC_VER)
  77. #pragma warning(pop)
  78. #endif