From 81355840dfdd4dffad7b37a973e59ef8dd279362 Mon Sep 17 00:00:00 2001 From: John Mettraux Date: Wed, 15 Jul 2015 06:17:00 +0900 Subject: [PATCH] add specs for altg vs -1 initially meant for gh-13 --- spec/fabr_alt_spec.c | 4 +--- spec/fabr_altg_spec.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/spec/fabr_alt_spec.c b/spec/fabr_alt_spec.c index c6d7440..2fa385f 100644 --- a/spec/fabr_alt_spec.c +++ b/spec/fabr_alt_spec.c @@ -27,9 +27,7 @@ describe "fabr_alt()" static fabr_tree *_talterr(fabr_input *i) { fabr_tree *r = calloc(1, sizeof(fabr_tree)); - r->result = -1; - r->parter = "_talterr"; - r->offset = i->offset; + r->result = -1; r->parter = "_talterr"; r->offset = i->offset; return r; } diff --git a/spec/fabr_altg_spec.c b/spec/fabr_altg_spec.c index bcf75a8..1dc5d39 100644 --- a/spec/fabr_altg_spec.c +++ b/spec/fabr_altg_spec.c @@ -25,6 +25,14 @@ describe "fabr_altg()" static fabr_tree *_x2(fabr_input *i) { return fabr_str("x2", i, "xx"); } static fabr_tree *_3x(fabr_input *i) { return fabr_str("3x", i, "xxx"); } + static fabr_tree *_xerr(fabr_input *i) + { + fabr_tree *r = calloc(1, sizeof(fabr_tree)); + r->result = -1; r->parter = "_xerr"; r->offset = i->offset; + + return r; + } + context "greedy=0" { it "takes the first succesful result" @@ -117,6 +125,29 @@ describe "fabr_altg()" " [ \"2x\", 1, 0, 2, null, \"str\", 2, \"xx\" ]\n" "] ]"); } + it "propagates -1 (error)" + { + i.string = "xx"; + t = fabr_altg("e", &i, _2x, _1x, _xerr, NULL); + + ensure(fabr_tree_to_string(t, i.string, 0) ===f "" + "[ \"e\", -1, 0, 0, null, \"altg\", 0, [\n" + " [ \"2x\", 1, 0, 2, null, \"str\", 2, \"xx\" ],\n" + " [ \"1x\", 0, 0, 1, null, \"str\", 1, [] ],\n" + " [ null, -1, 0, 0, null, \"_xerr\", 0, [] ]\n" + "] ]"); + } + + it "propagates -1 (error) immediately" + { + i.string = "xx"; + t = fabr_altg("e", &i, _xerr, _2x, _1x, NULL); + + ensure(fabr_tree_to_string(t, i.string, 0) ===f "" + "[ \"e\", -1, 0, 0, null, \"altg\", 0, [\n" + " [ null, -1, 0, 0, null, \"_xerr\", 0, [] ]\n" + "] ]"); + } } }