[LON-CAPA-cvs] cvs: loncapa(1) /loncom/cfgedittests append.t delete.t lookup.t modify.t new.t test.pl test2config.cfg testconfig.cfg

foxr lon-capa-cvs@mail.lon-capa.org
Tue, 25 Nov 2003 12:15:35 -0000


This is a MIME encoded message

--foxr1069762535
Content-Type: text/plain

foxr		Tue Nov 25 07:15:35 2003 EDT

  Added files:                 (Branch: 1)
    /loncapa/loncom/cfgedittests	append.t delete.t lookup.t modify.t 
                                	new.t test.pl test2config.cfg 
                                	testconfig.cfg 
  Log:
  Bring back into the mainl-line from whatever branching hell import decided
  to put them in.
  
  
  
--foxr1069762535
Content-Type: text/plain
Content-Disposition: attachment; filename="foxr-20031125071535.txt"


Index: loncapa/loncom/cfgedittests/append.t
+++ loncapa/loncom/cfgedittests/append.t
#
# $Id: append.t,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

use strict;
use Test;
use ConfigFileEdit;


#
#   Test the ability to append an item to the list.
#   Tests we plan are:
#      Append item and check that:
#      1)   The count of items is incremented by 1.
#      2)   The last item is the one we added.
#      3)   Append comment ensure that size of index is not
#           increased.
#
BEGIN {plan tests=>3}

#
#   Test that after an append, the count of items is increased by one.

sub TestAppendCount {
    my $editor        = ConfigFileEdit->new("testconfig.cfg", 0);
    my $editorlines   = ($editor->{LineArray});
    my $initiallength = @$editorlines;
 
    $editor->Append("onemore:line:added:at:end");

    $editorlines      = ($editor->{LineArray});
    my $finallength   = @$editorlines;

    ok($initiallength+1, $finallength);
}
#
#   Test that after an append the new item has an index that is last.
#
sub TestAppendIsLast
{
    my $editor       = ConfigFileEdit->new("testconfig.cfg", 0);
    $editor->Append("onemore:line:added:at:end");

    my $editorlines  = ($editor->{LineArray});
    my $finallength  = @$editorlines;
    
    my $editorhash   = ($editor->{KeyToLines});
    my $linenum      = $editorhash->{"onemore"};

    ok($finallength - 1, $linenum); # New line is last line.
    
}
#
#   Test that append comment does not add to the index hash.
#
sub TestCommentAppend {
    my $editor           = ConfigFileEdit->new("testconfig.cfg", 0);

    # Figure out how many keys we have now.

    my $editorhash       = ($editor->{KeyToLines});
    my @hashkeys         = keys %$editorhash;
    my $initialcount     = @hashkeys;

    #  Append a comment and figure out how many keys we have:

    $editor->Append("#   This is clearly a comment line");
    $editorhash         = ($editor->{KeyToLines});
    @hashkeys           = keys %$editorhash;

    my $finalcount      = @hashkeys;

    ok($initialcount, $finalcount);
}

TestAppendCount;
TestAppendIsLast;
TestCommentAppend

Index: loncapa/loncom/cfgedittests/delete.t
+++ loncapa/loncom/cfgedittests/delete.t
#
# $Id: delete.t,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#


#  Test new functionality of ConfigFileEditor::delete
#

use strict;
use Test;
use ConfigFileEdit;


#
#   Tests  we plan are:
#   Delete a line from the middle of the test file
#     with a pile of consistency test.
#   Delete a line from the end of the test file 
#     with a pile of consistency tests.
#  NOTE: deleting from the beginning is the same as
#    from the middle.
#

BEGIN {plan tests => 6}

#
#   Test deletion of middle element.
#   - Use the test file test2config.cfg
#   - Delete the line with key line2
#   - Total number of lines should go down by 1.
#   - line1 - index should not change.
#   - last  - index should go down by 1.
sub  TestDeleteMiddle {

    my $editor    =    ConfigFileEdit->new("test2config.cfg", 0);

    my $linecount =    $editor->LineCount(); 
    $editor->DeleteLine("line2");
    ok($editor->LineCount(), $linecount-1);

    my $hashref = $editor->{KeyToLines};
    ok($hashref->{"line1"}, 0);

    ok($hashref->{"last"}, $editor->LineCount()-1);
}

#
#   Test deletion of last element:
#   - Use the test file test2config.cfg
#   - Delete the line with key "last" (last line).
#   - # lines should go down by 1.
#   - line1 index should not change.
#   - line2 index should not change.
sub TestDeleteEnd {
    my $editor    = ConfigFileEdit->new("test2config.cfg", 0);
    my $linecount = $editor->LineCount();
    my $hashref   = $editor->{KeyToLines};
    my $line1idx  = $hashref->{"line1"};
    my $line2idx  = $hashref->{"line2"};

    $editor->DeleteLine("last");

    ok($editor->LineCount(), $linecount-1);
    $hashref   = $editor->{KeyToLines}; # Hash may change due to reindex!!
    ok($hashref->{"line1"}, $line1idx);
    ok($hashref->{"line2"}, $line2idx);

}

TestDeleteMiddle;
TestDeleteEnd;


Index: loncapa/loncom/cfgedittests/lookup.t
+++ loncapa/loncom/cfgedittests/lookup.t
#
# $Id: lookup.t,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
use strict;
use Test;
use ConfigFileEdit;


#   Test indexing functions.
#   1.  Find a known line by indexing its key field.
#   2.  Fail to find a line that's not there.
#

BEGIN {plan tests=>2}

#
#   Test that we can lookup a line with a known ok index.
#
sub TestOkIndex {
    my $editor    = ConfigFileEdit->new("test2config.cfg", 0);

    my $line      = $editor->Find("line2");
    if(!defined($line)) {
	ok(0);
    }
    if($line eq "line2:another line:field:field") {
	ok(1);
    } else {
	ok(0);
    }
}
#
#   Test that we cannot find a line that's not there.
#
sub TestBadIndex {
    my $editor   = ConfigFileEdit->new("test2config.cfg", 0);

    my $line = $editor->Find("trash");
    if(!defined($line)) {
	ok(1);
    } else {
	ok(0);
    }
}



TestOkIndex;
TestBadIndex;

Index: loncapa/loncom/cfgedittests/modify.t
+++ loncapa/loncom/cfgedittests/modify.t
#
# $Id: modify.t,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#


#  Test new functionality of ConfigFileEditor::delete
#

use strict;
use Test;
use ConfigFileEdit;


#
#   Tests  we plan are:
#     Modify a line in the test file and
#     - Ensure that the modified line is retrievable.
#     - Ensure that the old line is gone
#       To do this we modify a line in a way that
#       changes its index.
#     Modify a line in the test file, replacing it with a comment.
#     - Ensure the old line is gone.
#     - Ensure the number of keys in the index hash decreased by 1.
#

BEGIN {plan tests => 4} 

#
#  Replace a line with noncomment.#
#
sub NonCommentReplace {
    my $newline = "end:final:line:in:file";
    my $editor = ConfigFileEdit->new("test2config.cfg", 0);
    $editor->ReplaceLine("last", $newline);
    
    ok($editor->Find("end"), $newline);
    
    if(!defined($editor->Find("last"))) {
	ok(1);
    } else {
	ok(0);
    }
}

sub CommentReplace {
    my $newline  = "  # end:final:line:in:file";
    my $editor   = ConfigFileEdit->new("test2config.cfg", 0);

    my $hashref  = $editor->{KeyToLines};
    my @keys     = keys(%$hashref);
    my $keycount1= @keys;	# Number of keys initially.

    $editor->ReplaceLine("last", $newline);
    
    if(!defined($editor->Find("last"))) {
	ok(1);
    } else {
	ok(0);
    }
    @keys         = keys(%$hashref);
    my $keycount2 = @keys;
    ok($keycount2, $keycount1 - 1);


}


NonCommentReplace;
CommentReplace;


Index: loncapa/loncom/cfgedittests/new.t
+++ loncapa/loncom/cfgedittests/new.t
#
# $Id: new.t,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#


#  Test new functionality of ConfigFileEditor::new
#

use strict;
use Test;
use ConfigFileEdit;


#
#   Tests  we plan are:
#     Check open failure
#     Check open success
#     Check read ok for config files.
#     Check parse/index ok for config files.
#

BEGIN {plan tests => 4}


#
#  Test opens that should fail (file not found in this case).
sub TestOpenFail {
    my $editor = ConfigFileEdit->new("nosuchfile.cfg",0);
    ok($editor, undef);
}
#
#  Test opens that should succeed (in this case we have a test configfile
#  in this directory called test.cfg
#
sub TestOpenOk {
    my $editor  = ConfigFileEdit->new("testconfig.cfg",0);
    if($editor) {
	ok(1);
    } else {
	ok(0);
    }

}

#
#  Test that we can read the file as a line array stored in the hash.
#
sub TestReadOk {
    my $editor = ConfigFileEdit->new("testconfig.cfg",0);

    # How many lines in the file?

    open(WCOUT, "wc -l testconfig.cfg |");
    my $output = <WCOUT>;	# Output of linecount
    chomp($output);
    $output    =~ s/ +/ /g;	# Get rid of multiple spaces so that...
    my ($trash, $linecount, $junk) = split(/ /,$output); # We can extract the count.
    close(WCOUT);

    #  How many lines in the editor linecount array?

    my $editorlines = ($editor->{LineArray});
    my $editorlinecount = @$editorlines;
    
    ok($linecount, $editorlinecount);


}

sub TestParseOk {
    my $editor = ConfigFileEdit->new("testconfig.cfg",0);

    #
    #  There should be a line in the file hash for the key line1
    #  It should index to line 0. which should match the first line
    #  of the file.
    #  There should only be 1 non-comment line in the file.

    my $LineIndex = $editor->{KeyToLines}; # Get the key to line hash.
    if (! defined $LineIndex->{line1}) {
	ok(0);
	print (" No index for 'line1'\n");
	my @keynames = keys %$LineIndex;
	return;
    }

    my $linesubscript = $LineIndex->{line1}; 
    if(!defined($linesubscript) || ($linesubscript != 0)) {
	ok(0,$linesubscript);
	print("Subscript for 'line1' <> 0\n");
	return;
    }

    my $editorlines   = $editor->{LineArray};
    my $editorline    = $editorlines->[$linesubscript];
    
    open(FILE, "< testconfig.cfg");
    my $firstline = <FILE>;
    chomp($firstline);
    if($firstline ne $editorline) {
	ok($firstline, $editorline);
	return;

    }


    ok(1, scalar(keys %$LineIndex));


}

TestOpenFail;
TestOpenOk;
TestReadOk;
TestParseOk;


Index: loncapa/loncom/cfgedittests/test.pl
+++ loncapa/loncom/cfgedittests/test.pl
#!/usr/bin/perl

#
# $Id: test.pl,v 1.1 2003/11/25 12:12:34 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#

#
#  This file runs all of the tests on the configuration file module.

use strict;
use FindBin;
use lib "$FindBin::Bin/..";	# Module under test is up one level.
use Test::Harness;


Test::Harness::verbose = 1;

my @tests; 			# Array of tests to run.
push @tests, qw(./new.t);	# Test new member.
push @tests, qw(./append.t);	# Test append member.
push @tests, qw(./lookup.t);	# Test lookup functions.
push @tests, qw(./delete.t);	# Test delete line functions.
push @tests, qw(./modify.t);	# Test modify line functionality

runtests(@tests);

Index: loncapa/loncom/cfgedittests/test2config.cfg
+++ loncapa/loncom/cfgedittests/test2config.cfg
line1:some text:another field:last field    # Trailing comment.
# comment by itself
       # comment with leading spaces
	# comment with leading tabs
line2:another line:field:field
last:last:line:in:the:file


Index: loncapa/loncom/cfgedittests/testconfig.cfg
+++ loncapa/loncom/cfgedittests/testconfig.cfg
line1:some text:another field:last field    # Trailing comment.
# comment by itself
       # comment with leading spaces
	# comment with leading tabs



--foxr1069762535--