StdWriters.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_TESTING__STD_WRITERS
18 #define COM_BORA_SOFTWARE__BALAU_TESTING__STD_WRITERS
19 
21 #include <Balau/Resource/File.hpp>
22 #include <Balau/Resource/Uri.hpp>
25 #include <Balau/Util/Vectors.hpp>
26 
27 #include <mutex>
28 
29 namespace Balau::Testing {
30 
34 class StdOutTestWriter : public TestWriter {
35  public: void writeString(const std::string & str) override {
36  std::cout << str;
37  }
38 
39  public: std::unique_ptr<TestWriter> clone() const override {
40  return std::unique_ptr<TestWriter>(new StdOutTestWriter());
41  }
42 };
43 
47 class FileTestWriter : public TestWriter {
48  public: explicit FileTestWriter(const Resource::File & file) : writeResource(file) {}
49 
50  public: void writeString(const std::string & str) override {
51  std::lock_guard<std::mutex> lock(mutex);
52  writeResource.writeStream() << str;
53  }
54 
55  public: std::unique_ptr<TestWriter> clone() const override {
56  return std::unique_ptr<TestWriter>(new FileTestWriter(writeResource.getFile()));
57  }
58 
60 
61  private: Resource::FileByteWriteResource writeResource;
62  private: std::mutex mutex;
63 };
64 
68 class OStreamTestWriter : public TestWriter {
69  public: explicit OStreamTestWriter(std::ostream & stream_) : stream(stream_) {}
70 
71  public: void writeString(const std::string & str) override {
72  stream << str;
73  }
74 
75  public: std::unique_ptr<TestWriter> clone() const override {
76  return std::unique_ptr<TestWriter>(new OStreamTestWriter(stream));
77  }
78 
80 
81  private: std::ostream & stream;
82 };
83 
84 } // namespace Balau::Testing
85 
86 #endif // COM_BORA_SOFTWARE__BALAU_TESTING__STD_WRITERS
Base class of test writers.
Definition: TestWriter.hpp:31
A test writer that writes to stdout.
Definition: StdWriters.hpp:34
A file on the local file system.
A write only standard file on a file system which is written as bytes.
void writeString(const std::string &str) override
Write the supplied UTF-8 string to the writer&#39;s output stream(s).
Definition: StdWriters.hpp:35
Utilities for vectors.
A write only standard file on a file system which is written as bytes.
Definition: FileByteWriteResource.hpp:32
A test writer that writes to the supplied output stream.
Definition: StdWriters.hpp:68
std::unique_ptr< TestWriter > clone() const override
Clone the writer instance.
Definition: StdWriters.hpp:39
A test writer that writes to the specified file.
Definition: StdWriters.hpp:47
std::unique_ptr< TestWriter > clone() const override
Clone the writer instance.
Definition: StdWriters.hpp:55
std::unique_ptr< TestWriter > clone() const override
Clone the writer instance.
Definition: StdWriters.hpp:75
Test assertion functions.
A file on the local file system.
Definition: File.hpp:35
Test writer that writes to the specified Balau logger.
The test runner and test assertion functions.
Definition: Assertions.hpp:23
void writeString(const std::string &str) override
Write the supplied UTF-8 string to the writer&#39;s output stream(s).
Definition: StdWriters.hpp:50
void writeString(const std::string &str) override
Write the supplied UTF-8 string to the writer&#39;s output stream(s).
Definition: StdWriters.hpp:71
The abstract URI base class.