feisty meow concerns codebase  2.140
test_ini_parser.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_ini_parser *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 1991-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
17 #include <basis/astring.h>
18 #include <basis/functions.h>
19 #include <basis/guards.h>
21 #include <filesystem/byte_filer.h>
22 #include <loggers/console_logger.h>
27 #include <textual/parser_bits.h>
28 #include <unit_test/unit_base.h>
29 
30 using namespace application;
31 using namespace basis;
32 using namespace configuration;
33 using namespace filesystem;
34 using namespace loggers;
35 using namespace structures;
36 using namespace textual;
37 using namespace unit_test;
38 
39 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
40 
41 const astring INI_FILE_1 = "\
42 [bork]\n\
43 norple=1\n\
44 train=12.5\n\
45 singhy=9 \r\n\
46 \n\
47 [twerf]\r\n\
48 noodles=fungus\n\
49 dora=34\n\
50 ";
51 
52 //#define RUN_READ_FILE_TEST
53  // uncomment to read in a file and parse it.
54 
55 class test_ini_parser : virtual public unit_base, virtual public application_shell
56 {
57 public:
58  test_ini_parser() : application_shell() {}
59  DEFINE_CLASS_NAME("test_ini_parser");
60  virtual int execute();
61 };
62 
63 int test_ini_parser::execute()
64 {
65  FUNCDEF("execute")
66  program_wide_logger::get().eol(parser_bits::NO_ENDING);
67 
69 
70 //astring dump;
71 //par.restate(dump);
72 //LOG(astring("table has:\n") + dump);
73 
74  string_table twerf;
75  if (!par.get_section("twerf", twerf))
76  deadly_error(class_name(), "get_section 1", "twerf section was not found");
77 //LOG(astring("twerf section is: ") + twerf.text_form());
78  if (!twerf.find("noodles"))
79  deadly_error(class_name(), "get_section 1", "item #1 was not found");
80  if (*twerf.find("noodles") != astring("fungus"))
81  deadly_error(class_name(), "get_section 1", "item #1 found is incorrect");
82  if (!twerf.find("dora"))
83  deadly_error(class_name(), "get_section 1", "item #2 was not found");
84  if (*twerf.find("dora") != astring("34"))
85  deadly_error(class_name(), "get_section 1", "item #2 found is incorrect");
86 
87  string_table bork;
88  if (!par.get_section("bork", bork))
89  deadly_error(class_name(), "get_section 2", "bork section was not found");
90  if (!bork.find("norple"))
91  deadly_error(class_name(), "get_section 2", "item #1 was not found");
92  if (*bork.find("norple") != astring("1"))
93  deadly_error(class_name(), "get_section 2", "item #1 found is incorrect");
94  if (!bork.find("train"))
95  deadly_error(class_name(), "get_section 2", "item #2 was not found");
96  if (*bork.find("train") != astring("12.5"))
97  deadly_error(class_name(), "get_section 2", "item #2 found is incorrect");
98  if (!bork.find("singhy"))
99  deadly_error(class_name(), "get_section 2", "item #3 was not found");
100  if (*bork.find("singhy") != astring("9"))
101  deadly_error(class_name(), "get_section 2", "item #3 found is incorrect");
102 
103  astring new_ini;
104  par.restate(new_ini);
105 
106  program_wide_logger::get().eol(parser_bits::CRLF_AT_END);
107  LOG("");
108 
109 //hmmm: this could be a useful additional feature if it weren't a hard-coded filename;
110 // make this test take command line parameters which are interpreted as files to process here.
111 // loop over all of them and do the test.
112 #ifdef RUN_READ_FILE_TEST
113  byte_filer input("c:/home/fungal.lld", "rb");
114  int len = input.length();
115  LOG(a_sprintf("fungal len is %d", len));
116  astring jojo;
117  input.read(jojo, len);
118  //LOG("whole file is:");
119  //LOG(jojo);
120 
121  ini_parser klug(jojo);
122  astring dump2;
123  klug.restate(dump2);
124  LOG(dump2);
125 #endif
126 
127  critical_events::alert_message(astring(class_name()) + ": works for those functions tested.");
128 
129  return 0;
130 }
131 
132 HOOPLE_MAIN(test_ini_parser, )
133 
The application_shell is a base object for console programs.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
Parses strings in the fairly well-known INI file format.
Definition: ini_parser.h:74
A class that provides logging facilities vertically to all of hoople.
Provides a symbol_table that holds strings as the content.
Definition: string_table.h:32
Warehouses some functions that are often useful during text parsing.
Definition: parser_bits.h:25
#define deadly_error(c, f, i)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
Definition: enhance_cpp.h:42
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
A platform independent way to obtain the timestamp of a file.
Definition: byte_filer.cpp:37
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
const astring INI_FILE_1