1a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch/*
2a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Copyright (C) 2010 Apple Inc. All rights reserved.
3a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
4a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * Redistribution and use in source and binary forms, with or without
5a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * modification, are permitted provided that the following conditions
6a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * are met:
7a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 1. Redistributions of source code must retain the above copyright
8a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer.
9a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * 2. Redistributions in binary form must reproduce the above copyright
10a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    notice, this list of conditions and the following disclaimer in the
11a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *    documentation and/or other materials provided with the distribution.
12a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch *
13a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch * THE POSSIBILITY OF SUCH DAMAGE.
24a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch */
25a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
26a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#ifndef Test_h
27a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#define Test_h
28a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
29a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "TestsController.h"
30a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
31a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochnamespace TestWebKitAPI {
32a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
33a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch// Abstract base class that all tests inherit from.
34a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochclass Test {
35a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochpublic:
36a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual ~Test() { }
37a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
38a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    virtual void run() = 0;
39a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    std::string name() const { return m_identifier; }
40a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
41a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    template<typename TestClassTy> class Register {
42a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    public:
43a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        Register(const std::string& testSuite, const std::string& testCase)
44a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        {
45a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            TestsController::shared().registerCreateTestFunction(testSuite + "/" + testCase, Register::create);
46a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        }
47a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
48a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    private:
49a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        static Test* create(const std::string& identifier)
50a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        {
51a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            return new TestClassTy(identifier);
52a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        }
53a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    };
54a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
55a94275402997c11dd2e778633dacf4b7e630a35dBen Murdochprotected:
56a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    Test(const std::string& identifier)
57a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        : m_identifier(identifier)
58a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    {
59a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }
60a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
61a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    std::string m_identifier;
62a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch};
63a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
64a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#define TEST_CLASS_NAME(testSuite, testCaseName) testSuite##testCaseName##_Test
65a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#define TEST_REGISTRAR_NAME(testSuite, testCaseName) testSuite##testCaseName##_Registrar
66a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
67a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch// Use this to define a new test.
68a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#define TEST(testSuite, testCaseName) \
69a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    class TEST_CLASS_NAME(testSuite, testCaseName) : public Test { \
70a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    public: \
71a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        TEST_CLASS_NAME(testSuite, testCaseName)(const std::string& identifier) \
72a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch            : Test(identifier) \
73a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        { \
74a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        } \
75a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch        virtual void run(); \
76a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    }; \
77a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    \
78a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    static Test::Register<TEST_CLASS_NAME(testSuite, testCaseName)> TEST_REGISTRAR_NAME(testSuite, testCaseName)(#testSuite, #testCaseName); \
79a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    \
80a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch    void TEST_CLASS_NAME(testSuite, testCaseName)::run()
81a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
822fc2651226baac27029e38c9d6ef883fa32084dbSteve Block#define _TEST_ASSERT_HELPER(expression, returnStatement) do { if (!(expression)) { TestsController::shared().testFailed(__FILE__, __LINE__, #expression); returnStatement; } } while (0)
832fc2651226baac27029e38c9d6ef883fa32084dbSteve Block#define TEST_ASSERT(expression) _TEST_ASSERT_HELPER(expression, return)
842fc2651226baac27029e38c9d6ef883fa32084dbSteve Block#define TEST_ASSERT_RETURN(expression, returnValue) _TEST_ASSERT_HELPER(expression, return (returnValue))
85a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
86a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch} // namespace TestWebKitAPI
87a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch
88a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#endif // Test_h
89