Browse Source

Add simplistic test for a CLI quitting.

Sam Jaffe 5 years ago
parent
commit
0bb846893d

+ 14 - 0
cli.xcodeproj/project.pbxproj

@@ -9,6 +9,8 @@
 /* Begin PBXBuildFile section */
 		CDC748C725313592008D9D1D /* libcli.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748A52531353C008D9D1D /* libcli.a */; };
 		CDC748E5253136FD008D9D1D /* cli.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC748E2253136FD008D9D1D /* cli.cxx */; };
+		CDC748E725313CBC008D9D1D /* cli_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CDC748E625313CBC008D9D1D /* cli_test.cxx */; };
+		CDC748E925313D81008D9D1D /* GoogleMock.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CDC748B425313553008D9D1D /* GoogleMock.framework */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -56,6 +58,7 @@
 		CDC748C225313592008D9D1D /* cli-test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "cli-test.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDC748C625313592008D9D1D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		CDC748E2253136FD008D9D1D /* cli.cxx */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cli.cxx; sourceTree = "<group>"; };
+		CDC748E625313CBC008D9D1D /* cli_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = cli_test.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -70,6 +73,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDC748E925313D81008D9D1D /* GoogleMock.framework in Frameworks */,
 				CDC748C725313592008D9D1D /* libcli.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -86,6 +90,7 @@
 				CDC748BC2531357C008D9D1D /* test */,
 				CDC748C325313592008D9D1D /* cli-test */,
 				CDC748A62531353C008D9D1D /* Products */,
+				CDC748E825313D81008D9D1D /* Frameworks */,
 			);
 			sourceTree = "<group>";
 		};
@@ -120,6 +125,7 @@
 		CDC748BC2531357C008D9D1D /* test */ = {
 			isa = PBXGroup;
 			children = (
+				CDC748E625313CBC008D9D1D /* cli_test.cxx */,
 			);
 			path = test;
 			sourceTree = "<group>";
@@ -132,6 +138,13 @@
 			path = "cli-test";
 			sourceTree = "<group>";
 		};
+		CDC748E825313D81008D9D1D /* Frameworks */ = {
+			isa = PBXGroup;
+			children = (
+			);
+			name = Frameworks;
+			sourceTree = "<group>";
+		};
 /* End PBXGroup section */
 
 /* Begin PBXHeadersBuildPhase section */
@@ -276,6 +289,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CDC748E725313CBC008D9D1D /* cli_test.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 77 - 0
cli.xcodeproj/xcshareddata/xcschemes/cli.xcscheme

@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Scheme
+   LastUpgradeVersion = "1130"
+   version = "1.3">
+   <BuildAction
+      parallelizeBuildables = "YES"
+      buildImplicitDependencies = "YES">
+      <BuildActionEntries>
+         <BuildActionEntry
+            buildForTesting = "YES"
+            buildForRunning = "YES"
+            buildForProfiling = "YES"
+            buildForArchiving = "YES"
+            buildForAnalyzing = "YES">
+            <BuildableReference
+               BuildableIdentifier = "primary"
+               BlueprintIdentifier = "CDC748A42531353C008D9D1D"
+               BuildableName = "libcli.a"
+               BlueprintName = "cli"
+               ReferencedContainer = "container:cli.xcodeproj">
+            </BuildableReference>
+         </BuildActionEntry>
+      </BuildActionEntries>
+   </BuildAction>
+   <TestAction
+      buildConfiguration = "Debug"
+      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      shouldUseLaunchSchemeArgsEnv = "YES">
+      <Testables>
+         <TestableReference
+            skipped = "NO">
+            <BuildableReference
+               BuildableIdentifier = "primary"
+               BlueprintIdentifier = "CDC748C125313592008D9D1D"
+               BuildableName = "cli-test.xctest"
+               BlueprintName = "cli-test"
+               ReferencedContainer = "container:cli.xcodeproj">
+            </BuildableReference>
+         </TestableReference>
+      </Testables>
+   </TestAction>
+   <LaunchAction
+      buildConfiguration = "Debug"
+      selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
+      selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
+      launchStyle = "0"
+      useCustomWorkingDirectory = "NO"
+      ignoresPersistentStateOnLaunch = "NO"
+      debugDocumentVersioning = "YES"
+      debugServiceExtension = "internal"
+      allowLocationSimulation = "YES">
+   </LaunchAction>
+   <ProfileAction
+      buildConfiguration = "Release"
+      shouldUseLaunchSchemeArgsEnv = "YES"
+      savedToolIdentifier = ""
+      useCustomWorkingDirectory = "NO"
+      debugDocumentVersioning = "YES">
+      <MacroExpansion>
+         <BuildableReference
+            BuildableIdentifier = "primary"
+            BlueprintIdentifier = "CDC748A42531353C008D9D1D"
+            BuildableName = "libcli.a"
+            BlueprintName = "cli"
+            ReferencedContainer = "container:cli.xcodeproj">
+         </BuildableReference>
+      </MacroExpansion>
+   </ProfileAction>
+   <AnalyzeAction
+      buildConfiguration = "Debug">
+   </AnalyzeAction>
+   <ArchiveAction
+      buildConfiguration = "Release"
+      revealArchiveInOrganizer = "YES">
+   </ArchiveAction>
+</Scheme>

+ 4 - 3
include/cli/cli.h

@@ -9,8 +9,8 @@
 #pragma once
 
 #include <functional>
+#include <iostream>
 #include <string>
-#include <string_view>
 #include <tuple>
 #include <unordered_map>
 #include <vector>
@@ -21,15 +21,16 @@ namespace cli {
 
 class cli {
 public:
-  using args_t = std::vector<std::string_view>;
+  using args_t = std::vector<std::string>;
   using callback = std::function<void(args_t const &)>;
 
 private:
+  std::istream &in_;
   std::unordered_map<std::string, callback> callbacks_;
   bool eof_;
 
 public:
-  cli();
+  cli(std::istream &in = std::cin);
   template <typename... Args>
   cli & register_callback(std::string const & handle,
                           std::function<void(Args...)> const & cb);

+ 3 - 3
src/cli.cxx

@@ -13,7 +13,7 @@
 namespace {
 
 auto tokenize(std::string const & search, std::string const & token) {
-  cli::cli::args_t rval;
+  std::vector<std::string> rval;
   size_t i = 0;
   for (size_t n = search.find(token); n != std::string::npos;
        i = n + 1, n = search.find(token, i)) {
@@ -28,13 +28,13 @@ auto tokenize(std::string const & search, std::string const & token) {
 
 namespace cli {
 
-cli::cli() : callbacks_(), eof_(false) {
+cli::cli(std::istream &in) : in_(in), callbacks_(), eof_(false) {
   register_callback("quit", [this] { eof_ = true; });
 }
 
 void cli::run() const {
   std::string command;
-  while (!eof_ && std::getline(std::cin, command).good()) {
+  while (!eof_ && std::getline(in_, command).good()) {
     auto views = tokenize(command, " ");
     command = views.front();
     views.erase(views.begin());

+ 24 - 0
test/cli_test.cxx

@@ -0,0 +1,24 @@
+//
+//  cli_test.cxx
+//  cli-test
+//
+//  Created by Sam Jaffe on 10/9/20.
+//  Copyright © 2020 Sam Jaffe. All rights reserved.
+//
+
+#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
+#endif
+
+TEST(CliTest, EndsOnQuit) {
+  std::stringstream input{R"(quit)"};
+  cli::cli(input).run();
+}