Fix argument to catch claus was wrong.

Add support to compile TinyGLTF with no C++ exception(TINYGLTF_NOEXCEPTION). Fixes #18.
Add initial unit tests.
This commit is contained in:
Syoyo Fujita 2018-01-05 20:13:50 +09:00
parent 584f8c0ddf
commit 17cfbcc976
6 changed files with 10520 additions and 5 deletions

View File

@ -1,6 +1,6 @@
# Use this for strict compilation check(will work on clang 3.8+) # Use this for strict compilation check(will work on clang 3.8+)
#EXTRA_CXXFLAGS := -fsanitize=address -Wall -Werror -Weverything -Wno-c++11-long-long -DTINYGLTF_APPLY_CLANG_WEVERYTHING #EXTRA_CXXFLAGS := -fsanitize=address -Wall -Werror -Weverything -Wno-c++11-long-long
all: all:
clang++ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o loader_example loader_example.cc clang++ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o loader_example loader_example.cc

View File

@ -75,6 +75,7 @@ Copy `stb_image.h`, `json.hpp` and `tiny_gltf.h` to your project.
// Define these only in *one* .cc file. // Define these only in *one* .cc file.
#define TINYGLTF_IMPLEMENTATION #define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
// #define TINYGLTF_NOEXCEPTION // optional. disable exception handling.
#include "tiny_gltf.h" #include "tiny_gltf.h"
using namespace tinygltf; using namespace tinygltf;
@ -95,19 +96,42 @@ if (!ret) {
} }
``` ```
## Compile options
* `TINYGLTF_NOEXCEPTION` : Disable C++ exception handling. You can use `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION` to fully disable C++ exception codes.
* `TINGLTF_
### Saving gltTF 2.0 model ### Saving gltTF 2.0 model
T.B.W. T.B.W.
## Running tests. ## Running tests.
### Setup ### glTF parsing test
#### Setup
Python 2.6 or 2.7 required. Python 2.6 or 2.7 required.
Git clone https://github.com/KhronosGroup/glTF-Sample-Models to your local dir. Git clone https://github.com/KhronosGroup/glTF-Sample-Models to your local dir.
### Run test #### Run parsing test
After building `loader_example`, edit `test_runner.py`, then, After building `loader_example`, edit `test_runner.py`, then,
$ python test_runner.py $ python test_runner.py
### Unit tests
```
$ cd tests
$ make
$ ./tester
$ ./tester_noexcept
```
## Third party licenses
* json.hpp : Licensed under the MIT License <http://opensource.org/licenses/MIT>. Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
* stb_image : Public domain.
* catch : Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. Distributed under the Boost Software License, Version 1.0.
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

6
tests/Makefile Normal file
View File

@ -0,0 +1,6 @@
# Use this for strict compilation check(will work on clang 3.8+)
#EXTRA_CXXFLAGS := -fsanitize=address -Wall -Werror -Weverything -Wno-c++11-long-long -DTINYGLTF_APPLY_CLANG_WEVERYTHING
all: ../tiny_gltf.h
clang++ -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester tester.cc
clang++ -DTINYGLTF_NOEXCEPTION -I../ $(EXTRA_CXXFLAGS) -std=c++11 -g -O0 -o tester_noexcept tester.cc

10445
tests/catch.hpp Normal file

File diff suppressed because it is too large Load Diff

26
tests/tester.cc Normal file
View File

@ -0,0 +1,26 @@
#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "tiny_gltf.h"
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
TEST_CASE("parse-error", "[parse]") {
tinygltf::Model model;
tinygltf::TinyGLTF ctx;
std::string err;
bool ret = ctx.LoadASCIIFromString(&model, &err, "bora", strlen("bora"), /* basedir*/ "");
REQUIRE(false == ret);
}

View File

@ -2405,17 +2405,31 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, const char *str,
return false; return false;
} }
// TODO(syoyo): Add feature not using exception handling.
json v; json v;
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && not defined(TINYGLTF_NOEXCEPTION)
try { try {
v = json::parse(str, str + length); v = json::parse(str, str + length);
} catch (std::exception e) { } catch (const std::exception &e) {
if (err) { if (err) {
(*err) = e.what(); (*err) = e.what();
} }
return false; return false;
} }
#else
{
v = json::parse(str, str + length, nullptr, /* exception */false);
if (!v.is_object()) {
// Assume parsing was failed.
if (err) {
(*err) = "Failed to parse JSON object\n";
}
return false;
}
}
#endif
if (!v.is_object()) { if (!v.is_object()) {
// root is not an object. // root is not an object.