Annotation of loncom/cfgedittests/write.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: use strict;
! 27: use Test;
! 28: use ConfigFileEdit;
! 29: use IO::File;
! 30: #
! 31: # Test ConfigFileEdtitor's ability to write back files.
! 32: #
! 33:
! 34: # Tests we plan:
! 35: # 1. Check write unmodified
! 36: # 2. Check write modified
! 37: #
! 38:
! 39: BEGIN {plan tests => 8}
! 40:
! 41: #
! 42: # Test write unmodified:
! 43: # - Read test2config.cfg
! 44: # - Write it to test3config.cfg
! 45: # - Read test3config.cfg
! 46: # - Ensure that the number of lines is the same.
! 47: # - Ensure that the number of keys is the same.
! 48: # - Ensure that all key values point to identical lines.
! 49: # - Delete test3config.cfg
! 50:
! 51: sub TestWriteUnmodified {
! 52:
! 53: # Read in original and write it back out... then read supposed copy.
! 54:
! 55: my $original = ConfigFileEdit->new("test2config.cfg",0);
! 56: my $fh = new IO::File "> test3config.cfg";
! 57: if(!$fh) {
! 58: die "Can't create test3config.cfg";
! 59: }
! 60: $original->Write($fh);
! 61: $fh->close;
! 62: undef $fh;
! 63:
! 64: my $copy = ConfigFileEdit->new("test3config.cfg",0);
! 65:
! 66: # Check that the two files have the same no. lines:
! 67:
! 68: my $origcount = $original->LineCount();
! 69: my $copycount = $copy->LineCount();
! 70: ok($copycount, $origcount);
! 71:
! 72: # Check that the number of keys is the same
! 73:
! 74: my $origkeys = $original->{KeyToLines};
! 75: my $copykeys = $original->{KeyToLines};
! 76:
! 77: my @origkeyarray = keys %$origkeys;
! 78: my @copykeyarray = keys %$origkeys;
! 79:
! 80: my $origkeycount = @origkeyarray;
! 81: my $copykeycount = @copykeyarray;
! 82:
! 83: ok($copykeycount, $origkeycount);
! 84:
! 85: # check that all keys in the original point to the same lines in copy
! 86:
! 87: my $isok = 1;
! 88: foreach my $item (keys %$origkeys) {
! 89: my $origline = $original->Find($item);
! 90: my $copyline = $copy->Find($item);
! 91:
! 92: # Must exist in copy.:
! 93:
! 94: if(!defined($copyline)) {
! 95: $isok = 0;
! 96: }
! 97: if($copyline ne $origline) {
! 98: $isok = 0;
! 99: }
! 100: }
! 101: ok($isok);
! 102:
! 103: # check that all keys in the copy point to the same lines in the original
! 104:
! 105: $isok = 1;
! 106: foreach my $item (keys %$copykeys) {
! 107: my $origline = $original->Find($item);
! 108: my $copyline = $copy->Find($item);
! 109:
! 110: # Must exist in copy:
! 111:
! 112: if(!defined($origline)) {
! 113: $isok = 0;
! 114: }
! 115: if($copyline ne $origline) {
! 116: $isok = 0;;
! 117: }
! 118: }
! 119: ok($isok);
! 120:
! 121: # Delete the test3config.cfg temp file:
! 122:
! 123: unlink("test3config.cfg");
! 124: }
! 125:
! 126: #
! 127: # Test ability to correctly write a modified file.
! 128: # Read test2config.cfg
! 129: # Delete the line keyed line2.
! 130: # Write the resulting config -> test3config.cfg
! 131: # Re-read the two files.
! 132: # 5. Ensure that test3config.cfg has one fewer line than test2config.cfg
! 133: # 6. Ensure that test3config.cfg has one fewer keys than test3config.cfg.
! 134: # 7. Ensure that line2 key is not in test3config.cfg
! 135: # 8. Ensure that all keys in test3config.cfg produce matching lines in
! 136: # test2config.cfg
! 137: sub TestWriteModified {
! 138:
! 139: # Create the modified file.
! 140:
! 141: my $original = ConfigFileEdit->new("test2config.cfg", 0);
! 142: $original->DeleteLine("line2");
! 143: my $fh = new IO::File "> test3config.cfg";
! 144: if(!$fh) {
! 145: die "Can't create test3cofig.cfg";
! 146: }
! 147:
! 148: $original->Write($fh);
! 149: $fh->close;
! 150: undef $fh;
! 151: undef $original;
! 152:
! 153: # Reread the original and read the modified file:
! 154:
! 155: $original = ConfigFileEdit->new("test2config.cfg",0); # Reread.
! 156: my $modified = ConfigFileEdit->new("test3config.cfg",0);
! 157:
! 158: # Test 5: Modified should have one fewer lines than original:
! 159:
! 160: ok($modified->LineCount(), $original->LineCount() - 1);
! 161:
! 162: # Test 6 Modified should have one fewer key elements than original:
! 163:
! 164: my $origkeys = $original->{KeyToLines};
! 165: my $modkeys = $modified->{KeyToLines};
! 166: my @origkeyarray = keys %$origkeys;
! 167: my @modkeyarray = keys %$modkeys;
! 168:
! 169: my $origkeycount = @origkeyarray;
! 170: my $modkeycount = @modkeyarray;
! 171: ok($modkeycount, $origkeycount - 1);
! 172:
! 173: # Test 7 Ensure that "line2" does not return a lookup for modified:
! 174:
! 175: my $sbundef = $modified->Find("line2");
! 176: if(!defined($sbundef)) {
! 177: ok(1);
! 178: } else {
! 179: ok(0);
! 180: }
! 181:
! 182: # Test 8 All keys in test3config should lookup identical lines in test2config.
! 183:
! 184: my $isok = 1;
! 185: foreach my $item (keys %$modkeys) {
! 186: my $origline = $original->Find($item);
! 187: my $modline = $modified->Find($item);
! 188:
! 189: # Origline must be defined:
! 190:
! 191: if(!defined($origline)) {
! 192: $isok = 0;
! 193: }
! 194:
! 195: # Origline eq $modline
! 196:
! 197: if($origline ne $modline) {
! 198: $isok = 0;
! 199: }
! 200: }
! 201: ok($isok);
! 202:
! 203: unlink("test3config.cfg"); # Get rid of temp test3config.cfg
! 204: }
! 205:
! 206: TestWriteUnmodified;
! 207:
! 208: TestWriteModified;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>