чување ef3ceea9d6ff60d6a48048205547aae1d79465e7
родитељ bb21a9fecbad1c8681949f1f9bdb95afd4a6016a
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Tue, 5 Mar 2024 20:56:59 +0100
Add tests
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
измењених датотека: 7, додавања: 165(+), брисања: 1(-)
diff --git a/INSTALL b/INSTALL
@@ -10,6 +10,15 @@ Optional
* gzip (autodetected, for manpage compression)
+Testing
+-------
+
+* Perl ≥ 5.010
+* expect
+
+ $ make test
+
+
Install
-------
diff --git a/Makefile b/Makefile
@@ -33,7 +33,8 @@ $(PROG): $(OBJS) $(SRC) $(HEADERS)
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
clean:
- rm -f version date version.h *.o *.xz *~ $(PROG) $(MANPAGE)
+ rm -f tests/testfile version date version.h *.o *.xz *~ \
+ $(PROG) $(MANPAGE)
install: all
$(INSTALL) -Dm 0644 $(MANPAGE) $(MANPREFIX)/man1/$(MANPAGE)
@@ -53,6 +54,10 @@ uninstall:
rm -f sledv $(BINDIR)/sledv
rmdir $(BINDIR) 2>/dev/null || true
+test: all
+ command -v perl >/dev/null || { echo Test suite requires perl; exit 1; }
+ perl lib/runtests
+
pack: all version
sh -x lib/mkpack
diff --git a/lib/runtests b/lib/runtests
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+# This program is licensed under the terms of GNU GPL v3 or (at your option)
+# any later version. Copyright (C) 2022-2024 Страхиња Радић.
+# See the file LICENSE for exact copyright and license details.
+
+
+use 5.010;
+use File::Find;
+
+my $errors = 0;
+my @files;
+my $maxlen = 60;
+my $testdir = 'tests';
+
+system("sh -c 'command -v expect' >/dev/null") == 0 ||
+ die "Test suite requires expect";
+
+find(sub {
+ push @files, $File::Find::name if (/\.test$/);
+}, $testdir . "/");
+
+for (@files)
+{
+ system $_;
+ $results{$_} = $?>>8;
+}
+
+# Clear the screen
+print "\033[0m\033[2J\033[2K\033[1;1H";
+
+print <<"!";
+TEST RESULTS
+============================================================
+!
+
+for (@files)
+{
+ printf "%s:%*s%s\t%s\n", $_, $maxlen-5-length($_), " ",
+ $results{$_} == 0 ? " OK" : "FAIL";
+ if ($results{$_})
+ {
+ $errors = 1;
+ }
+}
+
+if ($errors == 1)
+{
+ printf "Some tests were unsuccessful - please report\n";
+}
+else
+{
+ printf "All tests passed\n";
+}
+
+print <<"!";
+============================================================
+!
diff --git a/tests/001-insert-text-50000-lines.test b/tests/001-insert-text-50000-lines.test
@@ -0,0 +1,30 @@
+#!/bin/sh
+# vim: set ft=bash:
+exec 2>/dev/null
+testfile=tests/testfile
+text=$( for k in $(seq 0 9)
+ do
+ printf "$k"
+ done)
+text=$( for j in $(seq 7)
+ do
+ printf "%s" "$text"
+ done)
+text=$( for i in $(seq 100)
+ do
+ printf "%s\n" "$text"
+ done)
+
+for i in $(seq 500)
+do
+ printf "%s\n" "$text"
+done > "$testfile"
+
+expect tests/insert.expect
+if [ "$(wc -l "$testfile" | awk '{print $1}')" -eq 50001 ]; then
+ result=0
+else
+ result=1
+fi
+rm "$testfile"
+exit "$result"
diff --git a/tests/002-replace-text-50000-lines.test b/tests/002-replace-text-50000-lines.test
@@ -0,0 +1,30 @@
+#!/bin/sh
+# vim: set ft=bash:
+exec 2>/dev/null
+testfile=tests/testfile
+text=$( for k in $(seq 0 9)
+ do
+ printf "$k"
+ done)
+text=$( for j in $(seq 7)
+ do
+ printf "%s" "$text"
+ done)
+text=$( for i in $(seq 100)
+ do
+ printf "%s\n" "$text"
+ done)
+
+for i in $(seq 500)
+do
+ printf "%s\n" "$text"
+done > "$testfile"
+
+expect tests/replace.expect
+if [ -n "$(grep "some text" "$testfile")" ]; then
+ result=0
+else
+ result=1
+fi
+rm "$testfile"
+exit "$result"
diff --git a/tests/insert.expect b/tests/insert.expect
@@ -0,0 +1,12 @@
+# vim: set ft=expect:
+spawn ./sled tests/testfile
+interact timeout 1 return
+send_user "Typing sentence"
+send "Hello, world!\r"
+interact timeout 1 return
+send_user "Pressing Ctrl+S"
+send "\023"
+interact timeout 1 return
+send_user "Pressing Ctrl+Q"
+send "\021"
+exit
diff --git a/tests/replace.expect b/tests/replace.expect
@@ -0,0 +1,21 @@
+# vim: set ft=expect:
+spawn ./sled tests/testfile
+interact timeout 1 return
+send_user "Entering command mode"
+send "\034"
+interact timeout 1 return
+send_user "Pressing \\"
+send "\\"
+interact timeout 1 return
+send_user "Entering text"
+send "4567\r"
+interact timeout 1 return
+send_user "Entering replacement"
+send "some text\r"
+interact timeout 1 return
+send_user "Pressing Ctrl+S"
+send "\023"
+interact timeout 1 return
+send_user "Pressing Ctrl+Q"
+send "\021"
+exit