44
55using c4::csubstr;
66using c4::substr;
7+ using ryml::Location;
8+
79
810namespace c4
911{
@@ -94,39 +96,54 @@ size_t expected_size(std::vector<EvtWithScalar> const& evt)
9496 return exp;
9597}
9698
99+ #define _runtest (name, ...) \
100+ do { \
101+ printf (" [ RUN ] %s ... \n " , #name); \
102+ TestResult tr_ = name (__VA_ARGS__); \
103+ tr.add (tr_); \
104+ printf (" [ %s ] %s\n " , tr_?" OK " :" FAIL" , #name); \
105+ } while (0 )
106+
107+ #define CHECK (cond ) \
108+ do { \
109+ bool pass = !!(cond); \
110+ ++tr.num_assertions ; \
111+ if (!pass) { \
112+ printf (" %s:%d: fail! %s\n " , __FILE__, __LINE__, #cond); \
113+ ++tr.num_failed_assertions ; \
114+ } \
115+ } while (0 )
116+
117+ #define CHECK_EQ (lhs, rhs ) \
118+ do { \
119+ bool pass = !!(lhs == rhs); \
120+ ++tr.num_assertions ; \
121+ if (!pass) { \
122+ std::string slhs = c4::catrs<std::string>(lhs); \
123+ std::string srhs = c4::catrs<std::string>(rhs); \
124+ printf (" %s:%d: fail! %s=%s == %s=%s\n " , __FILE__, __LINE__, #lhs, slhs.c_str (), #rhs, srhs.c_str ()); \
125+ ++tr.num_failed_assertions ; \
126+ } \
127+ } while (0 )
128+
129+ #define CHECK_MSG (cond, fmt, ...) \
130+ do { \
131+ bool pass = !!(cond); \
132+ ++tr.num_assertions ; \
133+ if (!pass) { \
134+ printf (" %s:%d: fail! %s:" fmt " \n " , __FILE__, __LINE__, #cond, ## __VA_ARGS__); \
135+ ++tr.num_failed_assertions ; \
136+ } \
137+ } while (0 )
138+
139+
97140struct TestCase
98141{
99142 csubstr ys;
100143 std::vector<EvtWithScalar> evt;
101144
102145public:
103146
104- #define _runtest (name, ...) \
105- do { \
106- printf (" [ RUN ] %s ... \n " , #name); \
107- TestResult tr_ = name (__VA_ARGS__); \
108- tr.add (tr_); \
109- printf (" [ %s ] %s\n " , tr_?" OK " :" FAIL" , #name); \
110- } while (0 )
111- #define CHECK (cond ) \
112- do { \
113- bool pass = !!(cond); \
114- ++tr.num_assertions ; \
115- if (!pass) { \
116- printf (" %s:%d: fail! %s\n " , __FILE__, __LINE__, #cond); \
117- ++tr.num_failed_assertions ; \
118- } \
119- } while (0 )
120- #define CHECK_MSG (cond, fmt, ...) \
121- do { \
122- bool pass = !!(cond); \
123- ++tr.num_assertions ; \
124- if (!pass) { \
125- printf (" %s:%d: fail! %s:" fmt " \n " , __FILE__, __LINE__, #cond, ## __VA_ARGS__); \
126- ++tr.num_failed_assertions ; \
127- } \
128- } while (0 )
129-
130147 TestResult test (ysparse *ryml2evt) const
131148 {
132149 TestResult tr = {};
@@ -298,6 +315,65 @@ struct TestCase
298315};
299316
300317
318+ // -----------------------------------------------------------------------------
319+
320+ struct TestCaseErr
321+ {
322+ csubstr ys;
323+ bool is_parse_err;
324+ ryml::Location loc;
325+
326+ TestCaseErr (csubstr ys_) : ys(ys_), is_parse_err(false ), loc() {}
327+ TestCaseErr (csubstr ys_, ryml::Location loc_) : ys(ys_), is_parse_err(true ), loc(loc_) {}
328+
329+ TestResult test (ysparse *ryml2evt) const
330+ {
331+ TestResult tr = {};
332+ _runtest (test_err, );
333+ _runtest (test_err_reuse, ryml2evt);
334+ return tr;
335+ }
336+
337+ TestResult test_err_reuse (ysparse *ryml2evt) const
338+ {
339+ TestResult tr = {};
340+ std::string input_ (ys.begin (), ys.end ());
341+ substr input = c4::to_substr (input_);
342+ bool gotit = false ;
343+ try
344+ {
345+ size_type reqsize = ysparse_parse (ryml2evt, " ysfilename" ,
346+ input.str , (size_type)input.len ,
347+ nullptr , 0 );
348+ }
349+ catch (YsParseError const & exc)
350+ {
351+ if (is_parse_err)
352+ {
353+ gotit = true ;
354+ CHECK_EQ (exc.location .name , " ysfilename" );
355+ CHECK_EQ (exc.location .line , loc.line );
356+ CHECK_EQ (exc.location .col , loc.col );
357+ CHECK_EQ (exc.location .offset , loc.offset );
358+ }
359+ }
360+ catch (std::exception const & exc)
361+ {
362+ if (!is_parse_err)
363+ gotit = true ;
364+ }
365+ CHECK (gotit);
366+ return tr;
367+ return tr;
368+ }
369+ TestResult test_err () const
370+ {
371+ Ys2EvtScoped lib;
372+ return test_err_reuse (lib.ryml2evt );
373+ }
374+ };
375+
376+
301377// -----------------------------------------------------------------------------
302378
303379namespace {
@@ -308,6 +384,20 @@ using namespace evt;
308384inline constexpr bool needs_filter = true ;
309385const TestCase test_cases[] = {
310386 // case -------------------------------------------------
387+ tc (" !yamlscript/v0/bare\n --- !code\n 42\n " ,
388+ {
389+ e (BSTR),
390+ e (BDOC),
391+ e (VAL_|TAG_, 1 , 18 , " yamlscript/v0/bare" ),
392+ e (VAL_|SCLR|PLAI, 0 , 0 , " " ),
393+ e (EDOC),
394+ e (BDOC|EXPL),
395+ e (VAL_|TAG_, 25 , 4 , " code" ),
396+ e (VAL_|SCLR|PLAI, 30 , 2 , " 42" ),
397+ e (EDOC),
398+ e (ESTR),
399+ }),
400+ // case -------------------------------------------------
311401 tc (" a: 1" ,
312402 {
313403 e (BSTR),
@@ -568,8 +658,16 @@ defn run(prompt session=nil):
568658 e (ESTR),
569659 }),
570660};
661+ #define tcf (...) TestCaseErr(__VA_ARGS__)
662+ const TestCaseErr test_cases_err[] = {
663+ tcf (" - !!str, xxx\n " , Location (13 , 2 , 1 )),
664+ // FIXME tcf(": : : :", Location(2, 1, 3)),
665+ };
571666} // namespace
572667
668+
669+ // -----------------------------------------------------------------------------
670+
573671int main (int argc, const char *argv[])
574672{
575673 for (int i = 1 ; i < argc; ++i)
@@ -592,9 +690,20 @@ int main(int argc, const char *argv[])
592690 failed_cases += (!tr);
593691 printf (" case %zu/%zu: %s\n " , i, C4_COUNTOF (test_cases), tr ? " ok!" : " failed" );
594692 }
693+ size_t num_cases_err = C4_COUNTOF (test_cases_err);
694+ for (size_t i = 0 ; i < C4_COUNTOF (test_cases_err); ++i)
695+ {
696+ printf (" -----------------------------------------\n "
697+ " errcase %zu/%zu ...\n "
698+ " [%zu]~~~%.*s~~~\n " , i, num_cases_err, test_cases_err[i].ys .len , (int )test_cases_err[i].ys .len , test_cases_err[i].ys .str );
699+ const TestResult tr = test_cases_err[i].test (ys2evt.ryml2evt );
700+ total.add (tr);
701+ failed_cases += (!tr);
702+ printf (" case %zu/%zu: %s\n " , i, C4_COUNTOF (test_cases), tr ? " ok!" : " failed" );
703+ }
595704 printf (" assertions: %u/%u pass %u/%u fail\n " , total.num_assertions - total.num_failed_assertions , total.num_assertions , total.num_failed_assertions , total.num_assertions );
596705 printf (" tests: %u/%u pass %u/%u fail\n " , total.num_tests - total.num_failed_tests , total.num_tests , total.num_failed_tests , total.num_tests );
597- printf (" cases: %zu/%zu pass %zu/%zu fail\n " , num_cases-failed_cases, num_cases, failed_cases, num_cases);
706+ printf (" cases: %zu/%zu pass %zu/%zu fail\n " , num_cases-failed_cases, num_cases+num_cases_err , failed_cases, num_cases);
598707 if (total)
599708 printf (" TESTS SUCCEED!\n " );
600709 return total ? 0 : -1 ;
0 commit comments