瀏覽代碼

Add escapable strings to CLI.

Sam Jaffe 4 年之前
父節點
當前提交
3a1971c76c
共有 6 個文件被更改,包括 63 次插入30 次删除
  1. 2 0
      cli.xcodeproj/project.pbxproj
  2. 1 1
      external/string-utils
  3. 2 1
      src/cli.cxx
  4. 19 15
      test/cli_test.cxx
  5. 1 13
      test/prompt_test.cxx
  6. 38 0
      test/xcode_gtest_helper.h

+ 2 - 0
cli.xcodeproj/project.pbxproj

@@ -75,6 +75,7 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		CD7B8B7825E2946900867188 /* xcode_gtest_helper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xcode_gtest_helper.h; sourceTree = "<group>"; };
 		CDC748A52531353C008D9D1D /* libcli.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libcli.a; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDC748AC25313553008D9D1D /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
 		CDC748BD25313587008D9D1D /* cli */ = {isa = PBXFileReference; lastKnownFileType = folder; name = cli; path = include/cli; sourceTree = "<group>"; };
@@ -152,6 +153,7 @@
 		CDC748BC2531357C008D9D1D /* test */ = {
 			isa = PBXGroup;
 			children = (
+				CD7B8B7825E2946900867188 /* xcode_gtest_helper.h */,
 				CDC748E625313CBC008D9D1D /* cli_test.cxx */,
 				CDF978A425323ADB00204B5C /* prompt_test.cxx */,
 			);

+ 1 - 1
external/string-utils

@@ -1 +1 @@
-Subproject commit 9b9852d627a5bd510029dc55585b184835d813cb
+Subproject commit 6908eff3248a9ae007482b0d9a739d6d25cd585c

+ 2 - 1
src/cli.cxx

@@ -35,7 +35,8 @@ bool cli::active() const {
 void cli::run() const {
   std::string command;
   while (active() && std::getline(in_, command)) {
-    auto views = string_utils::split(command, " ");
+    auto split = string_utils::tokenizer(" ", {'"', "\\\""}).escapable(true);
+    auto views = split(command);
     command = views.front();
     views.erase(views.begin());
     if (!callbacks_.count(command)) {

+ 19 - 15
test/cli_test.cxx

@@ -8,21 +8,7 @@
 
 #include "cli/cli.h"
 
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-#if XCODE_UNIT_TEST
-// This is a hack to allow XCode to properly display failures when running
-// unit tests.
-#undef EXPECT_THAT
-#define EXPECT_THAT ASSERT_THAT
-#undef EXPECT_THROW
-#define EXPECT_THROW ASSERT_THROW
-#undef EXPECT_TRUE
-#define EXPECT_TRUE ASSERT_TRUE
-#undef EXPECT_FALSE
-#define EXPECT_FALSE ASSERT_FALSE
-#endif
+#include "xcode_gtest_helper.h"
 
 TEST(CliTest, EndsOnQuit) {
   std::stringstream input{R"(quit)"};
@@ -43,6 +29,24 @@ TEST(CliTest, WillSkipCallbackIfNotEnoughArgs) {
   EXPECT_FALSE(was_hit);
 }
 
+TEST(CliTest, CanEscapeSpaces) {
+  std::string data{};
+  std::stringstream input{R"(act A\ B)"};
+  cli::cli(input).register_callback("act", [&](std::string const &str){
+    data = str;
+  }).run();
+  EXPECT_THAT(data, "A B");
+}
+
+TEST(CliTest, CanQuoteWrapArgs) {
+  std::string data{};
+  std::stringstream input{R"(act "A B")"};
+  cli::cli(input).register_callback("act", [&](std::string const &str){
+    data = str;
+  }).run();
+  EXPECT_THAT(data, "A B");
+}
+
 struct example {};
 void cli_print(example const &) { throw example{}; }
 

+ 1 - 13
test/prompt_test.cxx

@@ -8,19 +8,7 @@
 
 #include "cli/prompt.h"
 
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-#if XCODE_UNIT_TEST
-// This is a hack to allow XCode to properly display failures when running
-// unit tests.
-#undef EXPECT_THAT
-#define EXPECT_THAT ASSERT_THAT
-#undef EXPECT_TRUE
-#define EXPECT_TRUE ASSERT_TRUE
-#undef EXPECT_FALSE
-#define EXPECT_FALSE ASSERT_FALSE
-#endif
+#include "xcode_gtest_helper.h"
 
 TEST(PromptTest, WillOutputEverythingInMessageWithNoSpaces) {
   std::stringstream out;

+ 38 - 0
test/xcode_gtest_helper.h

@@ -0,0 +1,38 @@
+//
+//  xcode_gtest_helper.h
+//  tax-calculator-test
+//
+//  Created by Sam Jaffe on 11/25/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#if defined(__APPLE__)
+
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wquoted-include-in-framework-header"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#pragma clang diagnostic pop
+
+#if defined(TARGET_OS_OSX)
+// This is a hack to allow XCode to properly display failures when running
+// unit tests.
+#undef EXPECT_THAT
+#define EXPECT_THAT ASSERT_THAT
+#undef EXPECT_THROW
+#define EXPECT_THROW ASSERT_THROW
+#undef EXPECT_ANY_THROW
+#define EXPECT_ANY_THROW ASSERT_ANY_THROW
+#undef EXPECT_NO_THROW
+#define EXPECT_NO_THROW ASSERT_NO_THROW
+#undef EXPECT_TRUE
+#define EXPECT_TRUE ASSERT_TRUE
+#undef EXPECT_FALSE
+#define EXPECT_FALSE ASSERT_FALSE
+
+#endif
+#endif