gtest: added Google Unit Test example to playground

This commit is contained in:
Syl 2014-02-20 21:33:00 +01:00
parent f4185e7a26
commit 1edb8a2f00
15 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,17 @@
@echo off
rem edit this path
set LIB_PATH=e:/project/libs
set INCLUDE=%CD%
pushd "%LIB_PATH%"
set LIB_PATH=%CD%
popd
rem edit those paths
set GTEST_PATH=%LIB_PATH%\gtest-1.7.0
set INCLUDE=%INCLUDE%;%GTEST_PATH%\include
set LIB=%LIB%;%GTEST_PATH%\msvc\gtest-msvc2013\Debug
set LIB=%LIB%;%GTEST_PATH%\msvc\gtest-msvc2013\Release
python waf configure

View File

@ -0,0 +1,16 @@
#include <stdlib.h>
#include "Accumulator.h"
Accumulator::Accumulator():m_total(0)
{
}
void Accumulator::accumulate(const char * data)
{
m_total += strtol(data, 0, 0);
}
int Accumulator::total() const
{
return m_total;
}

View File

@ -0,0 +1,23 @@
#ifndef Accumulator_h_seen
#define Accumulator_h_seen
/**
* A not-very-useful class that accumulates int values from input data.
*/
class Accumulator
{
public:
Accumulator();
/**
* Given some input data, read to end of line and convert to an int
*/
void accumulate(const char * data);
int total() const;
private:
int m_total;
};
#endif

View File

@ -0,0 +1,18 @@
#include "HelloWorld.h"
HelloWorld::HelloWorld():m_message("Hello World")
{
}
HelloWorld::HelloWorld(const std::string & msg):m_message(msg)
{}
std::string HelloWorld::message() const
{
return m_message;
}
void HelloWorld::setMessage(const std::string & msg)
{
m_message = msg;
}

View File

@ -0,0 +1,15 @@
#ifndef HelloWorld_h_seen
#define HelloWorld_h_seen
#include <string>
class HelloWorld
{
public:
HelloWorld();
HelloWorld(const std::string & msg);
std::string message() const;
void setMessage(const std::string & msg);
private:
std::string m_message;
};
#endif

View File

@ -0,0 +1,10 @@
#! /usr/bin/env python
# encoding: utf-8
bld(
features = 'cxx cxxstlib',
source = 'Accumulator.cpp HelloWorld.cpp',
target = 'useless',
export_includes = '.',
)

View File

@ -0,0 +1,38 @@
// Copyright 2006, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <cstdio>
#include "gtest/gtest.h"
GTEST_API_ int main(int argc, char **argv) {
printf("Running main() from gtest_main.cc\n");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,23 @@
#include "gtest/gtest.h"
#include "HelloWorld.h"
#include <string>
using namespace std;
TEST(HelloWorldTest, test0)
{
HelloWorld hello;
string expected("Hello World");
ASSERT_EQ(expected, hello.message());
}
TEST(HelloWorldTest, test1)
{
string expected("Hola Mundo");
HelloWorld hello(expected);
ASSERT_EQ(expected, hello.message());
expected = "Hello, world!";
hello.setMessage(expected);
ASSERT_EQ(expected, hello.message());
}

View File

@ -0,0 +1,10 @@
#! /usr/bin/env python
# encoding: utf-8
bld(
features = 'cxx cxxprogram test',
source = 'HelloWorldTest.cpp',
target = 'unit_test_program',
use = 'unittestmain useless GTEST',
)

View File

@ -0,0 +1,72 @@
#include "gtest/gtest.h"
#include "Accumulator.h"
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class AccumulatorTest : public testing::Test
{
protected:
virtual void SetUp();
virtual void TearDown();
Accumulator * m_accumulator;
};
void AccumulatorTest::SetUp()
{
m_accumulator = new Accumulator;
}
void AccumulatorTest::TearDown()
{
delete m_accumulator;
}
static void readlines(const char * filename, vector<string> & lines)
{
string datafile("input");
datafile += "/";
datafile += filename;
ifstream infile;
infile.open(datafile.c_str());
if (infile.is_open())
{
char buffer[BUFSIZ];
while (!infile.eof())
{
infile.getline(buffer, BUFSIZ);
lines.push_back(buffer);
}
}
}
TEST_F(AccumulatorTest, test0)
{
vector<string> lines;
readlines("test0.txt", lines);
size_t expected(2);
ASSERT_EQ(expected, lines.size());
m_accumulator->accumulate(lines[0].c_str());
ASSERT_EQ(10, m_accumulator->total());
}
TEST_F(AccumulatorTest, test1)
{
vector<string> lines;
readlines("test1.txt", lines);
size_t expected(6);
ASSERT_EQ(expected, lines.size());
for (vector<string>::const_iterator it(lines.begin());
it != lines.end(); ++it)
{
const string & line(*it);
m_accumulator->accumulate(line.c_str());
}
ASSERT_EQ(1+2+3+4+5, m_accumulator->total());
}

View File

@ -0,0 +1 @@
10

View File

@ -0,0 +1,5 @@
1
2
3
4
5

View File

@ -0,0 +1,18 @@
#! /usr/bin/env python
# encoding: utf-8
def fun(task):
pass
# print task.generator.bld.name_to_obj('somelib').link_task.outputs[0].abspath(task.env)
# task.ut_exec.append('--help')
bld(
features = 'cxx cxxprogram test',
source = 'AccumulatorTest.cpp',
target = 'unit_test_program',
use = 'unittestmain useless GTEST',
ut_cwd = bld.path.abspath(),
ut_fun = fun
)

View File

@ -0,0 +1,11 @@
#! /usr/bin/env python
# encoding: utf-8
def build(bld):
bld.recurse('test0 test1')
bld(
features = 'cxx stlib',
source = 'gtest_main.cc',
use = 'GTEST',
target = 'unittestmain')

66
playground/gtest/wscript Normal file
View File

@ -0,0 +1,66 @@
#! /usr/bin/env python
# encoding: utf-8
# Sylvain Rouquette, 2014
# based on Richard Quirk's demo (unit_test), 2008
"""
Execute tests during the build - requires cppunit
To force all tests, run with "waf build --alltests"
"""
from waflib import Logs
top = '.'
out = 'build'
def options(opt):
opt.load('compiler_cxx')
opt.load('waf_unit_test')
opt.add_option('--onlytests', action='store_true', default=True, help='Exec unit tests only', dest='only_tests')
def configure(conf):
conf.load('compiler_cxx')
conf.load('waf_unit_test')
conf.check(lib='gtest', uselib_store='GTEST')
def gtest_results(bld):
lst = getattr(bld, 'utest_results', [])
if not lst:
return
for (f, code, out, err) in lst:
# if not code:
# continue
# uncomment if you want to see what's happening
# print(str(out, 'utf-8'))
output = str(out, 'utf-8').split('\n')
for i, line in enumerate(output):
if code and '[ RUN ]' in line:
if ' OK ]' in output[i + 1]:
continue
# \r to get colors back...
Logs.warn('%s' % output[i + 1])
elif code and ' FAILED ]' in line:
Logs.error('%s' % line)
elif ' PASSED ]' in line:
Logs.info('%s' % line)
def build(bld):
bld.recurse('src tests')
# unittestw.summary is a pretty ugly function for displaying a report (feel free to improve!)
# results -> bld.utest_results [(filename, returncode, stdout, stderr), (..., ), ...]
#bld.add_post_fun(waf_unit_test.summary)
bld.add_post_fun(gtest_results)
# to execute all tests:
# $ waf --alltests
# to set this behaviour permanenly:
bld.options.all_tests = True
# debugging zone:
# $ waf --zones=ut
# setting the cwd for a unit test execution: see tests/test1/wscript_build (ut_cwd)