From c8b860fb8d0f1531f99db4fad74f5892c6806f1b Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 18 Mar 2013 12:10:35 +0100 Subject: fix pkgTagSection::Exists() and add test --- test/libapt/makefile | 9 ++++++- test/libapt/tagfile_test.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/libapt/tagfile_test.cc (limited to 'test/libapt') diff --git a/test/libapt/makefile b/test/libapt/makefile index 5e225f240..953e455e0 100644 --- a/test/libapt/makefile +++ b/test/libapt/makefile @@ -93,8 +93,15 @@ SLIBS = -lapt-pkg SOURCE = cdromreducesourcelist_test.cc include $(PROGRAM_H) -# text IndexCopy::ConvertToSourceList +# test IndexCopy::ConvertToSourceList PROGRAM = IndexCopyToSourceList${BASENAME} SLIBS = -lapt-pkg SOURCE = indexcopytosourcelist_test.cc include $(PROGRAM_H) + +# test tagfile +PROGRAM = PkgTagFile${BASENAME} +SLIBS = -lapt-pkg +SOURCE = tagfile_test.cc +include $(PROGRAM_H) + diff --git a/test/libapt/tagfile_test.cc b/test/libapt/tagfile_test.cc new file mode 100644 index 000000000..2e2144f99 --- /dev/null +++ b/test/libapt/tagfile_test.cc @@ -0,0 +1,57 @@ +#include +#include + +#include "assert.h" +#include +#include + +char *tempfile = NULL; +int tempfile_fd = -1; + +void remove_tmpfile(void) +{ + if (tempfile_fd > 0) + close(tempfile_fd); + if (tempfile != NULL) { + unlink(tempfile); + free(tempfile); + } +} + +int main(int argc, char *argv[]) +{ + FileFd fd; + const char contents[] = "FieldA-12345678: the value of the field"; + atexit(remove_tmpfile); + tempfile = strdup("apt-test.XXXXXXXX"); + tempfile_fd = mkstemp(tempfile); + + /* (Re-)Open (as FileFd), write and seek to start of the temp file */ + equals(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite), true); + equals(fd.Write(contents, strlen(contents)), true); + equals(fd.Seek(0), true); + + pkgTagFile tfile(&fd); + pkgTagSection section; + equals(tfile.Step(section), true); + + /* It has one field */ + equals(section.Count(), 1); + + /* ... and it is called FieldA-12345678 */ + equals(section.Exists("FieldA-12345678"), true); + + /* its value is correct */ + equals(section.FindS("FieldA-12345678"), std::string("the value of the field")); + /* A non-existent field has an empty string as value */ + equals(section.FindS("FieldB-12345678"), std::string()); + + /* ... and Exists does not lie about missing fields... */ + equalsNot(section.Exists("FieldB-12345678"), true); + + /* There is only one section in this tag file */ + equals(tfile.Step(section), false); + + /* clean up handled by atexit handler, so just return here */ + return 0; +} -- cgit v1.2.3 From 0c98ee5ade6bb660bf23b09d759e0bb3c52068b9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 18 Mar 2013 13:52:43 +0100 Subject: test/libapt/tagfile_test.cc: add missing "unistd.h" (thanks to Niels Thykier) --- test/libapt/tagfile_test.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'test/libapt') diff --git a/test/libapt/tagfile_test.cc b/test/libapt/tagfile_test.cc index 2e2144f99..d12c74c95 100644 --- a/test/libapt/tagfile_test.cc +++ b/test/libapt/tagfile_test.cc @@ -4,6 +4,7 @@ #include "assert.h" #include #include +#include char *tempfile = NULL; int tempfile_fd = -1; -- cgit v1.2.3