Skip to content

Commit a41fa9e

Browse files
committed
Write tests for all known types of error conditions
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1136d92 commit a41fa9e

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

src/ir/ir_default_compiler.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if (!allowed.contains(entry.first)) { \
2020
throw sourcemeta::codegen::UnsupportedKeywordError( \
2121
(schema), (pointer), entry.first, \
22-
"Unexpected keyword in subschema"); \
22+
"Unsupported keyword in subschema"); \
2323
} \
2424
} \
2525
}
@@ -497,8 +497,7 @@ auto default_compiler(const sourcemeta::core::JSON &schema,
497497
return handle_ref(schema, frame, location, vocabularies, resolver,
498498
subschema);
499499
} else {
500-
throw UnexpectedSchemaError(schema, location.pointer,
501-
"Unsupported subschema");
500+
throw UnexpectedSchemaError(schema, location.pointer, "Unsupported schema");
502501
}
503502
}
504503

test/ir/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sourcemeta_googletest(NAMESPACE sourcemeta PROJECT codegen NAME ir
22
FOLDER "Codegen/IR"
3-
SOURCES ir_2020_12_test.cc ir_symbol_test.cc ir_test_utils.h)
3+
SOURCES ir_test.cc ir_2020_12_test.cc ir_symbol_test.cc ir_test_utils.h)
44

55
target_link_libraries(sourcemeta_codegen_ir_unit
66
PRIVATE sourcemeta::codegen::ir)

test/ir/ir_test.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <sourcemeta/codegen/ir.h>
4+
#include <sourcemeta/core/jsonschema.h>
5+
6+
TEST(IR, unsupported_dialect_draft3) {
7+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
8+
"$schema": "http://json-schema.org/draft-03/schema#",
9+
"type": "string"
10+
})JSON")};
11+
12+
EXPECT_THROW(
13+
sourcemeta::codegen::compile(schema, sourcemeta::core::schema_walker,
14+
sourcemeta::core::schema_resolver,
15+
sourcemeta::codegen::default_compiler),
16+
sourcemeta::core::SchemaVocabularyError);
17+
}
18+
19+
TEST(IR, unsupported_keyword_error) {
20+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
21+
"$schema": "https://json-schema.org/draft/2020-12/schema",
22+
"type": "string",
23+
"if": { "minLength": 5 },
24+
"then": { "maxLength": 10 }
25+
})JSON")};
26+
27+
EXPECT_THROW(
28+
sourcemeta::codegen::compile(schema, sourcemeta::core::schema_walker,
29+
sourcemeta::core::schema_resolver,
30+
sourcemeta::codegen::default_compiler),
31+
sourcemeta::codegen::UnsupportedKeywordError);
32+
}
33+
34+
TEST(IR, unsupported_keyword_value_error_type_not_string) {
35+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
36+
"$schema": "https://json-schema.org/draft/2020-12/schema",
37+
"type": 123
38+
})JSON")};
39+
40+
EXPECT_THROW(
41+
sourcemeta::codegen::compile(schema, sourcemeta::core::schema_walker,
42+
sourcemeta::core::schema_resolver,
43+
sourcemeta::codegen::default_compiler),
44+
sourcemeta::codegen::UnsupportedKeywordValueError);
45+
}
46+
47+
TEST(IR, unsupported_keyword_value_error_unknown_type) {
48+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
49+
"$schema": "https://json-schema.org/draft/2020-12/schema",
50+
"type": "foo"
51+
})JSON")};
52+
53+
EXPECT_THROW(
54+
sourcemeta::codegen::compile(schema, sourcemeta::core::schema_walker,
55+
sourcemeta::core::schema_resolver,
56+
sourcemeta::codegen::default_compiler),
57+
sourcemeta::codegen::UnsupportedKeywordValueError);
58+
}
59+
60+
TEST(IR, unexpected_schema_error_unsupported_shape) {
61+
const sourcemeta::core::JSON schema{sourcemeta::core::parse_json(R"JSON({
62+
"$schema": "https://json-schema.org/draft/2020-12/schema",
63+
"not": { "type": "string" }
64+
})JSON")};
65+
66+
EXPECT_THROW(
67+
sourcemeta::codegen::compile(schema, sourcemeta::core::schema_walker,
68+
sourcemeta::core::schema_resolver,
69+
sourcemeta::codegen::default_compiler),
70+
sourcemeta::codegen::UnexpectedSchemaError);
71+
}

0 commit comments

Comments
 (0)