14a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Copyright (c) 2010 The Chromium Authors. All rights reserved.
24a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
34a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch// found in the LICENSE file.
44a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
54a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "base/values.h"
64a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "chrome/common/json_schema_validator.h"
74a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "chrome/common/json_schema_validator_unittest_base.h"
84a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#include "testing/gtest/include/gtest/gtest.h"
94a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
104a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdochclass JSONSchemaValidatorCPPTest : public JSONSchemaValidatorTestBase {
114a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch public:
124a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  JSONSchemaValidatorCPPTest()
134a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      : JSONSchemaValidatorTestBase(JSONSchemaValidatorTestBase::CPP) {
144a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  }
154a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
164a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch protected:
174a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void ExpectValid(const std::string& test_source,
184a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                           Value* instance, DictionaryValue* schema,
194a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                           ListValue* types) {
204a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    JSONSchemaValidator validator(schema, types);
214a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    if (validator.Validate(instance))
224a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      return;
234a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
244a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    for (size_t i = 0; i < validator.errors().size(); ++i) {
254a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      ADD_FAILURE() << test_source << ": "
264a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                    << validator.errors()[i].path << ": "
274a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                    << validator.errors()[i].message;
284a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    }
294a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  }
304a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
314a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  virtual void ExpectNotValid(const std::string& test_source,
324a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                              Value* instance, DictionaryValue* schema,
334a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                              ListValue* types,
344a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                              const std::string& expected_error_path,
354a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch                              const std::string& expected_error_message) {
364a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    JSONSchemaValidator validator(schema, types);
374a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    if (validator.Validate(instance)) {
384a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      ADD_FAILURE() << test_source;
394a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch      return;
404a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    }
414a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
424a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    ASSERT_EQ(1u, validator.errors().size()) << test_source;
434a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    EXPECT_EQ(expected_error_path, validator.errors()[0].path) << test_source;
444a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch    EXPECT_EQ(expected_error_message, validator.errors()[0].message)
454a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch        << test_source;
464a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  }
474a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch};
484a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch
494a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben MurdochTEST_F(JSONSchemaValidatorCPPTest, Test) {
504a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  RunTests();
514a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch}
52