Coverage for /Syzygy/common/unittest_util.h

CoverageLines executed / instrumented / missingexe / inst / missLanguageGroup
100.0%25250.C++test

Line-by-line coverage:

   1    :  // Copyright 2012 Google Inc.
   2    :  //
   3    :  // Licensed under the Apache License, Version 2.0 (the "License");
   4    :  // you may not use this file except in compliance with the License.
   5    :  // You may obtain a copy of the License at
   6    :  //
   7    :  //     http://www.apache.org/licenses/LICENSE-2.0
   8    :  //
   9    :  // Unless required by applicable law or agreed to in writing, software
  10    :  // distributed under the License is distributed on an "AS IS" BASIS,
  11    :  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12    :  // See the License for the specific language governing permissions and
  13    :  // limitations under the License.
  14    :  //
  15    :  // Declares some unittest helper functions.
  16    :  //
  17    :  // There is no corresponding .cc file for this header; it can be freely
  18    :  // included in any unittest file without incurring additional dependencies
  19    :  // (other than base).
  20    :  
  21    :  #ifndef SYZYGY_COMMON_UNITTEST_UTIL_H_
  22    :  #define SYZYGY_COMMON_UNITTEST_UTIL_H_
  23    :  
  24    :  #include "base/file_path.h"
  25    :  #include "base/file_util.h"
  26    :  #include "base/logging.h"
  27    :  #include "gtest/gtest.h"
  28    :  
  29    :  namespace testing {
  30    :  
  31    :  // Helper class to make sure that a test that plays with the log level doesn't
  32    :  // change it for other tests.
  33    :  class ScopedLogLevelSaver {
  34    :   public:
  35  E :    ScopedLogLevelSaver() : level_(logging::GetMinLogLevel()) {
  36  E :    }
  37    :  
  38  E :    ~ScopedLogLevelSaver() {
  39  E :      logging::SetMinLogLevel(level_);
  40  E :    }
  41    :  
  42    :    int level() const { return level_; }
  43    :  
  44    :   private:
  45    :    int level_;
  46    :  };
  47    :  
  48    :  // An intermediate class to add helper streams to a unit-test fixture.
  49    :  class ApplicationTestBase : public testing::Test {
  50    :   public:
  51    :    // @name IO Stream Accessors.
  52    :    // Call InitStreams() to route the IO streams to/from specific files;
  53    :    // otherwise, they will be routed to/from the NUL device on first use.
  54    :    // @{
  55  E :    FILE* in() const { return GetOrInitFile(&in_, "r"); }
  56  E :    FILE* out() const { return GetOrInitFile(&out_, "w"); }
  57  E :    FILE* err() const { return GetOrInitFile(&err_, "w"); }
  58    :    // @}
  59    :  
  60    :    // Initialize the IO Streams to send output to specific files.
  61    :    void InitStreams(const FilePath& in_path,
  62    :                     const FilePath& out_path,
  63  E :                     const FilePath& err_path) {
  64  E :      ASSERT_FALSE(out_path.empty());
  65  E :      ASSERT_FALSE(err_path.empty());
  66    :  
  67  E :      in_.reset(file_util::OpenFile(in_path, "r"));
  68  E :      out_.reset(file_util::OpenFile(out_path, "w"));
  69  E :      err_.reset(file_util::OpenFile(err_path, "w"));
  70    :  
  71  E :      ASSERT_TRUE(in_.get() != NULL);
  72  E :      ASSERT_TRUE(out_.get() != NULL);
  73  E :      ASSERT_TRUE(err_.get() != NULL);
  74  E :    }
  75    :  
  76    :   protected:
  77    :    // Helper to initialize a given stream to refer to the NUL device on first
  78    :    // use if it hasn't already been associated with a file.
  79  E :    static FILE* GetOrInitFile(file_util::ScopedFILE* f, const char* mode) {
  80  E :      DCHECK(f != NULL);
  81  E :      DCHECK(mode != NULL);
  82  E :      if (f->get() == NULL)
  83  E :        f->reset(file_util::OpenFile("NUL", mode));
  84  E :      return f->get();
  85  E :    }
  86    :  
  87    :    // @name Replacements for the standard IO streams.
  88    :    //
  89    :    // By default they are routed to the NUL device (on first unitialized use).
  90    :    //
  91    :    // @{
  92    :    mutable file_util::ScopedFILE in_;
  93    :    mutable file_util::ScopedFILE out_;
  94    :    mutable file_util::ScopedFILE err_;
  95    :    // @}
  96    :  };
  97    :  
  98    :  }  // namespace testing
  99    :  
 100    :  #endif  // SYZYGY_COMMON_UNITTEST_UTIL_H_

Coverage information generated Thu Sep 06 11:30:46 2012.