source: trunk/test/copyright.cc @ 596

Last change on this file since 596 was 596, checked in by Peter Johansson, 13 years ago

added testlib to share code between different tests

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: copyright.cc 596 2008-04-14 21:54:10Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Suite.h"
25
26#include "Configuration.h"
27#include "File.h"
28#include "SVN.h"
29#include "SVNinfo.h"
30
31#include <cassert>
32#include <iostream>
33#include <fstream>
34#include <string>
35#include <vector>
36
37std::vector<std::string> copyright_lines(std::istream&);
38
39int main( int argc, char* argv[])
40{
41  using namespace theplu::svndigest;
42  bool verbose=false;
43  bool ok=true;
44  if (argc>1 && argv[1]==std::string("-v") )
45    verbose=true;
46
47  std::string root=test::filename("toy_project");
48  std::string filename = root + "/README";
49
50  // Saving original file
51  std::ifstream is(filename.c_str());
52  assert(is.good());
53  std::string original_file;
54  std::getline(is, original_file, '\0');
55  is.close();
56  is.clear(std::ios::goodbit);
57
58  is.open(filename.c_str());
59  std::vector<std::string> copyrights_old=copyright_lines(is);
60  is.close();
61  is.clear(std::ios::goodbit);
62
63  if (copyrights_old.size()!=1) {
64    if (verbose){
65      std::cout << copyrights_old.size() << " Copyright lines\n";
66      for (size_t i=0; i<copyrights_old.size(); ++i)
67        std::cout << copyrights_old[i] << "\n";
68    }
69    ok = false;
70  }
71  else if (verbose)
72    std::cout << "File containes 1 copyright line.\n"; 
73
74  if (verbose)
75    std::cout << "Create SVN instance" << std::endl;
76  SVN* svn=SVN::instance(root);
77  if (!svn)
78    return 1;
79
80  // Extract repository location
81  if (verbose)
82    std::cout << "Extract repository location" << std::endl;
83  std::string repo=SVNinfo(root).repos_root_url();
84  if (verbose)
85    std::cout << "Create File object" << std::endl;
86  File file(0,filename,"");
87 
88  if (verbose)
89    std::cout << "Get stats for file" << std::endl;
90  file.parse(verbose, true);
91 
92  if (verbose)
93    std::cout << "Updating copyright statements" << std::endl;
94  std::map<std::string, Alias> alias;
95  file.print_copyright(alias, verbose);
96
97  is.open(filename.c_str());
98  std::vector<std::string> copyrights=copyright_lines(is);
99  is.close();
100  is.clear(std::ios::goodbit);
101
102  if (copyrights.size()!=2) {
103    ok = false;
104    if (verbose)
105      std::cout << "ERROR: expected two lines of Copyright (C)\n";
106  }
107  if (verbose) {
108    std::cout << copyrights.size() << " Copyright lines:\n";
109    for (size_t i=0; i<copyrights.size(); ++i)
110      std::cout << i+1 << ": " << copyrights[i] << "\n";
111  }
112
113  // Restoring file
114  std::ofstream os(filename.c_str());
115  assert(os.good());
116  os << original_file;
117
118  if (ok) {
119    if (verbose)
120      std::cout << "Test is Ok!" << std::endl;
121    return 0;
122  }
123  if (verbose)
124    std::cout << "Test failed." << std::endl;
125  return 1;
126}
127
128std::vector<std::string> copyright_lines(std::istream& is)
129{
130  using namespace theplu::svndigest;
131  std::vector<std::string> res;
132  std::string line;
133  while (std::getline(is, line)){
134    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
135      res.push_back(line);
136  }
137 
138  return res;
139}
140
Note: See TracBrowser for help on using the repository browser.