source: trunk/test/copyright_test.cc @ 1082

Last change on this file since 1082 was 978, checked in by Peter Johansson, 12 years ago

refs #429. Convert copyright statements to UTF-8

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
RevLine 
[592]1// $Id: copyright_test.cc 978 2009-12-12 20:09:41Z peter $
2
3/*
[978]4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
[847]5  Copyright (C) 2009 Peter Johansson
[592]6
[687]7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
[592]8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
[693]11  the Free Software Foundation; either version 3 of the License, or
[592]12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
[693]20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
[592]21*/
22
[596]23#include "Suite.h"
24
[592]25#include "Configuration.h"
26#include "File.h"
27#include "SVN.h"
28#include "SVNinfo.h"
29
30#include <cassert>
31#include <iostream>
32#include <fstream>
[597]33#include <sstream>
[592]34#include <string>
[594]35#include <vector>
[592]36
[594]37std::vector<std::string> copyright_lines(std::istream&);
38
[592]39int main( int argc, char* argv[])
40{
41  using namespace theplu::svndigest;
[744]42  test::Suite suite(argc, argv, true);
[592]43
[596]44  std::string root=test::filename("toy_project");
[594]45  std::string filename = root + "/README";
46
47  // Saving original file
48  std::ifstream is(filename.c_str());
49  assert(is.good());
50  std::string original_file;
51  std::getline(is, original_file, '\0');
52  is.close();
53  is.clear(std::ios::goodbit);
54
55  is.open(filename.c_str());
[595]56  std::vector<std::string> copyrights_old=copyright_lines(is);
[594]57  is.close();
58  is.clear(std::ios::goodbit);
59
[595]60  if (copyrights_old.size()!=1) {
[744]61    suite.out() << copyrights_old.size() << " Copyright lines\n";
62    for (size_t i=0; i<copyrights_old.size(); ++i)
63      suite.out() << copyrights_old[i] << "\n";
64    suite.add(false);
[594]65  }
[744]66  suite.out() << "File contains 1 copyright line.\n"; 
[594]67
[597]68  // warn about missing Copyright statement only in verbose mode
[744]69  if (suite.verbose()){
[597]70    std::string config_str("[copyright]\nmissing-copyright-warning=yes");
71    std::stringstream ss(config_str);
72    Configuration& config = Configuration::instance();
73    config.load(ss);
74  }
75
[744]76  suite.out() << "Create SVN instance" << std::endl;
[592]77  SVN* svn=SVN::instance(root);
78  if (!svn)
79    return 1;
80
81  // Extract repository location
[744]82  suite.out() << "Extract repository location" << std::endl;
[592]83  std::string repo=SVNinfo(root).repos_root_url();
[744]84  suite.out() << "Create File object" << std::endl;
[594]85  File file(0,filename,"");
[592]86 
[744]87  suite.out() << "Get stats for file" << std::endl;
88  file.parse(suite.verbose(), true);
[592]89 
[744]90  suite.out() << "Updating copyright statements" << std::endl;
[592]91  std::map<std::string, Alias> alias;
[597]92  alias["jari"]=Alias("jh", 1);
93  alias["peter"]=Alias("pj", 2);
94 
[744]95  file.print_copyright(alias, suite.verbose());
[592]96
[595]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
[597]102  std::vector<std::string> copyright_correct;
103  copyright_correct.push_back("Copyright (C) 2006 jh");
104  copyright_correct.push_back("Copyright (C) 2007, 2008 pj");
105  if (copyrights.size()!=copyright_correct.size()) {
[744]106    suite.add(false);
107    suite.out() << "ERROR: expected " << copyright_correct.size() 
[638]108                << " lines of Copyright (C)\n"
109                << "But found " << copyrights.size() << " lines.\n";
[595]110  }
[597]111  else {
[595]112    for (size_t i=0; i<copyrights.size(); ++i)
[597]113      if (copyrights[i]!=copyright_correct[i]){
[744]114        suite.add(false);
115        suite.out() << "ERROR: found '" << copyrights[i] << "'\n"
[597]116                    << "expected: '" << copyright_correct[i] << "'\n";
117      }
118
[595]119  }
120
[597]121
[594]122  // Restoring file
123  std::ofstream os(filename.c_str());
124  assert(os.good());
125  os << original_file;
[599]126  os.close();
[594]127
[744]128  if (suite.ok()) {
129    suite.out() << "Test is Ok!" << std::endl;
[594]130    return 0;
131  }
[744]132  suite.out() << "Test failed." << std::endl;
[594]133  return 1;
[592]134}
135
[594]136std::vector<std::string> copyright_lines(std::istream& is)
137{
138  using namespace theplu::svndigest;
139  std::vector<std::string> res;
140  std::string line;
141  while (std::getline(is, line)){
142    if (match_begin(line.begin(), line.end(), "Copyright (C)"))
143      res.push_back(line);
144  }
145 
146  return res;
147}
148
Note: See TracBrowser for help on using the repository browser.