Move Editor class from verify_test.cc to test.cc
authorCarl Hetherington <cth@carlh.net>
Sun, 25 Jun 2023 23:56:07 +0000 (01:56 +0200)
committerCarl Hetherington <cth@carlh.net>
Mon, 26 Jun 2023 22:10:53 +0000 (00:10 +0200)
test/test.cc
test/test.h
test/verify_test.cc

index 309d522583fe021ba190958aa7ea166a23ee147f..fb8d8a3dfd9ffda23af342960a88bbdbb102fb68 100644 (file)
@@ -68,6 +68,7 @@ LIBDCP_ENABLE_WARNINGS
 LIBDCP_DISABLE_WARNINGS
 #include <libxml++/libxml++.h>
 LIBDCP_ENABLE_WARNINGS
+#include <boost/algorithm/string.hpp>
 #include <boost/test/unit_test.hpp>
 #include <cstdio>
 #include <iostream>
@@ -546,4 +547,114 @@ find_file (boost::filesystem::path dir, string filename_part)
 }
 
 
+Editor::Editor(boost::filesystem::path path)
+       : _path(path)
+{
+       _content = dcp::file_to_string (_path);
+}
+
+
+Editor::~Editor()
+{
+       auto f = fopen(_path.string().c_str(), "w");
+       BOOST_REQUIRE (f);
+       fwrite (_content.c_str(), _content.length(), 1, f);
+       fclose (f);
+}
+
+
+void
+Editor::replace(string a, string b)
+{
+       ChangeChecker cc(this);
+       boost::algorithm::replace_all (_content, a, b);
+}
+
+
+void
+Editor::delete_first_line_containing(string s)
+{
+       ChangeChecker cc(this);
+       auto lines = as_lines();
+       _content = "";
+       bool done = false;
+       for (auto i: lines) {
+               if (i.find(s) == string::npos || done) {
+                       _content += i + "\n";
+               } else {
+                       done = true;
+               }
+       }
+}
+
+void
+Editor::delete_lines(string from, string to)
+{
+       ChangeChecker cc(this);
+       auto lines = as_lines();
+       bool deleting = false;
+       _content = "";
+       for (auto i: lines) {
+               if (i.find(from) != string::npos) {
+                       deleting = true;
+               }
+               if (!deleting) {
+                       _content += i + "\n";
+               }
+               if (deleting && i.find(to) != string::npos) {
+                       deleting = false;
+               }
+       }
+}
+
+
+void
+Editor::insert(string after, string line)
+{
+       ChangeChecker cc(this);
+       auto lines = as_lines();
+       _content = "";
+       bool replaced = false;
+       for (auto i: lines) {
+               _content += i + "\n";
+               if (!replaced && i.find(after) != string::npos) {
+                       _content += line + "\n";
+                       replaced = true;
+               }
+       }
+}
+
+
+void
+Editor::delete_lines_after(string after, int lines_to_delete)
+{
+       ChangeChecker cc(this);
+       auto lines = as_lines();
+       _content = "";
+       auto iter = std::find_if(lines.begin(), lines.end(), [after](string const& line) {
+               return line.find(after) != string::npos;
+       });
+       int to_delete = 0;
+       for (auto i = lines.begin(); i != lines.end(); ++i) {
+               if (i == iter) {
+                       to_delete = lines_to_delete;
+                       _content += *i + "\n";
+               } else if (to_delete == 0) {
+                       _content += *i + "\n";
+               } else {
+                       --to_delete;
+               }
+       }
+}
+
+
+vector<string>
+Editor::as_lines() const
+{
+       vector<string> lines;
+       boost::algorithm::split(lines, _content, boost::is_any_of("\r\n"), boost::token_compress_on);
+       return lines;
+}
+
+
 BOOST_GLOBAL_FIXTURE (TestConfig);
index 82e6afd54f33eca2bedbf3279d5aab1bc4767b56..bacb9311db9175d7fb21ebfd1804e77a73212a8d 100644 (file)
@@ -26,6 +26,7 @@
 #include "reel_asset.h"
 #include <boost/filesystem.hpp>
 #include <boost/optional.hpp>
+#include <boost/test/unit_test.hpp>
 
 namespace xmlpp {
        class Element;
@@ -85,3 +86,48 @@ public:
        RNGFixer ();
        ~RNGFixer ();
 };
+
+
+/** Class that can alter a file by searching and replacing strings within it.
+ *  On destruction modifies the file whose name was given to the constructor.
+ */
+class Editor
+{
+public:
+       Editor(boost::filesystem::path path);
+       ~Editor();
+
+       class ChangeChecker
+       {
+       public:
+               ChangeChecker(Editor* editor)
+                       : _editor(editor)
+               {
+                       _old_content = _editor->_content;
+               }
+
+               ~ChangeChecker()
+               {
+                       BOOST_REQUIRE(_old_content != _editor->_content);
+               }
+       private:
+               Editor* _editor;
+               std::string _old_content;
+       };
+
+       void replace(std::string a, std::string b);
+       void delete_first_line_containing(std::string s);
+       void delete_lines(std::string from, std::string to);
+       void insert(std::string after, std::string line);
+       void delete_lines_after(std::string after, int lines_to_delete);
+
+private:
+       friend class ChangeChecker;
+
+       std::vector<std::string> as_lines() const;
+
+       boost::filesystem::path _path;
+       std::string _content;
+};
+
+
index bc76de753b388b786c8eba8dfd6a9c0a845cc7e9..d7d9aaaec335184c2e285de14f45aec76aede82e 100644 (file)
@@ -153,135 +153,6 @@ write_dcp_with_single_asset (path dir, shared_ptr<dcp::ReelAsset> reel_asset, dc
 }
 
 
-/** Class that can alter a file by searching and replacing strings within it.
- *  On destruction modifies the file whose name was given to the constructor.
- */
-class Editor
-{
-public:
-       Editor (path path)
-               : _path(path)
-       {
-               _content = dcp::file_to_string (_path);
-       }
-
-       ~Editor ()
-       {
-               auto f = fopen(_path.string().c_str(), "w");
-               BOOST_REQUIRE (f);
-               fwrite (_content.c_str(), _content.length(), 1, f);
-               fclose (f);
-       }
-
-       class ChangeChecker
-       {
-       public:
-               ChangeChecker(Editor* editor)
-                       : _editor(editor)
-               {
-                       _old_content = _editor->_content;
-               }
-
-               ~ChangeChecker()
-               {
-                       BOOST_REQUIRE(_old_content != _editor->_content);
-               }
-       private:
-               Editor* _editor;
-               std::string _old_content;
-       };
-
-       void replace (string a, string b)
-       {
-               ChangeChecker cc(this);
-               boost::algorithm::replace_all (_content, a, b);
-       }
-
-       void delete_first_line_containing (string s)
-       {
-               ChangeChecker cc(this);
-               auto lines = as_lines();
-               _content = "";
-               bool done = false;
-               for (auto i: lines) {
-                       if (i.find(s) == string::npos || done) {
-                               _content += i + "\n";
-                       } else {
-                               done = true;
-                       }
-               }
-       }
-
-       void delete_lines (string from, string to)
-       {
-               ChangeChecker cc(this);
-               auto lines = as_lines();
-               bool deleting = false;
-               _content = "";
-               for (auto i: lines) {
-                       if (i.find(from) != string::npos) {
-                               deleting = true;
-                       }
-                       if (!deleting) {
-                               _content += i + "\n";
-                       }
-                       if (deleting && i.find(to) != string::npos) {
-                               deleting = false;
-                       }
-               }
-       }
-
-       void insert (string after, string line)
-       {
-               ChangeChecker cc(this);
-               auto lines = as_lines();
-               _content = "";
-               bool replaced = false;
-               for (auto i: lines) {
-                       _content += i + "\n";
-                       if (!replaced && i.find(after) != string::npos) {
-                               _content += line + "\n";
-                               replaced = true;
-                       }
-               }
-       }
-
-       void delete_lines_after(string after, int lines_to_delete)
-       {
-               ChangeChecker cc(this);
-               auto lines = as_lines();
-               _content = "";
-               auto iter = std::find_if(lines.begin(), lines.end(), [after](string const& line) {
-                       return line.find(after) != string::npos;
-               });
-               int to_delete = 0;
-               for (auto i = lines.begin(); i != lines.end(); ++i) {
-                       if (i == iter) {
-                               to_delete = lines_to_delete;
-                               _content += *i + "\n";
-                       } else if (to_delete == 0) {
-                               _content += *i + "\n";
-                       } else {
-                               --to_delete;
-                       }
-               }
-       }
-
-private:
-       friend class ChangeChecker;
-
-       vector<string> as_lines() const
-       {
-               vector<string> lines;
-               boost::algorithm::split(lines, _content, boost::is_any_of("\r\n"), boost::token_compress_on);
-               return lines;
-       }
-
-       path _path;
-       std::string _content;
-};
-
-
 LIBDCP_DISABLE_WARNINGS
 static
 void