Skip to content

Commit a65eb14

Browse files
committed
add more tests
Signed-off-by: karan-palan <karanpalan007@gmail.com>
1 parent ebdaaa0 commit a65eb14

File tree

4 files changed

+271
-10
lines changed

4 files changed

+271
-10
lines changed

src/extension/alterschema/linter/else_false.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,34 @@ class ElseFalse final : public SchemaTransformRule {
99
const SchemaFrame &, const SchemaFrame::Location &,
1010
const SchemaWalker &, const SchemaResolver &) const
1111
-> SchemaTransformRule::Result override {
12-
return contains_any(
13-
vocabularies,
14-
{"https://json-schema.org/draft/2020-12/vocab/applicator",
15-
"https://json-schema.org/draft/2019-09/vocab/applicator",
16-
"http://json-schema.org/draft-07/schema#"}) &&
17-
schema.is_object() && schema.defines("if") &&
18-
schema.defines("else") && is_schema(schema.at("else")) &&
19-
schema.at("else").is_boolean() && !schema.at("else").to_boolean() &&
20-
(!schema.defines("then") ||
21-
(schema.at("then").is_boolean() && schema.at("then").to_boolean()));
12+
if (!contains_any(vocabularies,
13+
{"https://json-schema.org/draft/2020-12/vocab/applicator",
14+
"https://json-schema.org/draft/2019-09/vocab/applicator",
15+
"http://json-schema.org/draft-07/schema#"})) {
16+
return false;
17+
}
18+
19+
if (!schema.is_object() || !schema.defines("if") ||
20+
!schema.defines("else") || !is_schema(schema.at("else")) ||
21+
!schema.at("else").is_boolean() || schema.at("else").to_boolean() ||
22+
!is_schema(schema.at("if"))) {
23+
return false;
24+
}
25+
26+
if (schema.defines("then") &&
27+
(!schema.at("then").is_boolean() || !schema.at("then").to_boolean())) {
28+
return false;
29+
}
30+
31+
if (schema.at("if").is_object()) {
32+
for (const auto &entry : schema.at("if").as_object()) {
33+
if (schema.defines(entry.first)) {
34+
return false;
35+
}
36+
}
37+
}
38+
39+
return true;
2240
}
2341

2442
auto transform(JSON &schema) const -> void override {

test/alterschema/alterschema_lint_2019_09_test.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,3 +2437,84 @@ TEST(AlterSchema_lint_2019_09, else_false_4) {
24372437

24382438
EXPECT_EQ(document, expected);
24392439
}
2440+
2441+
TEST(AlterSchema_lint_2019_09, else_false_5) {
2442+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2443+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2444+
"type": "object",
2445+
"properties": {
2446+
"name": { "type": "string" }
2447+
},
2448+
"if": {
2449+
"properties": {
2450+
"name": { "type": "number" }
2451+
}
2452+
},
2453+
"else": false
2454+
})JSON");
2455+
2456+
LINT_AND_FIX_FOR_READABILITY(document);
2457+
2458+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2459+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2460+
"type": "object",
2461+
"properties": {
2462+
"name": { "type": "string" }
2463+
},
2464+
"if": {
2465+
"properties": {
2466+
"name": { "type": "number" }
2467+
}
2468+
},
2469+
"else": false
2470+
})JSON");
2471+
2472+
EXPECT_EQ(document, expected);
2473+
}
2474+
2475+
TEST(AlterSchema_lint_2019_09, else_false_6) {
2476+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2477+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2478+
"if": {
2479+
"properties": {
2480+
"flag": { "const": true }
2481+
}
2482+
},
2483+
"else": false
2484+
})JSON");
2485+
2486+
LINT_AND_FIX_FOR_READABILITY(document);
2487+
2488+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2489+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2490+
"properties": {
2491+
"flag": { "const": true }
2492+
}
2493+
})JSON");
2494+
2495+
EXPECT_EQ(document, expected);
2496+
}
2497+
2498+
TEST(AlterSchema_lint_2019_09, else_false_7) {
2499+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2500+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2501+
"if": {
2502+
"type": "object"
2503+
},
2504+
"then": false,
2505+
"else": false
2506+
})JSON");
2507+
2508+
LINT_AND_FIX_FOR_READABILITY(document);
2509+
2510+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2511+
"$schema": "https://json-schema.org/draft/2019-09/schema",
2512+
"if": {
2513+
"type": "object"
2514+
},
2515+
"then": false,
2516+
"else": false
2517+
})JSON");
2518+
2519+
EXPECT_EQ(document, expected);
2520+
}

test/alterschema/alterschema_lint_2020_12_test.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,3 +2671,84 @@ TEST(AlterSchema_lint_2020_12, else_false_4) {
26712671

26722672
EXPECT_EQ(document, expected);
26732673
}
2674+
2675+
TEST(AlterSchema_lint_2020_12, else_false_5) {
2676+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2677+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2678+
"type": "object",
2679+
"properties": {
2680+
"name": { "type": "string" }
2681+
},
2682+
"if": {
2683+
"properties": {
2684+
"name": { "type": "number" }
2685+
}
2686+
},
2687+
"else": false
2688+
})JSON");
2689+
2690+
LINT_AND_FIX_FOR_READABILITY(document);
2691+
2692+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2693+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2694+
"type": "object",
2695+
"properties": {
2696+
"name": { "type": "string" }
2697+
},
2698+
"if": {
2699+
"properties": {
2700+
"name": { "type": "number" }
2701+
}
2702+
},
2703+
"else": false
2704+
})JSON");
2705+
2706+
EXPECT_EQ(document, expected);
2707+
}
2708+
2709+
TEST(AlterSchema_lint_2020_12, else_false_6) {
2710+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2711+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2712+
"if": {
2713+
"properties": {
2714+
"flag": { "const": true }
2715+
}
2716+
},
2717+
"else": false
2718+
})JSON");
2719+
2720+
LINT_AND_FIX_FOR_READABILITY(document);
2721+
2722+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2723+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2724+
"properties": {
2725+
"flag": { "const": true }
2726+
}
2727+
})JSON");
2728+
2729+
EXPECT_EQ(document, expected);
2730+
}
2731+
2732+
TEST(AlterSchema_lint_2020_12, else_false_7) {
2733+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2734+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2735+
"if": {
2736+
"type": "object"
2737+
},
2738+
"then": false,
2739+
"else": false
2740+
})JSON");
2741+
2742+
LINT_AND_FIX_FOR_READABILITY(document);
2743+
2744+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2745+
"$schema": "https://json-schema.org/draft/2020-12/schema",
2746+
"if": {
2747+
"type": "object"
2748+
},
2749+
"then": false,
2750+
"else": false
2751+
})JSON");
2752+
2753+
EXPECT_EQ(document, expected);
2754+
}

test/alterschema/alterschema_lint_draft7_test.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,3 +1978,84 @@ TEST(AlterSchema_lint_draft7, else_false_4) {
19781978

19791979
EXPECT_EQ(document, expected);
19801980
}
1981+
1982+
TEST(AlterSchema_lint_draft7, else_false_5) {
1983+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
1984+
"$schema": "http://json-schema.org/draft-07/schema#",
1985+
"type": "object",
1986+
"properties": {
1987+
"name": { "type": "string" }
1988+
},
1989+
"if": {
1990+
"properties": {
1991+
"name": { "type": "number" }
1992+
}
1993+
},
1994+
"else": false
1995+
})JSON");
1996+
1997+
LINT_AND_FIX_FOR_READABILITY(document);
1998+
1999+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2000+
"$schema": "http://json-schema.org/draft-07/schema#",
2001+
"type": "object",
2002+
"properties": {
2003+
"name": { "type": "string" }
2004+
},
2005+
"if": {
2006+
"properties": {
2007+
"name": { "type": "number" }
2008+
}
2009+
},
2010+
"else": false
2011+
})JSON");
2012+
2013+
EXPECT_EQ(document, expected);
2014+
}
2015+
2016+
TEST(AlterSchema_lint_draft7, else_false_6) {
2017+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2018+
"$schema": "http://json-schema.org/draft-07/schema#",
2019+
"if": {
2020+
"properties": {
2021+
"flag": { "const": true }
2022+
}
2023+
},
2024+
"else": false
2025+
})JSON");
2026+
2027+
LINT_AND_FIX_FOR_READABILITY(document);
2028+
2029+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2030+
"$schema": "http://json-schema.org/draft-07/schema#",
2031+
"properties": {
2032+
"flag": { "const": true }
2033+
}
2034+
})JSON");
2035+
2036+
EXPECT_EQ(document, expected);
2037+
}
2038+
2039+
TEST(AlterSchema_lint_draft7, else_false_7) {
2040+
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
2041+
"$schema": "http://json-schema.org/draft-07/schema#",
2042+
"if": {
2043+
"type": "object"
2044+
},
2045+
"then": false,
2046+
"else": false
2047+
})JSON");
2048+
2049+
LINT_AND_FIX_FOR_READABILITY(document);
2050+
2051+
const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
2052+
"$schema": "http://json-schema.org/draft-07/schema#",
2053+
"if": {
2054+
"type": "object"
2055+
},
2056+
"then": false,
2057+
"else": false
2058+
})JSON");
2059+
2060+
EXPECT_EQ(document, expected);
2061+
}

0 commit comments

Comments
 (0)