LLOOP Index | GSP Language | GSP Library | Framework Classes | Component Classes

testini.gsp

This is the verbatim text of the file "testini.gsp" part of the LLOOP package. The copyright remains with Michel MEHL. All rights reserved.



import urlfile;        

/* ------------------------------------------------------------------------- */

symbol TestIniFile_1   
  include "IniFile.h"
  test 
  {{
    "Modifying existing ini file 'conf.cfg'" : pass "file://examples/conf.cfg" file://examples/conf_expected.cfg
  }};

symbol TestIniFile_2
  include "IniFile.h"
  test 
  {{
    "Creating ini file 'new.ini' from scratch" : pass "file://examples/new.ini" file://examples/new_expected.ini
  }};

/* ------------------------------------------------------------------------- */

/**
 * TestIniFile 1
 *
 * Symbol responsible for the Ini File Component Testing
 */

TestIniFile_1 ::= urlfile
{{
  using namespace universal;

  // ## Start of parse code
  // ##

  String sFilename = $1.name();
  File($1.name()+".sav").remove();

  cout << "Reading ini file "<< sFilename  << endl; cout.flush();
  IniFile iniFile(sFilename);

  if (iniFile.fail())
    {
      iniFile.getError(cerr);
      cerr << endl;

      fatal("%s: failed to read ini file.\n", iniFile.filename().str());
    }

  // ## Get some data of the ini file
  // ## 

  cout << ".Get Test: " << endl; cout.flush();
  cout << "General.Path=" << iniFile.get("General","Path") << endl;
  cout << "General.Name=" << iniFile.get("General","Name") << endl;
  cout << "General.Invalid=" << iniFile.get("General","Invalid") << endl;
  cout << "TestSection1.TestData1=" << iniFile.get("TestSection1","TestData1") << endl;
  cout << "TestSection1.TestData2=" << iniFile.get("TestSection1","TestData2") << endl;
  cout << "TestSection1.TestData3=" << iniFile.get("TestSection1","TestData3") << endl;
  cout << "TestSection1.TestData4=" << iniFile.get("TestSection1","TestData4") << endl;
  cout << "TestSection1.TestData5=" << iniFile.get("TestSection1","TestData5") << endl;
  cout << "TestSection1.TestData6=" << iniFile.get("TestSection1","TestData6") << endl;
  cout << "TestSection3.Dummy2=" << iniFile.get("TestSection3","Dummy2") << endl;
  cout << "TestSection6.Dummy2=" << iniFile.get("TestSection6","Dummy2") << endl;  
  cout << "TestSection7.Dummy2=" << iniFile.get("TestSection7","Dummy2") << endl;

  // ## Modify some data of the ini file
  // ## 

  cout << ".Modify Test: " << endl; cout.flush();
  iniFile.get("General","Path") = "/this is mymodifed path";
  iniFile.get("TestSection1","TestData3") ="new data for TestData3 of TestSection1";
  iniFile.get(1) ="TestSection1Renamed";

  // ## Add some data of the ini file
  // ## 

  cout << ".Insert Test1: " << endl; cout.flush();
  if (!iniFile.insert("This is a new section","This_is_the_new_data", "This is the value of the new data"))
    cerr << "error: insertion 1 failed! " << endl;

  cout << ".Insert Test2:" << endl; cout.flush();
  if (!iniFile.insert("This is a new section","This_is_another_new_data", "This is the value of the other new data!"))
    cerr << "error: insertion 2 failed! " << endl;

  cout << ".Insert Test3: " << endl; cout.flush();
  if (!iniFile.insert("General","new_data", "new data value!"))
    cerr << "error: insertion 3 failed! " << endl;

  cout << ".Insert Test3b: " << endl; cout.flush();
  if (!iniFile.insert("General","new_data2", "new data value2!"))
    cerr << "error: insertion 3b failed! " << endl;

  cout << ".Insert Test4: " << endl; cout.flush();
  if (iniFile.insert("General","Path", "should not work!"))
    cerr << "error: insertion 4 worked but should have failed! " << endl;

  cout << ".Insert Test5: " << endl; cout.flush();
  if (iniFile.insert("General","Bad Data Path ", "Test 5 shouldn't work because of a bad data ! OK ?"))
    cerr << "error: insertion 5 worked but should have failed! " << endl;

  /*
    cout << ".Insert Test6 (insert variant): " << endl; cout.flush();
    if (!iniFile.insert2("General","Data_insert_variant", "OK"))
    cerr << "error: insertion 6 failed! " << endl;    
    cout << ".Insert Test7 (insert variant): " << endl; cout.flush();
    if (!iniFile.insert2("Section_insert_variant","Section_insert_variant", "OK ?"))
    cerr << "error: insertion 7 failed! " << endl;
    */

  cout << ".Insert Test8: " << endl; cout.flush();
  if (!iniFile.insert("General","other_new_data", "new data value!"))
    cerr << "error: insertion 8 failed! " << endl;

  // ## Save the changes in another file
  // ## 

  sFilename = sFilename+".sav";
  if (!iniFile.save(sFilename))
    fatal("Save failure!"); 
  else
    cout << "Content of file was saved into " << sFilename << endl;

  // Position to end of stream so that parsing is succesful
  $0.stream().seekg(0, ios::end);
}}
expand
{{
  using namespace universal;

  File resultFile($1.name()+".sav");
  String sResultFileContent;

  resultFile.read(sResultFileContent);
  os << sResultFileContent;
}}


/**
 * TestIniFile 2
 *
 * Symbol responsible for the Ini File Component Testing
 */

TestIniFile_2 ::= urlfile
{{
  using namespace universal;

  // ## Create a new ini file
  // ## 

  cout << "Creating ini file new.ini" << endl; cout.flush();

  IniFile newIniFile($1.name(), true);

  if (newIniFile.fail())
    {
      newIniFile.getError(cerr);
      cerr << endl;

      fatal("%s: failed to create ini file.", newIniFile.filename().str());
    }

  if (!newIniFile.insert("new_inifile_section","data1", "Hello"))
    cerr << "error: insertion 1 in new ini file object failed! " << endl;

  if (!newIniFile.insert("new_inifile_section","data2", "Bonjour"))
    cerr << "error: insertion 2 in new ini file object failed! " << endl;

  if (!newIniFile.insert("new_inifile_section2","data1", "Guten Tag"))
    cerr << "error: insertion 3 in new ini file object failed! " << endl;

  // ## Save the changes in another file
  // ## 

  if (!newIniFile.save())
  {
    fatal("Saving of new ini file failed !");
  }

  // Position to end of stream so that parsing is succesful
  $0.stream().seekg(0, ios::end);
}}

expand
{{
  using namespace universal;

  File resultFile($1.name());
  String sResultFileContent;

  resultFile.read(sResultFileContent);
  os << sResultFileContent;
}}


This file is part of the LLOOP Reversible Object-Oriented Parser Generator. Copyright (c) 2005-2006 Michel MEHL, France. All rights reserved. LLOOP is distributed by the company ERSA SaRL.


Copyright (c) 2005-2006 Michel MEHL, Haguenau, France
LLOOP version 1.1