File: tst-python.c

package info (click to toggle)
libeconf 0.7.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,644 kB
  • sloc: ansic: 7,483; python: 1,204; cs: 651; sh: 214; xml: 182; makefile: 7
file content (137 lines) | stat: -rw-r--r-- 3,789 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include "libeconf_ext.h"

/* Test case:
 *  Test python style:
 *  - no comment in the same line
 *  - mulitline is represented by identations
 */

static int
print_error_get (const char *key, econf_err error)
{
  fprintf (stderr, "ERROR: tried to get '%s': %s\n",
	   key, econf_errString(error));
  return 1;
}

static bool
check (econf_file *key_file, const char *key,
       const char *value, const char *comment_before_key)
{
  econf_err error;
  econf_ext_value *ext_val;
  char *val_String;

  if ((error = econf_getExtValue(key_file, NULL, key, &ext_val)))
  {
    print_error_get (key, error);
    econf_freeExtValue(ext_val);
    return false;
  }

  if ((error = econf_getStringValue(key_file, NULL, key, &val_String)))
  {
    print_error_get (key, error);
    return false;
  }  

  if ((val_String != NULL && value == NULL) ||
      (val_String == NULL && value != NULL) ||
      (val_String != NULL && value != NULL &&
       strcmp(val_String, value) != 0))
  {
    fprintf (stderr, "ERROR: %s\nExpected String:\n'%s'\nGot:\n'%s'\n",
	     key, value, val_String);
    econf_freeExtValue(ext_val);
    free(val_String);
    return false;
  }

  free(val_String);

  if ((ext_val->comment_before_key != NULL && comment_before_key == NULL) ||
      (ext_val->comment_before_key == NULL && comment_before_key != NULL) ||
      (ext_val->comment_before_key != NULL && comment_before_key != NULL &&
       strcmp(ext_val->comment_before_key, comment_before_key) != 0))
  {
    fprintf (stderr, "ERROR: %s\nExpected String:\n'%s'\nGot:\n'%s'\n",
	     key, comment_before_key, ext_val->comment_before_key);
    econf_freeExtValue(ext_val);
    return false;
  }

  if (ext_val->comment_after_value != NULL)
  {
    fprintf (stderr, "ERROR: %s\nNo comment_after_value expected\nGot:\n'%s'\n",
	     key, ext_val->comment_after_value);
    econf_freeExtValue(ext_val);
    return false;
  }
  
  econf_freeExtValue(ext_val);
  return true;
}

int
main(void)
{
  econf_file *key_file = NULL;
  econf_err error;
  int retval = 0;

  static const struct {
    const char *const key;
    const char *const value;	  
    const char *const comment_before_key;
  } tests[] = {
    { "none_comment", "1", NULL},
    { "none_comment_with_delim", "1=test", NULL},
    { "header_single", "string with spaces", " header_single_line test"},
    { "header_single_with_delim", "string with spaces = 3", " header_single_line test with delim"},
    { "header_with_value", "string with spaces #value description", " header_with_value test"},
    { "multiline_header", "multiline header", " line 1\n line 2\n\n line 3"},
    { "multiline_entry", "line one # line 1\nline two # line 2", " header multiline"},
    { "multiline_entry_with_delim", "\nline one # line 1\nline two=\nline three= # line 3", NULL},
  };
  unsigned int i;

  error = econf_newKeyFile_with_options(&key_file, "PYTHON_STYLE=1");
  if (error != ECONF_SUCCESS)
  {
    fprintf (stderr, "ERROR: couldn't create new key_file: %s\n",
             econf_errString(error));
    return 1;
  }

  error = econf_readConfig (&key_file,
			    NULL,
			    TESTSDIR"tst-python-data",
			    "arguments", "conf",
			    "=", "#");
  if (error)
  {
    fprintf (stderr, "ERROR: couldn't read configuration file: %s\n", econf_errString(error));
    char *filename;
    uint64_t line_nr;
    econf_errLocation (&filename, &line_nr);
    fprintf (stderr, "ERROR: in file: %s:%ld\n", filename, line_nr);
    free(filename);
    return 1;
  }

  for (i = 0; i < sizeof(tests)/sizeof(*tests); i++)
  {
    if (!check(key_file, tests[i].key, tests[i].value, tests[i].comment_before_key))
      retval = 1;
  }

  econf_free(key_file);
  return retval;  
}