Annotation of loncom/cfgedittests/new.t, revision 1.1
1.1 ! foxr 1: #
! 2: # $Id: gplheader.pl,v 1.1 2001/11/29 18:19:27 www Exp $
! 3: #
! 4: # Copyright Michigan State University Board of Trustees
! 5: #
! 6: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
! 7: #
! 8: # LON-CAPA is free software; you can redistribute it and/or modify
! 9: # it 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: # LON-CAPA is distributed in the hope that it will be useful,
! 14: # but WITHOUT ANY WARRANTY; without even the implied warranty of
! 15: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! 16: # GNU General Public License for more details.
! 17: #
! 18: # You should have received a copy of the GNU General Public License
! 19: # along with LON-CAPA; if not, write to the Free Software
! 20: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
! 21: #
! 22: # /home/httpd/html/adm/gpl.txt
! 23: #
! 24: # http://www.lon-capa.org/
! 25: #
! 26:
! 27:
! 28: # Test new functionality of ConfigFileEditor::new
! 29: #
! 30:
! 31: use strict;
! 32: use Test;
! 33: use ConfigFileEdit;
! 34:
! 35:
! 36: #
! 37: # Tests we plan are:
! 38: # Check open failure
! 39: # Check open success
! 40: # Check read ok for config files.
! 41: # Check parse/index ok for config files.
! 42: #
! 43:
! 44: BEGIN {plan tests => 4}
! 45:
! 46:
! 47: #
! 48: # Test opens that should fail (file not found in this case).
! 49: sub TestOpenFail {
! 50: my $editor = ConfigFileEdit->new("nosuchfile.cfg",0);
! 51: ok($editor, undef);
! 52: }
! 53: #
! 54: # Test opens that should succeed (in this case we have a test configfile
! 55: # in this directory called test.cfg
! 56: #
! 57: sub TestOpenOk {
! 58: my $editor = ConfigFileEdit->new("testconfig.cfg",0);
! 59: if($editor) {
! 60: ok(1);
! 61: } else {
! 62: ok(0);
! 63: }
! 64:
! 65: }
! 66:
! 67: #
! 68: # Test that we can read the file as a line array stored in the hash.
! 69: #
! 70: sub TestReadOk {
! 71: my $editor = ConfigFileEdit->new("testconfig.cfg",0);
! 72:
! 73: # How many lines in the file?
! 74:
! 75: open(WCOUT, "wc -l testconfig.cfg |");
! 76: my $output = <WCOUT>; # Output of linecount
! 77: chomp($output);
! 78: $output =~ s/ +/ /g; # Get rid of multiple spaces so that...
! 79: my ($trash, $linecount, $junk) = split(/ /,$output); # We can extract the count.
! 80: close(WCOUT);
! 81:
! 82: # How many lines in the editor linecount array?
! 83:
! 84: my $editorlines = ($editor->{LineArray});
! 85: my $editorlinecount = @$editorlines;
! 86:
! 87: ok($linecount, $editorlinecount);
! 88:
! 89:
! 90: }
! 91:
! 92: sub TestParseOk {
! 93: my $editor = ConfigFileEdit->new("testconfig.cfg",0);
! 94:
! 95: #
! 96: # There should be a line in the file hash for the key line1
! 97: # It should index to line 0. which should match the first line
! 98: # of the file.
! 99: # There should only be 1 non-comment line in the file.
! 100:
! 101: my $LineIndex = $editor->{KeyToLines}; # Get the key to line hash.
! 102: if (! defined $LineIndex->{line1}) {
! 103: ok(0);
! 104: print (" No index for 'line1'\n");
! 105: my @keynames = keys %$LineIndex;
! 106: return;
! 107: }
! 108:
! 109: my $linesubscript = $LineIndex->{line1};
! 110: if(!defined($linesubscript) || ($linesubscript != 0)) {
! 111: ok(0,$linesubscript);
! 112: print("Subscript for 'line1' <> 0\n");
! 113: return;
! 114: }
! 115:
! 116: my $editorlines = $editor->{LineArray};
! 117: my $editorline = $editorlines->[$linesubscript];
! 118:
! 119: open(FILE, "< testconfig.cfg");
! 120: my $firstline = <FILE>;
! 121: chomp($firstline);
! 122: if($firstline ne $editorline) {
! 123: ok($firstline, $editorline);
! 124: return;
! 125:
! 126: }
! 127:
! 128:
! 129: ok(1, scalar(keys %$LineIndex));
! 130:
! 131:
! 132: }
! 133:
! 134: TestOpenFail;
! 135: TestOpenOk;
! 136: TestReadOk;
! 137: TestParseOk;
! 138:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>