Return-Path: X-Original-To: apmail-corinthia-commits-archive@minotaur.apache.org Delivered-To: apmail-corinthia-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id ACD3D9E9B for ; Wed, 17 Dec 2014 13:29:32 +0000 (UTC) Received: (qmail 41318 invoked by uid 500); 17 Dec 2014 13:29:32 -0000 Delivered-To: apmail-corinthia-commits-archive@corinthia.apache.org Received: (qmail 41303 invoked by uid 500); 17 Dec 2014 13:29:32 -0000 Mailing-List: contact commits-help@corinthia.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@corinthia.incubator.apache.org Delivered-To: mailing list commits@corinthia.incubator.apache.org Received: (qmail 41292 invoked by uid 99); 17 Dec 2014 13:29:32 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Dec 2014 13:29:32 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD,WEIRD_QUOTING X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 17 Dec 2014 13:28:38 +0000 Received: (qmail 33727 invoked by uid 99); 17 Dec 2014 13:28:14 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Dec 2014 13:28:14 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AEF6682E4E8; Wed, 17 Dec 2014 13:28:14 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: pmkelly@apache.org To: commits@corinthia.incubator.apache.org Date: Wed, 17 Dec 2014 13:29:42 -0000 Message-Id: In-Reply-To: <9867ce2e8a674e46a1b0776f45bb10a3@git.apache.org> References: <9867ce2e8a674e46a1b0776f45bb10a3@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [92/92] [abbrv] incubator-corinthia git commit: Move test functions into DocFormats library X-Virus-Checked: Checked by ClamAV on apache.org Move test functions into DocFormats library Move the implementation of all data test functions from dfutil into files in the 'test' directories of the various modules, such as core and ooxml. Support functions like those used to retrieve a HTML file or Word document from the plain text representation in the data file have similarly been moved. Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/d1aca0ca Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/d1aca0ca Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/d1aca0ca Branch: refs/heads/stable Commit: d1aca0ca834f5dc8f07135d8127aa4f004b7b6c0 Parents: 4e0e8b9 Author: Peter Kelly Authored: Sat Dec 13 01:27:25 2014 +0700 Committer: Peter Kelly Committed: Sat Dec 13 01:52:17 2014 +0700 ---------------------------------------------------------------------- DocFormats/core/CMakeLists.txt | 6 +- DocFormats/core/src/xml/DFChanges.c | 221 ++++++++++ DocFormats/core/src/xml/DFChanges.h | 23 ++ DocFormats/core/tests/common/BDTTests.c | 404 ++++++++++++++++++ DocFormats/core/tests/common/BDTTests.h | 24 ++ DocFormats/core/tests/css/CSSTests.c | 49 ++- DocFormats/core/tests/html/HTMLPlain.c | 11 + DocFormats/core/tests/html/HTMLPlain.h | 2 + DocFormats/core/tests/html/HTMLTests.c | 69 +++- DocFormats/filters/latex/CMakeLists.txt | 1 + DocFormats/filters/latex/tests/LaTeXTests.c | 24 +- DocFormats/filters/ooxml/CMakeLists.txt | 1 + DocFormats/filters/ooxml/tests/word/WordPlain.c | 12 + DocFormats/filters/ooxml/tests/word/WordPlain.h | 2 + DocFormats/filters/ooxml/tests/word/WordTests.c | 222 ++++++++++ consumers/dfutil/src/BDTTest.c | 369 ----------------- consumers/dfutil/src/BDTTest.h | 24 -- consumers/dfutil/src/CMakeLists.txt | 7 +- consumers/dfutil/src/Commands.c | 2 +- consumers/dfutil/src/DFChanges.c | 221 ---------- consumers/dfutil/src/DFChanges.h | 23 -- consumers/dfutil/src/Test.c | 1 - consumers/dfutil/src/TestCase.c | 45 -- consumers/dfutil/src/TestCase.h | 26 -- consumers/dfutil/src/TestFunctions.c | 408 ++----------------- consumers/dfutil/src/TestFunctions.h | 2 - consumers/dfutil/src/main.c | 2 +- 27 files changed, 1101 insertions(+), 1100 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/DocFormats/core/CMakeLists.txt b/DocFormats/core/CMakeLists.txt index 02debe0..b026cac 100644 --- a/DocFormats/core/CMakeLists.txt +++ b/DocFormats/core/CMakeLists.txt @@ -22,7 +22,9 @@ set(GroupSrcCommon src/common/DFTable.c src/common/DFTable.h) -set(GroupTestsCommon) +set(GroupTestsCommon + tests/common/BDTTests.c + tests/common/BDTTests.h) set(GroupSrcCSS src/css/CSS.c @@ -98,6 +100,8 @@ set(GroupSrcNames src/names/DFXMLNamespaces.h) set(GroupSrcXML + src/xml/DFChanges.c + src/xml/DFChanges.h src/xml/DFDOM.c src/xml/DFDOM.h src/xml/DFMarkupCompatibility.c http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/src/xml/DFChanges.c ---------------------------------------------------------------------- diff --git a/DocFormats/core/src/xml/DFChanges.c b/DocFormats/core/src/xml/DFChanges.c new file mode 100644 index 0000000..3e2ad96 --- /dev/null +++ b/DocFormats/core/src/xml/DFChanges.c @@ -0,0 +1,221 @@ +// Copyright 2012-2014 UX Productivity Pty Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "DFChanges.h" +#include "DFHashTable.h" +#include "DFString.h" +#include "DFNameMap.h" +#include "DFCommon.h" +#include +#include +#include + +static char *fullNameForTag(DFDocument *doc, Tag tag) +{ + const TagDecl *tagDecl = DFNameMapNameForTag(doc->map,tag); + const NamespaceDecl *nsDecl = DFNameMapNamespaceForID(doc->map,tagDecl->namespaceID); + if ((nsDecl->prefix == NULL) || (tagDecl->namespaceID == NAMESPACE_HTML)) + return strdup(tagDecl->localName); + else + return DFFormatString("%s:%s",nsDecl->prefix,tagDecl->localName); +} + +static void nodeDetailToBuffer(DFNode *node, DFBuffer *output) +{ + if (node == NULL) { + DFBufferAppendString(output,"null"); + return; + } + switch (node->tag) { + case DOM_DOCUMENT: + DFBufferAppendString(output,"#document"); + break; + case DOM_TEXT: { + char *quotedValue = DFQuote(node->value); + DFBufferAppendString(output,quotedValue); + free(quotedValue); + break; + } + case DOM_COMMENT: + DFBufferAppendString(output,"#comment"); + break; + case DOM_CDATA: + DFBufferAppendString(output,"#cdata-section"); + break; + case DOM_PROCESSING_INSTRUCTION: + DFBufferAppendString(output,node->target); + break; + default: { + char *elementName = fullNameForTag(node->doc,node->tag); + DFBufferFormat(output,"<%s",elementName); + for (unsigned int i = 0; i < node->attrsCount; i++) { + char *attrName = fullNameForTag(node->doc,node->attrs[i].tag); + const char *attrValue = node->attrs[i].value; + char *quotedValue = DFQuote(attrValue); + DFBufferFormat(output," %s=%s",attrName,quotedValue); + free(quotedValue); + free(attrName); + } + DFBufferFormat(output,">"); + switch (node->tag) { + case WORD_T: + case WORD_DELTEXT: { + char *content = DFNodeTextToString(node); + char *quoted = DFQuote(content); + char *sub; + if (strlen(quoted) >= 2) + sub = DFSubstring(quoted,1,strlen(quoted)-2); + else + sub = strdup(quoted); + DFBufferFormat(output,"%s",quoted,elementName); + free(sub); + free(quoted); + free(content); + } + } + free(elementName); + break; + } + } +} + +static void DFElementsByAttr(DFNode *node, Tag attr, DFHashTable *result) +{ + if (node->tag >= MIN_ELEMENT_TAG) { + const char *value = DFGetAttribute(node,attr); + if ((value != NULL) && (DFHashTableLookup(result,value) == NULL)) + DFHashTableAdd(result,value,node); + } + + for (DFNode *child = node->first; child != NULL; child = child->next) + DFElementsByAttr(child,attr,result); +} + +static void DFRecordChanges(DFNode *parent1, Tag idAttr, DFHashTable *map) +{ + assert(parent1->tag >= MIN_ELEMENT_TAG); + + // Check for changes in children + for (DFNode *child1 = parent1->first; child1 != NULL; child1 = child1->next) { + if (child1->tag >= MIN_ELEMENT_TAG) + DFRecordChanges(child1,idAttr,map); + } + + // Check the parent itself - starting with finding out, first of all, whether there + // is actually a corresponding element in the new document + const char *idValue = DFGetAttribute(parent1,idAttr); + DFNode *parent2 = (idValue != NULL) ? DFHashTableLookup(map,idValue) : NULL; + if (parent2 == NULL) { + parent1->changed = 1; + return; + } + + if (parent1->tag != parent2->tag) + parent1->changed = 1; // keep going - still want to check the children + + if (!identicalAttributesExcept(parent1,parent2,0)) + parent1->changed = 1;; // keep going - still want to check the children + + DFNode *child1 = parent1->first; + DFNode *child2 = parent2->first; + + while ((child1 != NULL) && (child2 != NULL)) { + + // Non-element nodes can't be correlated based on their id attribute. So we judge whether + // they are the same by inspecting their content, assuming the parent has the same set + // of children. + + if (child1->tag != child2->tag) { + parent1->changed = 1; + } + else { + switch (child1->tag) { + case DOM_TEXT: + case DOM_COMMENT: + case DOM_CDATA: { + assert(child1->value != NULL); + assert(child2->value != NULL); + if (strcmp(child1->value,child2->value)) + child1->changed = 1; + break; + } + case DOM_PROCESSING_INSTRUCTION: { + if (strcmp(child1->target,child2->target) || + strcmp(child1->value,child2->value)) { + child1->changed = 1; + } + break; + } + } + } + + child1 = child1->next; + child2 = child2->next; + } + + if ((child1 != NULL) && (child2 == NULL)) + parent1->changed = 1; + if ((child1 == NULL) && (child2 != NULL)) + parent1->changed = 1; +} + +static void DFPropagateChanges(DFNode *node) +{ + for (DFNode *child = node->first; child != NULL; child = child->next) { + DFPropagateChanges(child); + if (child->changed || child->childrenChanged) + node->childrenChanged = 1; + } +} + +static void DFChangesToStringRecursive(DFNode *node, int indent, DFBuffer *output) +{ + if (node->childrenChanged) + DFBufferAppendString(output,"*"); + else + DFBufferAppendString(output," "); + + if (node->changed) + DFBufferAppendString(output,"C"); + else + DFBufferAppendString(output," "); + + DFBufferAppendString(output," "); + + for (int i = 0; i < indent; i++) + DFBufferAppendString(output," "); + nodeDetailToBuffer(node,output); + DFBufferAppendString(output,"\n"); + + for (DFNode *child = node->first; child != NULL; child = child->next) + DFChangesToStringRecursive(child,indent+1,output); +} + +void DFComputeChanges(DFNode *root1, DFNode *root2, Tag idAttr) +{ + DFHashTable *map = DFHashTableNew(NULL,NULL); + DFElementsByAttr(root2,idAttr,map); + DFRecordChanges(root1,HTML_ID,map); + DFPropagateChanges(root1); + DFHashTableRelease(map); +} + +char *DFChangesToString(DFNode *root) +{ + DFBuffer *output = DFBufferNew(); + DFChangesToStringRecursive(root,0,output); + char *result = strdup(output->data); + DFBufferRelease(output); + return result; +} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/src/xml/DFChanges.h ---------------------------------------------------------------------- diff --git a/DocFormats/core/src/xml/DFChanges.h b/DocFormats/core/src/xml/DFChanges.h new file mode 100644 index 0000000..16759e4 --- /dev/null +++ b/DocFormats/core/src/xml/DFChanges.h @@ -0,0 +1,23 @@ +// Copyright 2012-2014 UX Productivity Pty Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef dfutil_DFChanges_h +#define dfutil_DFChanges_h + +#include "DFDOM.h" + +void DFComputeChanges(DFNode *root1, DFNode *root2, Tag idAttr); +char *DFChangesToString(DFNode *root); + +#endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/common/BDTTests.c ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/common/BDTTests.c b/DocFormats/core/tests/common/BDTTests.c new file mode 100644 index 0000000..83204d6 --- /dev/null +++ b/DocFormats/core/tests/common/BDTTests.c @@ -0,0 +1,404 @@ +// Copyright 2012-2014 UX Productivity Pty Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "BDTTests.h" +#include "DFDOM.h" +#include "DFBDT.h" +#include "DFString.h" +#include "DFXML.h" +#include "DFCommon.h" +#include "DFUnitTest.h" +#include +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// // +// ItemLens // +// // +//////////////////////////////////////////////////////////////////////////////////////////////////// + +typedef struct ItemLens ItemLens; + +struct ItemLens { + DFDocument *abstractDoc; + DFDocument *concreteDoc; +}; + +static ItemLens *ItemLensNew(DFDocument *abstractDoc, DFDocument *concreteDoc) +{ + ItemLens *lens = (ItemLens *)calloc(1,sizeof(ItemLens)); + lens->abstractDoc = DFDocumentRetain(abstractDoc); + lens->concreteDoc = DFDocumentRetain(concreteDoc); + return lens; +} + +static void ItemLensFree(ItemLens *lens) +{ + DFDocumentRelease(lens->abstractDoc); + DFDocumentRelease(lens->concreteDoc); + free(lens); +} + +static DFNode *ItemGet(ItemLens *lens, DFNode *concrete) +{ + switch (concrete->tag) { + case HTML_P: { + DFNode *li = DFCreateElement(lens->abstractDoc,HTML_H1); + DFFormatAttribute(li,HTML_ID,"x%u", concrete->seqNo); + char *value = DFNodeTextToString(concrete); + DFNode *text = DFCreateTextNode(lens->abstractDoc,value); + free(value); + DFAppendChild(li,text); + return li; + } + default: + return NULL; + } +} + +static int ItemIsVisible(ItemLens *lens, DFNode *concrete) +{ + switch (concrete->tag) { + case HTML_P: + return 1; + default: + return 0; + } +} + +static DFNode *ItemToConcrete(ItemLens *lens, DFNode *abstract) +{ + DFNode *result = NULL; + const char *idval = DFGetAttribute(abstract,HTML_ID); + if ((idval != NULL) && DFStringHasPrefix(idval,"x")) { + unsigned int seqNo = atoi(&idval[1]); + result = DFNodeForSeqNo(lens->concreteDoc,seqNo); + assert(result != NULL); + } + if (result == NULL) { + // FIXME: Not covered by tests + DFNode *p = DFCreateElement(lens->concreteDoc,HTML_P); + char *value = DFNodeTextToString(abstract); + DFNode *text = DFCreateTextNode(lens->concreteDoc,value); + free(value); + DFFormatAttribute(abstract,HTML_ID,"x%u",p->seqNo); + DFAppendChild(p,text); + result = p; + } + return result; +} + +DFLens lensItem = { + .isVisible = (void *)ItemIsVisible, + .get = (void *)ItemGet, + .put = NULL, + .create = NULL, + .remove = NULL, +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// // +// Test code // +// // +//////////////////////////////////////////////////////////////////////////////////////////////////// + +typedef struct TestContainerLens TestContainerLens; + +struct TestContainerLens { + DFDocument *abstractDoc; + DFDocument *concreteDoc; + ItemLens *itemLens; +}; + +static TestContainerLens *TestContainerLensNew(DFDocument *abstractDoc, DFDocument *concreteDoc) +{ + TestContainerLens *lens = (TestContainerLens *)calloc(1,sizeof(TestContainerLens)); + lens->abstractDoc = DFDocumentRetain(abstractDoc); + lens->concreteDoc = DFDocumentRetain(concreteDoc); + lens->itemLens = ItemLensNew(abstractDoc,concreteDoc); + return lens; +} + +static void TestContainerLensFree(TestContainerLens *lens) +{ + DFDocumentRelease(lens->abstractDoc); + DFDocumentRelease(lens->concreteDoc); + ItemLensFree(lens->itemLens); + free(lens); +} + +DFNode *TestContainerLensToAbstract(TestContainerLens *lens, DFNode *concrete) +{ + DFNode *abstract = DFCreateElement(lens->abstractDoc,HTML_UL); + BDTContainerGet(lens->itemLens,&lensItem,abstract,concrete); + return abstract; +} + +void TestContainerLensPut(TestContainerLens *lens, DFNode *abstract, DFNode *concrete) +{ + BDTContainerPut(lens->itemLens,&lensItem,abstract,concrete, + (DFLookupConcreteFunction)ItemToConcrete); +} + +void TestContainerLensRemove(TestContainerLens *lens, DFNode *concrete) +{ + for (DFNode *child = concrete->first; child != NULL; child = child->next) + lensItem.remove(lens->itemLens,child); +} + +static DFDocument *createHTMLDoc() +{ + DFDocument *doc = DFDocumentNew(); + DFNode *html = DFCreateElement(doc,HTML_HTML); + DFNode *body = DFCreateElement(doc,HTML_BODY); + DFAppendChild(doc->docNode,html); + DFAppendChild(html,body); + return doc; +} + +static DFDocument *createConcrete() +{ + const char *strings[] = { + "start1", + "start2", + "a", + "b", + "mid1", + "mid2", + "c", + "d", + "mid3", + "mid4", + "e", + "f", + "end1", + "end2", + NULL }; + + DFDocument *doc = createHTMLDoc(); + for (int i = 0; strings[i]; i++) { + const char *str = strings[i]; + DFNode *item; + if (strlen(str) == 1) + item = DFCreateElement(doc,HTML_P); + else + item = DFCreateElement(doc,HTML_DIV); + DFNode *body = doc->docNode->last->last; + DFNode *text = DFCreateTextNode(doc,str); + DFAppendChild(body,item); + DFAppendChild(item,text); + } + return doc; +} + +static void move(DFNode *container, int count, int from, int to) +{ + if ((to >= from) && (to <= from+count)) + return;; + + DFNode *nextSibling = NULL; + DFArray *move = DFArrayNew(NULL,NULL); + int index = 0; + for (DFNode *child = container->first; child != NULL; child = child->next) { + if (index == to) + nextSibling = child; + if ((index >= from) && (index < from + count)) + DFArrayAppend(move,child); + index++; + } + + for (size_t i = 0; i < DFArrayCount(move); i++) { + DFNode *child = DFArrayItemAt(move,i); + DFInsertBefore(container,child,nextSibling); + } + DFArrayRelease(move); +} + +static void removeChildren(DFNode *container, int *indices, int count) +{ + int index = 0; + DFNode *next; + for (DFNode *child = container->first; child != NULL; child = next) { + next = child->next; + + int removeThisNode = 0; + for (int i = 0; i < count; i++) { + if (indices[i] == index) + removeThisNode = 1; + } + + if (removeThisNode) + DFRemoveNode(child); + + index++; + } +} + +static DFDocument *getAbstractDoc(DFDocument *concreteDoc) +{ + DFDocument *abstractDoc = DFDocumentNew(); + DFNode *html = DFCreateElement(abstractDoc,HTML_HTML); + DFAppendChild(abstractDoc->docNode,html); + + DFNode *concrete = concreteDoc->docNode->last->last; + TestContainerLens *containerLens = TestContainerLensNew(abstractDoc,concreteDoc); + DFNode *abstract = TestContainerLensToAbstract(containerLens,concrete); + DFAppendChild(html,abstract); + TestContainerLensFree(containerLens); + return abstractDoc; +} + +static int charIsNewline(int c) +{ + return (c == '\n'); +} + +static void combinedListing(DFDocument *abstractDoc, DFDocument *concreteDoc, DFBuffer *output) +{ + DFInsertBefore(abstractDoc->docNode,DFCreateTextNode(abstractDoc,""),abstractDoc->docNode->first); + DFInsertBefore(concreteDoc->docNode,DFCreateTextNode(concreteDoc,""),concreteDoc->docNode->first); + char *abstractStr = DFSerializeXMLString(abstractDoc,0,1); + char *concreteStr = DFSerializeXMLString(concreteDoc,0,1); + + const char **abstractLines = DFStringTokenize(abstractStr,charIsNewline); + const char **concreteLines = DFStringTokenize(concreteStr,charIsNewline); + + size_t abstractCount = DFStringArrayCount(abstractLines); + size_t concreteCount = DFStringArrayCount(concreteLines); + + size_t absIndex = 0; + size_t conIndex = 0; + while ((absIndex < abstractCount) && (conIndex < concreteCount)) { + const char *absLine = abstractLines[absIndex]; + const char *conLine = concreteLines[conIndex]; + + if (DFStringHasSuffix(conLine,"")) { + DFBufferFormat(output,"%-30s %-40s\n","",conLine); + conIndex++; + } + else { + DFBufferFormat(output,"%-30s %-40s\n",absLine,conLine); + absIndex++; + conIndex++; + } + + } + while (absIndex < abstractCount) { + DFBufferFormat(output,"%s\n",abstractLines[absIndex]); + absIndex++; + } + while (conIndex < concreteCount) { + DFBufferFormat(output,"%-30s %s\n","",concreteLines[conIndex]); + conIndex++; + } + free(abstractStr); + free(concreteStr); + free(abstractLines); + free(concreteLines); + + DFBufferFormat(output,"%-30s %-40s\n","",""); // to satisfy tests +} + +static void BDT_testResult(DFDocument *concreteDoc, DFDocument *abstractDoc, DFBuffer *output) +{ + char *oldAbstractStr = DFSerializeXMLString(abstractDoc,0,1); + TestContainerLens *containerLens = TestContainerLensNew(abstractDoc,concreteDoc); + TestContainerLensPut(containerLens,abstractDoc->docNode->first->first,concreteDoc->docNode->first->first); + TestContainerLensFree(containerLens); + + DFDocument *newAbstractDoc = getAbstractDoc(concreteDoc); + char *newAbstractStr = DFSerializeXMLString(newAbstractDoc,0,1); + DFDocumentRelease(newAbstractDoc); + + DFStripIds(abstractDoc->docNode); + combinedListing(abstractDoc,concreteDoc,output); + int match = !strcmp(newAbstractStr,oldAbstractStr); + DFBufferFormat(output,"Match? %s\n",match ? "true" : "false"); + free(oldAbstractStr); + free(newAbstractStr); +} + +void BDT_testMove(int count, int from, int to, DFBuffer *output) +{ + DFDocument *concreteDoc = createConcrete(); + DFDocument *abstractDoc = getAbstractDoc(concreteDoc); + move(abstractDoc->docNode->first->first,count,from,to); + BDT_testResult(concreteDoc,abstractDoc,output); + DFDocumentRelease(concreteDoc); + DFDocumentRelease(abstractDoc); +} + +void BDT_testRemove(int *indices, int count, DFBuffer *output) +{ + DFDocument *concreteDoc = createConcrete(); + DFDocument *abstractDoc = getAbstractDoc(concreteDoc); + removeChildren(abstractDoc->docNode->first->first,indices,count); + BDT_testResult(concreteDoc,abstractDoc,output); + DFDocumentRelease(concreteDoc); + DFDocumentRelease(abstractDoc); +} + +int BDT_Test(int argc, const char **argv) +{ + if (argc < 3) { + printf("Usage: dfutil -bdt count from to\n"); + return 1; + } + + int count = atoi(argv[0]); + int from = atoi(argv[1]); + int to = atoi(argv[2]); + + DFBuffer *output = DFBufferNew(); + BDT_testMove(count,from,to,output); + printf("%s",output->data); + DFBufferRelease(output); + return 0; +} + +void test_move(void) +{ + if (utgetargc() < 3) { + DFBufferFormat(utgetoutput(),"move: insufficient arguments"); + return; + } + + int count = atoi(utgetargv()[0]); + int from = atoi(utgetargv()[1]); + int to = atoi(utgetargv()[2]); + + DFBuffer *output = DFBufferNew(); + BDT_testMove(count,from,to,output); + DFBufferFormat(utgetoutput(),"%s",output->data); + DFBufferRelease(output); +} + +void test_removeChildren(void) +{ + int *indices = (int *)malloc(utgetargc()*sizeof(int)); + + for (int i = 0; i < utgetargc(); i++) { + int index = atoi(utgetargv()[i]); + indices[i] = index; + } + + DFBuffer *output = DFBufferNew(); + BDT_testRemove(indices,utgetargc(),output); + DFBufferFormat(utgetoutput(),"%s",output->data); + DFBufferRelease(output); + + free(indices); +} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/common/BDTTests.h ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/common/BDTTests.h b/DocFormats/core/tests/common/BDTTests.h new file mode 100644 index 0000000..f4bae09 --- /dev/null +++ b/DocFormats/core/tests/common/BDTTests.h @@ -0,0 +1,24 @@ +// Copyright 2012-2014 UX Productivity Pty Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef dfutil_BDTTest_h +#define dfutil_BDTTest_h + +#include "DFBuffer.h" + +void BDT_testMove(int count, int from, int to, DFBuffer *output); +void BDT_testRemove(int *indices, int count, DFBuffer *output); +int BDT_Test(int argc, const char **argv); + +#endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/css/CSSTests.c ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/css/CSSTests.c b/DocFormats/core/tests/css/CSSTests.c index 99944b5..c4e1d2b 100644 --- a/DocFormats/core/tests/css/CSSTests.c +++ b/DocFormats/core/tests/css/CSSTests.c @@ -13,7 +13,54 @@ // limitations under the License. #include "DFUnitTest.h" -#include +#include "DFHashTable.h" +#include "DFBuffer.h" +#include "CSSSheet.h" +#include "DFCommon.h" +#include +#include + +void test_CSS_setHeadingNumbering(void) +{ + const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css"); + if (inputCSS == NULL) { + DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: input.css not defined"); + return; + } + if (utgetargc() < 1) { + DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: expected 1 argument"); + return; + } + + CSSSheet *styleSheet = CSSSheetNew(); + CSSSheetUpdateFromCSSText(styleSheet,inputCSS); + int on = !strcasecmp(utgetargv()[0],"true"); + CSSSheetSetHeadingNumbering(styleSheet,on); + char *cssText = CSSSheetCopyCSSText(styleSheet); + DFBufferFormat(utgetoutput(),"%s",cssText); + free(cssText); + CSSSheetRelease(styleSheet); +} + +void test_CSS_parse(void) +{ + const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css"); + if (inputCSS == NULL) { + DFBufferFormat(utgetoutput(),"input.css not defined"); + return; + } + CSSSheet *styleSheet = CSSSheetNew(); + CSSSheetUpdateFromCSSText(styleSheet,inputCSS); + char *text = CSSSheetCopyText(styleSheet); + DFBufferFormat(utgetoutput(),"%s",text); + free(text); + DFBufferFormat(utgetoutput(), + "================================================================================\n"); + char *cssText = CSSSheetCopyCSSText(styleSheet); + DFBufferFormat(utgetoutput(),"%s",cssText); + free(cssText); + CSSSheetRelease(styleSheet); +} static void test_sample(void) { http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/html/HTMLPlain.c ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/html/HTMLPlain.c b/DocFormats/core/tests/html/HTMLPlain.c index 8e00e11..656c5ba 100644 --- a/DocFormats/core/tests/html/HTMLPlain.c +++ b/DocFormats/core/tests/html/HTMLPlain.c @@ -21,6 +21,7 @@ #include "DFHTML.h" #include "DFCommon.h" #include "DFZipFile.h" +#include "DFUnitTest.h" #include #include #include @@ -122,3 +123,13 @@ DFDocument *HTML_fromPlain(const char *plain, const char *path, DFStorage *htmlS TextPackageRelease(textPackage); return result; } + +DFDocument *TestCaseGetHTML(DFStorage *storage, DFError **error) +{ + const char *inputHtml = DFHashTableLookup(utgetdata(),"input.html"); + if (inputHtml == NULL) { + DFErrorFormat(error,"input.html not defined"); + return NULL; + } + return HTML_fromPlain(inputHtml,utgetpath(),storage,error); +} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/html/HTMLPlain.h ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/html/HTMLPlain.h b/DocFormats/core/tests/html/HTMLPlain.h index 4d4fb74..210282e 100644 --- a/DocFormats/core/tests/html/HTMLPlain.h +++ b/DocFormats/core/tests/html/HTMLPlain.h @@ -23,4 +23,6 @@ char *HTML_toPlain(DFDocument *doc, DFStorage *storage, DFError **error); DFDocument *HTML_fromPlain(const char *plain, const char *path, DFStorage *htmlStorage, DFError **error); +DFDocument *TestCaseGetHTML(DFStorage *storage, DFError **error); + #endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/core/tests/html/HTMLTests.c ---------------------------------------------------------------------- diff --git a/DocFormats/core/tests/html/HTMLTests.c b/DocFormats/core/tests/html/HTMLTests.c index 2b50f48..5097cc0 100644 --- a/DocFormats/core/tests/html/HTMLTests.c +++ b/DocFormats/core/tests/html/HTMLTests.c @@ -13,7 +13,74 @@ // limitations under the License. #include "DFUnitTest.h" -#include +#include "HTMLPlain.h" +#include "DFHTML.h" +#include "DFHTMLNormalization.h" +#include "DFXML.h" +#include "DFDOM.h" +#include "DFChanges.h" +#include + +void test_HTML_testNormalize(void) +{ + const char *inputHtml = DFHashTableLookup(utgetdata(),"input.html"); + if (inputHtml == NULL) { + DFBufferFormat(utgetoutput(),"input.html not defined"); + return; + } + DFError *error = NULL; + DFDocument *doc = DFParseHTMLString(inputHtml,0,&error); + if (doc == NULL) { + DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); + DFErrorRelease(error); + return; + } + HTML_normalizeDocument(doc); + HTML_safeIndent(doc->docNode,0); + char *docStr = DFSerializeXMLString(doc,0,0); + DFBufferFormat(utgetoutput(),"%s",docStr); + free(docStr); + DFDocumentRelease(doc); +} + +void test_HTML_showChanges(void) +{ + const char *input1 = DFHashTableLookup(utgetdata(),"input1.html"); + if (input1 == NULL) { + DFBufferFormat(utgetoutput(),"input.html not defined"); + return; + } + + const char *input2 = DFHashTableLookup(utgetdata(),"input2.html"); + if (input2 == NULL) { + DFBufferFormat(utgetoutput(),"input.html not defined"); + return; + } + + + DFError *error = NULL; + DFDocument *doc1 = DFParseHTMLString(input1,0,&error); + if (doc1 == NULL) { + DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); + DFErrorRelease(error); + return; + } + + DFDocument *doc2 = DFParseHTMLString(input2,0,&error); + if (doc2 == NULL) { + DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); + DFErrorRelease(error); + DFDocumentRelease(doc1); + } + + DFComputeChanges(doc1->root,doc2->root,HTML_ID); + + char *changesStr = DFChangesToString(doc1->root); + DFBufferFormat(utgetoutput(),"%s",changesStr); + free(changesStr); + DFDocumentRelease(doc1); + DFDocumentRelease(doc2); +} static void test_sample(void) { http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/latex/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/DocFormats/filters/latex/CMakeLists.txt b/DocFormats/filters/latex/CMakeLists.txt index 349bfc3..77f305f 100644 --- a/DocFormats/filters/latex/CMakeLists.txt +++ b/DocFormats/filters/latex/CMakeLists.txt @@ -13,6 +13,7 @@ include_directories(../../core/src/html) include_directories(../../core/src/lib) include_directories(../../core/src/names) include_directories(../../core/src/xml) +include_directories(../../core/tests/html) include_directories(../../filters/latex/src) include_directories(../../filters/odf/src) include_directories(../../filters/ooxml/src/common) http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/latex/tests/LaTeXTests.c ---------------------------------------------------------------------- diff --git a/DocFormats/filters/latex/tests/LaTeXTests.c b/DocFormats/filters/latex/tests/LaTeXTests.c index 3a25a3a..2373dae 100644 --- a/DocFormats/filters/latex/tests/LaTeXTests.c +++ b/DocFormats/filters/latex/tests/LaTeXTests.c @@ -13,7 +13,29 @@ // limitations under the License. #include "DFUnitTest.h" -#include +#include "HTMLPlain.h" +#include "HTMLToLaTeX.h" +#include "DFHTMLNormalization.h" +#include + +void test_LaTeX_testCreate(void) +{ + DFError *error = NULL; + DFStorage *htmlStorage = DFStorageNewMemory(DFFileFormatHTML); + DFDocument *htmlDoc = TestCaseGetHTML(htmlStorage,&error); + DFStorageRelease(htmlStorage); + if (htmlDoc == NULL) { + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + DFErrorRelease(error); + return; + } + + HTML_normalizeDocument(htmlDoc); + char *latex = HTMLToLaTeX(htmlDoc); + DFBufferFormat(utgetoutput(),"%s",latex); + free(latex); + DFDocumentRelease(htmlDoc); +} static void test_sample(void) { http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/ooxml/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/DocFormats/filters/ooxml/CMakeLists.txt b/DocFormats/filters/ooxml/CMakeLists.txt index d5fcfce..a5a1fc5 100644 --- a/DocFormats/filters/ooxml/CMakeLists.txt +++ b/DocFormats/filters/ooxml/CMakeLists.txt @@ -13,6 +13,7 @@ include_directories(../../core/src/html) include_directories(../../core/src/lib) include_directories(../../core/src/names) include_directories(../../core/src/xml) +include_directories(../../core/tests/html) include_directories(../../filters/latex/src) include_directories(../../filters/odf/src) include_directories(../../filters/ooxml/src/common) http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/ooxml/tests/word/WordPlain.c ---------------------------------------------------------------------- diff --git a/DocFormats/filters/ooxml/tests/word/WordPlain.c b/DocFormats/filters/ooxml/tests/word/WordPlain.c index b9a2f0c..13a25aa 100644 --- a/DocFormats/filters/ooxml/tests/word/WordPlain.c +++ b/DocFormats/filters/ooxml/tests/word/WordPlain.c @@ -23,6 +23,7 @@ #include "DFHTML.h" #include "DFCommon.h" #include "DFZipFile.h" +#include "DFUnitTest.h" #include #include #include @@ -587,3 +588,14 @@ end: return NULL; } } + +DFStorage *TestCaseOpenPackage(DFError **error) +{ + const char *inputDocx = DFHashTableLookup(utgetdata(),"input.docx"); + if (inputDocx == NULL) { + DFErrorFormat(error,"input.docx not defined"); + return NULL; + } + + return Word_fromPlain(inputDocx,utgetpath(),error); +} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/ooxml/tests/word/WordPlain.h ---------------------------------------------------------------------- diff --git a/DocFormats/filters/ooxml/tests/word/WordPlain.h b/DocFormats/filters/ooxml/tests/word/WordPlain.h index 41aed2d..8b44911 100644 --- a/DocFormats/filters/ooxml/tests/word/WordPlain.h +++ b/DocFormats/filters/ooxml/tests/word/WordPlain.h @@ -23,4 +23,6 @@ char *Word_toPlain(DFStorage *rawStorage, DFHashTable *parts); DFStorage *Word_fromPlain(const char *plain, const char *plainPath, DFError **error); +DFStorage *TestCaseOpenPackage(DFError **error); + #endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/DocFormats/filters/ooxml/tests/word/WordTests.c ---------------------------------------------------------------------- diff --git a/DocFormats/filters/ooxml/tests/word/WordTests.c b/DocFormats/filters/ooxml/tests/word/WordTests.c index 89fed2c..6686ae5 100644 --- a/DocFormats/filters/ooxml/tests/word/WordTests.c +++ b/DocFormats/filters/ooxml/tests/word/WordTests.c @@ -13,7 +13,229 @@ // limitations under the License. #include "DFUnitTest.h" +#include "DFCommon.h" +#include "WordPlain.h" +#include "Word.h" +#include "HTMLPlain.h" +#include "DFString.h" +#include #include +#include +#include + +void test_Word_testCollapseBookmarks(void) +{ + DFError *error = NULL; + DFStorage *storage = TestCaseOpenPackage(&error); + if (storage == NULL) { + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + DFErrorRelease(error); + return; + } + + if (!WordCollapseBookmarks(storage,&error)) { + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + DFErrorRelease(error); + DFStorageRelease(storage); + return; + } + + DFHashTable *parts = DFHashTableNew((DFCopyFunction)strdup,free); + DFHashTableAdd(parts,"document",""); + + // Output the docx file + char *plain = Word_toPlain(storage,parts); + DFBufferFormat(utgetoutput(),"%s",plain); + free(plain); + DFHashTableRelease(parts); + DFStorageRelease(storage); +} + + +void test_Word_testExpandBookmarks(void) +{ + DFError *error = NULL; + DFStorage *storage = TestCaseOpenPackage(&error); + if (storage == NULL) { + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + DFErrorRelease(error); + return; + } + + if (!WordExpandBookmarks(storage,&error)) { + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + DFErrorRelease(error); + DFStorageRelease(storage); + return; + } + + DFHashTable *parts = DFHashTableNew((DFCopyFunction)strdup,free); + DFHashTableAdd(parts,"document",""); + + // Output the docx file + char *plain = Word_toPlain(storage,parts); + DFBufferFormat(utgetoutput(),"%s",plain); + free(plain); + DFHashTableRelease(parts); + DFStorageRelease(storage); +} + +void test_Word_testGet(void) +{ + DFError *error = NULL; + DFStorage *abstractStorage = NULL; + DFStorage *concreteStorage = NULL; + DFAbstractDocument *abstractDoc = NULL; + DFConcreteDocument *concreteDoc = NULL; + char *htmlPlain = NULL; + + concreteStorage = TestCaseOpenPackage(&error); + if (concreteStorage == NULL) + goto end; + + concreteDoc = DFConcreteDocumentNew(concreteStorage); + abstractStorage = DFStorageNewMemory(DFFileFormatHTML); + abstractDoc = DFAbstractDocumentNew(abstractStorage); + + if (!DFGet(concreteDoc,abstractDoc,&error)) + goto end;; + + DFDocument *htmlDoc = DFAbstractDocumentGetHTML(abstractDoc); + if (htmlDoc == NULL) { + DFErrorFormat(&error,"Abstract document has no HTML"); + goto end; + } + + htmlPlain = HTML_toPlain(htmlDoc,abstractStorage,&error); + +end: + if (htmlPlain != NULL) + DFBufferFormat(utgetoutput(),"%s",htmlPlain); + else + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + + DFErrorRelease(error); + DFStorageRelease(abstractStorage); + DFStorageRelease(concreteStorage); + DFAbstractDocumentRelease(abstractDoc); + DFConcreteDocumentRelease(concreteDoc); + free(htmlPlain); +} + +static DFHashTable *getFlags(void) +{ + if (utgetargc() == 0) + return NULL;; + DFHashTable *set = DFHashTableNew((DFCopyFunction)strdup,free); + for (int i = 0; i < utgetargc(); i++) { + const char *colon = strchr(utgetargv()[i],':'); + if (colon == NULL) + continue; + size_t colonPos = colon - utgetargv()[i]; + char *rawName = DFSubstring(utgetargv()[i],0,colonPos); + char *rawValue = DFSubstring(utgetargv()[i],colonPos+1,strlen(utgetargv()[i])); + char *name = DFStringTrimWhitespace(rawName); + char *value = DFStringTrimWhitespace(rawValue); + if (!strcasecmp(value,"true")) + DFHashTableAdd(set,name,name); + + free(rawName); + free(rawValue); + free(name); + free(value); + } + return set; +} + +void test_Word_testCreate(void) +{ + DFError *error = NULL; + DFDocument *htmlDoc = NULL; + DFStorage *abstractStorage = DFStorageNewMemory(DFFileFormatHTML); + DFStorage *concreteStorage = DFStorageNewMemory(DFFileFormatDocx); + DFAbstractDocument *abstractDoc = DFAbstractDocumentNew(abstractStorage); + DFConcreteDocument *concreteDoc = DFConcreteDocumentNew(concreteStorage); + DFHashTable *parts = NULL; + char *wordPlain = NULL; + + // Read input.html + htmlDoc = TestCaseGetHTML(abstractStorage,&error); + if (htmlDoc == NULL) + goto end; + + DFAbstractDocumentSetHTML(abstractDoc,htmlDoc); + + if (!DFCreate(concreteDoc,abstractDoc,&error)) + goto end; + + parts = getFlags(); + wordPlain = Word_toPlain(concreteStorage,parts); + +end: + if (wordPlain != NULL) + DFBufferFormat(utgetoutput(),"%s",wordPlain); + else + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + + DFErrorRelease(error); + DFDocumentRelease(htmlDoc); + DFStorageRelease(abstractStorage); + DFStorageRelease(concreteStorage); + DFAbstractDocumentRelease(abstractDoc); + DFConcreteDocumentRelease(concreteDoc); + DFHashTableRelease(parts); + free(wordPlain); +} + +void test_Word_testUpdate(void) +{ + DFError *error = NULL; + DFDocument *htmlDoc = NULL; + DFStorage *abstractStorage = NULL; + DFStorage *concreteStorage = NULL; + DFAbstractDocument *abstractDoc = NULL; + DFConcreteDocument *concreteDoc = NULL; + DFHashTable *parts = NULL; + char *wordPlain = NULL; + + concreteStorage = TestCaseOpenPackage(&error); + if (concreteStorage == NULL) + goto end; + + abstractStorage = DFStorageNewMemory(DFFileFormatHTML); + abstractDoc = DFAbstractDocumentNew(abstractStorage); + concreteDoc = DFConcreteDocumentNew(concreteStorage); + + // Read input.html + htmlDoc = TestCaseGetHTML(abstractStorage,&error); + if (htmlDoc == NULL) + goto end; + + DFAbstractDocumentSetHTML(abstractDoc,htmlDoc); + + // Update the docx file based on the contents of the HTML file + if (!DFPut(concreteDoc,abstractDoc,&error)) + goto end; + + // Output the updated docx file + parts = getFlags(); + wordPlain = Word_toPlain(concreteStorage,parts); + +end: + if (wordPlain != NULL) + DFBufferFormat(utgetoutput(),"%s",wordPlain); + else + DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); + + DFErrorRelease(error); + DFDocumentRelease(htmlDoc); + DFStorageRelease(abstractStorage); + DFStorageRelease(concreteStorage); + DFAbstractDocumentRelease(abstractDoc); + DFConcreteDocumentRelease(concreteDoc); + DFHashTableRelease(parts); + free(wordPlain); +} static void test_sample(void) { http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/BDTTest.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/BDTTest.c b/consumers/dfutil/src/BDTTest.c deleted file mode 100644 index 77dd05e..0000000 --- a/consumers/dfutil/src/BDTTest.c +++ /dev/null @@ -1,369 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "BDTTest.h" -#include "DFDOM.h" -#include "DFBDT.h" -#include "DFString.h" -#include "DFXML.h" -#include "DFCommon.h" -#include -#include -#include -#include - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// // -// ItemLens // -// // -//////////////////////////////////////////////////////////////////////////////////////////////////// - -typedef struct ItemLens ItemLens; - -struct ItemLens { - DFDocument *abstractDoc; - DFDocument *concreteDoc; -}; - -static ItemLens *ItemLensNew(DFDocument *abstractDoc, DFDocument *concreteDoc) -{ - ItemLens *lens = (ItemLens *)calloc(1,sizeof(ItemLens)); - lens->abstractDoc = DFDocumentRetain(abstractDoc); - lens->concreteDoc = DFDocumentRetain(concreteDoc); - return lens; -} - -static void ItemLensFree(ItemLens *lens) -{ - DFDocumentRelease(lens->abstractDoc); - DFDocumentRelease(lens->concreteDoc); - free(lens); -} - -static DFNode *ItemGet(ItemLens *lens, DFNode *concrete) -{ - switch (concrete->tag) { - case HTML_P: { - DFNode *li = DFCreateElement(lens->abstractDoc,HTML_H1); - DFFormatAttribute(li,HTML_ID,"x%u", concrete->seqNo); - char *value = DFNodeTextToString(concrete); - DFNode *text = DFCreateTextNode(lens->abstractDoc,value); - free(value); - DFAppendChild(li,text); - return li; - } - default: - return NULL; - } -} - -static int ItemIsVisible(ItemLens *lens, DFNode *concrete) -{ - switch (concrete->tag) { - case HTML_P: - return 1; - default: - return 0; - } -} - -static DFNode *ItemToConcrete(ItemLens *lens, DFNode *abstract) -{ - DFNode *result = NULL; - const char *idval = DFGetAttribute(abstract,HTML_ID); - if ((idval != NULL) && DFStringHasPrefix(idval,"x")) { - unsigned int seqNo = atoi(&idval[1]); - result = DFNodeForSeqNo(lens->concreteDoc,seqNo); - assert(result != NULL); - } - if (result == NULL) { - // FIXME: Not covered by tests - DFNode *p = DFCreateElement(lens->concreteDoc,HTML_P); - char *value = DFNodeTextToString(abstract); - DFNode *text = DFCreateTextNode(lens->concreteDoc,value); - free(value); - DFFormatAttribute(abstract,HTML_ID,"x%u",p->seqNo); - DFAppendChild(p,text); - result = p; - } - return result; -} - -DFLens lensItem = { - .isVisible = (void *)ItemIsVisible, - .get = (void *)ItemGet, - .put = NULL, - .create = NULL, - .remove = NULL, -}; - -//////////////////////////////////////////////////////////////////////////////////////////////////// -// // -// Test code // -// // -//////////////////////////////////////////////////////////////////////////////////////////////////// - -typedef struct TestContainerLens TestContainerLens; - -struct TestContainerLens { - DFDocument *abstractDoc; - DFDocument *concreteDoc; - ItemLens *itemLens; -}; - -static TestContainerLens *TestContainerLensNew(DFDocument *abstractDoc, DFDocument *concreteDoc) -{ - TestContainerLens *lens = (TestContainerLens *)calloc(1,sizeof(TestContainerLens)); - lens->abstractDoc = DFDocumentRetain(abstractDoc); - lens->concreteDoc = DFDocumentRetain(concreteDoc); - lens->itemLens = ItemLensNew(abstractDoc,concreteDoc); - return lens; -} - -static void TestContainerLensFree(TestContainerLens *lens) -{ - DFDocumentRelease(lens->abstractDoc); - DFDocumentRelease(lens->concreteDoc); - ItemLensFree(lens->itemLens); - free(lens); -} - -DFNode *TestContainerLensToAbstract(TestContainerLens *lens, DFNode *concrete) -{ - DFNode *abstract = DFCreateElement(lens->abstractDoc,HTML_UL); - BDTContainerGet(lens->itemLens,&lensItem,abstract,concrete); - return abstract; -} - -void TestContainerLensPut(TestContainerLens *lens, DFNode *abstract, DFNode *concrete) -{ - BDTContainerPut(lens->itemLens,&lensItem,abstract,concrete, - (DFLookupConcreteFunction)ItemToConcrete); -} - -void TestContainerLensRemove(TestContainerLens *lens, DFNode *concrete) -{ - for (DFNode *child = concrete->first; child != NULL; child = child->next) - lensItem.remove(lens->itemLens,child); -} - -static DFDocument *createHTMLDoc() -{ - DFDocument *doc = DFDocumentNew(); - DFNode *html = DFCreateElement(doc,HTML_HTML); - DFNode *body = DFCreateElement(doc,HTML_BODY); - DFAppendChild(doc->docNode,html); - DFAppendChild(html,body); - return doc; -} - -static DFDocument *createConcrete() -{ - const char *strings[] = { - "start1", - "start2", - "a", - "b", - "mid1", - "mid2", - "c", - "d", - "mid3", - "mid4", - "e", - "f", - "end1", - "end2", - NULL }; - - DFDocument *doc = createHTMLDoc(); - for (int i = 0; strings[i]; i++) { - const char *str = strings[i]; - DFNode *item; - if (strlen(str) == 1) - item = DFCreateElement(doc,HTML_P); - else - item = DFCreateElement(doc,HTML_DIV); - DFNode *body = doc->docNode->last->last; - DFNode *text = DFCreateTextNode(doc,str); - DFAppendChild(body,item); - DFAppendChild(item,text); - } - return doc; -} - -static void move(DFNode *container, int count, int from, int to) -{ - if ((to >= from) && (to <= from+count)) - return;; - - DFNode *nextSibling = NULL; - DFArray *move = DFArrayNew(NULL,NULL); - int index = 0; - for (DFNode *child = container->first; child != NULL; child = child->next) { - if (index == to) - nextSibling = child; - if ((index >= from) && (index < from + count)) - DFArrayAppend(move,child); - index++; - } - - for (size_t i = 0; i < DFArrayCount(move); i++) { - DFNode *child = DFArrayItemAt(move,i); - DFInsertBefore(container,child,nextSibling); - } - DFArrayRelease(move); -} - -static void removeChildren(DFNode *container, int *indices, int count) -{ - int index = 0; - DFNode *next; - for (DFNode *child = container->first; child != NULL; child = next) { - next = child->next; - - int removeThisNode = 0; - for (int i = 0; i < count; i++) { - if (indices[i] == index) - removeThisNode = 1; - } - - if (removeThisNode) - DFRemoveNode(child); - - index++; - } -} - -static DFDocument *getAbstractDoc(DFDocument *concreteDoc) -{ - DFDocument *abstractDoc = DFDocumentNew(); - DFNode *html = DFCreateElement(abstractDoc,HTML_HTML); - DFAppendChild(abstractDoc->docNode,html); - - DFNode *concrete = concreteDoc->docNode->last->last; - TestContainerLens *containerLens = TestContainerLensNew(abstractDoc,concreteDoc); - DFNode *abstract = TestContainerLensToAbstract(containerLens,concrete); - DFAppendChild(html,abstract); - TestContainerLensFree(containerLens); - return abstractDoc; -} - -static int charIsNewline(int c) -{ - return (c == '\n'); -} - -static void combinedListing(DFDocument *abstractDoc, DFDocument *concreteDoc, DFBuffer *output) -{ - DFInsertBefore(abstractDoc->docNode,DFCreateTextNode(abstractDoc,""),abstractDoc->docNode->first); - DFInsertBefore(concreteDoc->docNode,DFCreateTextNode(concreteDoc,""),concreteDoc->docNode->first); - char *abstractStr = DFSerializeXMLString(abstractDoc,0,1); - char *concreteStr = DFSerializeXMLString(concreteDoc,0,1); - - const char **abstractLines = DFStringTokenize(abstractStr,charIsNewline); - const char **concreteLines = DFStringTokenize(concreteStr,charIsNewline); - - size_t abstractCount = DFStringArrayCount(abstractLines); - size_t concreteCount = DFStringArrayCount(concreteLines); - - size_t absIndex = 0; - size_t conIndex = 0; - while ((absIndex < abstractCount) && (conIndex < concreteCount)) { - const char *absLine = abstractLines[absIndex]; - const char *conLine = concreteLines[conIndex]; - - if (DFStringHasSuffix(conLine,"")) { - DFBufferFormat(output,"%-30s %-40s\n","",conLine); - conIndex++; - } - else { - DFBufferFormat(output,"%-30s %-40s\n",absLine,conLine); - absIndex++; - conIndex++; - } - - } - while (absIndex < abstractCount) { - DFBufferFormat(output,"%s\n",abstractLines[absIndex]); - absIndex++; - } - while (conIndex < concreteCount) { - DFBufferFormat(output,"%-30s %s\n","",concreteLines[conIndex]); - conIndex++; - } - free(abstractStr); - free(concreteStr); - free(abstractLines); - free(concreteLines); - - DFBufferFormat(output,"%-30s %-40s\n","",""); // to satisfy tests -} - -static void BDT_testResult(DFDocument *concreteDoc, DFDocument *abstractDoc, DFBuffer *output) -{ - char *oldAbstractStr = DFSerializeXMLString(abstractDoc,0,1); - TestContainerLens *containerLens = TestContainerLensNew(abstractDoc,concreteDoc); - TestContainerLensPut(containerLens,abstractDoc->docNode->first->first,concreteDoc->docNode->first->first); - TestContainerLensFree(containerLens); - - DFDocument *newAbstractDoc = getAbstractDoc(concreteDoc); - char *newAbstractStr = DFSerializeXMLString(newAbstractDoc,0,1); - DFDocumentRelease(newAbstractDoc); - - DFStripIds(abstractDoc->docNode); - combinedListing(abstractDoc,concreteDoc,output); - int match = !strcmp(newAbstractStr,oldAbstractStr); - DFBufferFormat(output,"Match? %s\n",match ? "true" : "false"); - free(oldAbstractStr); - free(newAbstractStr); -} - -void BDT_testMove(int count, int from, int to, DFBuffer *output) -{ - DFDocument *concreteDoc = createConcrete(); - DFDocument *abstractDoc = getAbstractDoc(concreteDoc); - move(abstractDoc->docNode->first->first,count,from,to); - BDT_testResult(concreteDoc,abstractDoc,output); - DFDocumentRelease(concreteDoc); - DFDocumentRelease(abstractDoc); -} - -void BDT_testRemove(int *indices, int count, DFBuffer *output) -{ - DFDocument *concreteDoc = createConcrete(); - DFDocument *abstractDoc = getAbstractDoc(concreteDoc); - removeChildren(abstractDoc->docNode->first->first,indices,count); - BDT_testResult(concreteDoc,abstractDoc,output); - DFDocumentRelease(concreteDoc); - DFDocumentRelease(abstractDoc); -} - -int BDT_Test(int argc, const char **argv) -{ - if (argc < 3) { - printf("Usage: dfutil -bdt count from to\n"); - return 1; - } - - int count = atoi(argv[0]); - int from = atoi(argv[1]); - int to = atoi(argv[2]); - - DFBuffer *output = DFBufferNew(); - BDT_testMove(count,from,to,output); - printf("%s",output->data); - DFBufferRelease(output); - return 0; -} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/BDTTest.h ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/BDTTest.h b/consumers/dfutil/src/BDTTest.h deleted file mode 100644 index f4bae09..0000000 --- a/consumers/dfutil/src/BDTTest.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef dfutil_BDTTest_h -#define dfutil_BDTTest_h - -#include "DFBuffer.h" - -void BDT_testMove(int count, int from, int to, DFBuffer *output); -void BDT_testRemove(int *indices, int count, DFBuffer *output); -int BDT_Test(int argc, const char **argv); - -#endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/CMakeLists.txt b/consumers/dfutil/src/CMakeLists.txt index 79ad9a5..1ebc8fd 100644 --- a/consumers/dfutil/src/CMakeLists.txt +++ b/consumers/dfutil/src/CMakeLists.txt @@ -1,18 +1,12 @@ cmake_minimum_required(VERSION 2.8) set(SOURCES - BDTTest.c - BDTTest.h Commands.c Commands.h - DFChanges.c - DFChanges.h FunctionTests.h StringTests.h Test.c Test.h - TestCase.c - TestCase.h TestFunctions.c TestFunctions.h main.c @@ -31,6 +25,7 @@ include_directories(../../../DocFormats/core/src/lib) include_directories(../../../DocFormats/core/src/names) include_directories(../../../DocFormats/core/src/xml) include_directories(../../../DocFormats/core/tests/html) +include_directories(../../../DocFormats/core/tests/common) include_directories(../../../DocFormats/filters/latex/src) include_directories(../../../DocFormats/filters/odf/src) include_directories(../../../DocFormats/filters/ooxml/src/common) http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/Commands.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/Commands.c b/consumers/dfutil/src/Commands.c index 07d84dd..81e8221 100644 --- a/consumers/dfutil/src/Commands.c +++ b/consumers/dfutil/src/Commands.c @@ -13,7 +13,7 @@ // limitations under the License. #include "Commands.h" -#include "BDTTest.h" +#include "BDTTests.h" #include "Test.h" #include "WordPlain.h" #include "HTMLPlain.h" http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/DFChanges.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/DFChanges.c b/consumers/dfutil/src/DFChanges.c deleted file mode 100644 index 3e2ad96..0000000 --- a/consumers/dfutil/src/DFChanges.c +++ /dev/null @@ -1,221 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "DFChanges.h" -#include "DFHashTable.h" -#include "DFString.h" -#include "DFNameMap.h" -#include "DFCommon.h" -#include -#include -#include - -static char *fullNameForTag(DFDocument *doc, Tag tag) -{ - const TagDecl *tagDecl = DFNameMapNameForTag(doc->map,tag); - const NamespaceDecl *nsDecl = DFNameMapNamespaceForID(doc->map,tagDecl->namespaceID); - if ((nsDecl->prefix == NULL) || (tagDecl->namespaceID == NAMESPACE_HTML)) - return strdup(tagDecl->localName); - else - return DFFormatString("%s:%s",nsDecl->prefix,tagDecl->localName); -} - -static void nodeDetailToBuffer(DFNode *node, DFBuffer *output) -{ - if (node == NULL) { - DFBufferAppendString(output,"null"); - return; - } - switch (node->tag) { - case DOM_DOCUMENT: - DFBufferAppendString(output,"#document"); - break; - case DOM_TEXT: { - char *quotedValue = DFQuote(node->value); - DFBufferAppendString(output,quotedValue); - free(quotedValue); - break; - } - case DOM_COMMENT: - DFBufferAppendString(output,"#comment"); - break; - case DOM_CDATA: - DFBufferAppendString(output,"#cdata-section"); - break; - case DOM_PROCESSING_INSTRUCTION: - DFBufferAppendString(output,node->target); - break; - default: { - char *elementName = fullNameForTag(node->doc,node->tag); - DFBufferFormat(output,"<%s",elementName); - for (unsigned int i = 0; i < node->attrsCount; i++) { - char *attrName = fullNameForTag(node->doc,node->attrs[i].tag); - const char *attrValue = node->attrs[i].value; - char *quotedValue = DFQuote(attrValue); - DFBufferFormat(output," %s=%s",attrName,quotedValue); - free(quotedValue); - free(attrName); - } - DFBufferFormat(output,">"); - switch (node->tag) { - case WORD_T: - case WORD_DELTEXT: { - char *content = DFNodeTextToString(node); - char *quoted = DFQuote(content); - char *sub; - if (strlen(quoted) >= 2) - sub = DFSubstring(quoted,1,strlen(quoted)-2); - else - sub = strdup(quoted); - DFBufferFormat(output,"%s",quoted,elementName); - free(sub); - free(quoted); - free(content); - } - } - free(elementName); - break; - } - } -} - -static void DFElementsByAttr(DFNode *node, Tag attr, DFHashTable *result) -{ - if (node->tag >= MIN_ELEMENT_TAG) { - const char *value = DFGetAttribute(node,attr); - if ((value != NULL) && (DFHashTableLookup(result,value) == NULL)) - DFHashTableAdd(result,value,node); - } - - for (DFNode *child = node->first; child != NULL; child = child->next) - DFElementsByAttr(child,attr,result); -} - -static void DFRecordChanges(DFNode *parent1, Tag idAttr, DFHashTable *map) -{ - assert(parent1->tag >= MIN_ELEMENT_TAG); - - // Check for changes in children - for (DFNode *child1 = parent1->first; child1 != NULL; child1 = child1->next) { - if (child1->tag >= MIN_ELEMENT_TAG) - DFRecordChanges(child1,idAttr,map); - } - - // Check the parent itself - starting with finding out, first of all, whether there - // is actually a corresponding element in the new document - const char *idValue = DFGetAttribute(parent1,idAttr); - DFNode *parent2 = (idValue != NULL) ? DFHashTableLookup(map,idValue) : NULL; - if (parent2 == NULL) { - parent1->changed = 1; - return; - } - - if (parent1->tag != parent2->tag) - parent1->changed = 1; // keep going - still want to check the children - - if (!identicalAttributesExcept(parent1,parent2,0)) - parent1->changed = 1;; // keep going - still want to check the children - - DFNode *child1 = parent1->first; - DFNode *child2 = parent2->first; - - while ((child1 != NULL) && (child2 != NULL)) { - - // Non-element nodes can't be correlated based on their id attribute. So we judge whether - // they are the same by inspecting their content, assuming the parent has the same set - // of children. - - if (child1->tag != child2->tag) { - parent1->changed = 1; - } - else { - switch (child1->tag) { - case DOM_TEXT: - case DOM_COMMENT: - case DOM_CDATA: { - assert(child1->value != NULL); - assert(child2->value != NULL); - if (strcmp(child1->value,child2->value)) - child1->changed = 1; - break; - } - case DOM_PROCESSING_INSTRUCTION: { - if (strcmp(child1->target,child2->target) || - strcmp(child1->value,child2->value)) { - child1->changed = 1; - } - break; - } - } - } - - child1 = child1->next; - child2 = child2->next; - } - - if ((child1 != NULL) && (child2 == NULL)) - parent1->changed = 1; - if ((child1 == NULL) && (child2 != NULL)) - parent1->changed = 1; -} - -static void DFPropagateChanges(DFNode *node) -{ - for (DFNode *child = node->first; child != NULL; child = child->next) { - DFPropagateChanges(child); - if (child->changed || child->childrenChanged) - node->childrenChanged = 1; - } -} - -static void DFChangesToStringRecursive(DFNode *node, int indent, DFBuffer *output) -{ - if (node->childrenChanged) - DFBufferAppendString(output,"*"); - else - DFBufferAppendString(output," "); - - if (node->changed) - DFBufferAppendString(output,"C"); - else - DFBufferAppendString(output," "); - - DFBufferAppendString(output," "); - - for (int i = 0; i < indent; i++) - DFBufferAppendString(output," "); - nodeDetailToBuffer(node,output); - DFBufferAppendString(output,"\n"); - - for (DFNode *child = node->first; child != NULL; child = child->next) - DFChangesToStringRecursive(child,indent+1,output); -} - -void DFComputeChanges(DFNode *root1, DFNode *root2, Tag idAttr) -{ - DFHashTable *map = DFHashTableNew(NULL,NULL); - DFElementsByAttr(root2,idAttr,map); - DFRecordChanges(root1,HTML_ID,map); - DFPropagateChanges(root1); - DFHashTableRelease(map); -} - -char *DFChangesToString(DFNode *root) -{ - DFBuffer *output = DFBufferNew(); - DFChangesToStringRecursive(root,0,output); - char *result = strdup(output->data); - DFBufferRelease(output); - return result; -} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/DFChanges.h ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/DFChanges.h b/consumers/dfutil/src/DFChanges.h deleted file mode 100644 index 16759e4..0000000 --- a/consumers/dfutil/src/DFChanges.h +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef dfutil_DFChanges_h -#define dfutil_DFChanges_h - -#include "DFDOM.h" - -void DFComputeChanges(DFNode *root1, DFNode *root2, Tag idAttr); -char *DFChangesToString(DFNode *root); - -#endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/Test.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/Test.c b/consumers/dfutil/src/Test.c index 88d73a6..8b02c63 100644 --- a/consumers/dfutil/src/Test.c +++ b/consumers/dfutil/src/Test.c @@ -13,7 +13,6 @@ // limitations under the License. #include "Test.h" -#include "TestCase.h" #include "TextPackage.h" #include "Commands.h" #include "TestFunctions.h" http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/TestCase.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/TestCase.c b/consumers/dfutil/src/TestCase.c deleted file mode 100644 index 4639f41..0000000 --- a/consumers/dfutil/src/TestCase.c +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "TestCase.h" -#include "TestFunctions.h" -#include "WordPlain.h" -#include "HTMLPlain.h" -#include "DFCommon.h" -#include "DFString.h" -#include "DFFilesystem.h" -#include "DFUnitTest.h" -#include -#include - -DFStorage *TestCaseOpenPackage(DFError **error) -{ - const char *inputDocx = DFHashTableLookup(utgetdata(),"input.docx"); - if (inputDocx == NULL) { - DFErrorFormat(error,"input.docx not defined"); - return NULL; - } - - return Word_fromPlain(inputDocx,utgetpath(),error); -} - -DFDocument *TestCaseGetHTML(DFStorage *storage, DFError **error) -{ - const char *inputHtml = DFHashTableLookup(utgetdata(),"input.html"); - if (inputHtml == NULL) { - DFErrorFormat(error,"input.html not defined"); - return NULL; - } - return HTML_fromPlain(inputHtml,utgetpath(),storage,error); -} http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/TestCase.h ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/TestCase.h b/consumers/dfutil/src/TestCase.h deleted file mode 100644 index 2aef8c4..0000000 --- a/consumers/dfutil/src/TestCase.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2012-2014 UX Productivity Pty Ltd -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef dfutil_TestCase_h -#define dfutil_TestCase_h - -#include "DFHashTable.h" -#include "DFBuffer.h" -#include -#include - -DFStorage *TestCaseOpenPackage(DFError **error); -DFDocument *TestCaseGetHTML(DFStorage *storage, DFError **error); - -#endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/TestFunctions.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/TestFunctions.c b/consumers/dfutil/src/TestFunctions.c index 8065b43..5a2d601 100644 --- a/consumers/dfutil/src/TestFunctions.c +++ b/consumers/dfutil/src/TestFunctions.c @@ -14,7 +14,7 @@ #include "TestFunctions.h" #include "DFBDT.h" -#include "BDTTest.h" +#include "BDTTests.h" #include "WordPlain.h" #include "HTMLPlain.h" #include "Commands.h" @@ -34,394 +34,48 @@ #include #include -typedef void (*TestFunction)(void); - -static void move(void) -{ - if (utgetargc() < 3) { - DFBufferFormat(utgetoutput(),"move: insufficient arguments"); - return; - } - - int count = atoi(utgetargv()[0]); - int from = atoi(utgetargv()[1]); - int to = atoi(utgetargv()[2]); - - DFBuffer *output = DFBufferNew(); - BDT_testMove(count,from,to,output); - DFBufferFormat(utgetoutput(),"%s",output->data); - DFBufferRelease(output); -} - -static void removeChildren(void) -{ - int *indices = (int *)malloc(utgetargc()*sizeof(int)); - - for (int i = 0; i < utgetargc(); i++) { - int index = atoi(utgetargv()[i]); - indices[i] = index; - } - - DFBuffer *output = DFBufferNew(); - BDT_testRemove(indices,utgetargc(),output); - DFBufferFormat(utgetoutput(),"%s",output->data); - DFBufferRelease(output); - - free(indices); -} - -static void CSS_setHeadingNumbering(void) -{ - const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css"); - if (inputCSS == NULL) { - DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: input.css not defined"); - return; - } - if (utgetargc() < 1) { - DFBufferFormat(utgetoutput(),"CSS_setHeadingNumbering: expected 1 argument"); - return; - } - - CSSSheet *styleSheet = CSSSheetNew(); - CSSSheetUpdateFromCSSText(styleSheet,inputCSS); - int on = !strcasecmp(utgetargv()[0],"true"); - CSSSheetSetHeadingNumbering(styleSheet,on); - char *cssText = CSSSheetCopyCSSText(styleSheet); - DFBufferFormat(utgetoutput(),"%s",cssText); - free(cssText); - CSSSheetRelease(styleSheet); -} - -static void Word_testCollapseBookmarks(void) -{ - DFError *error = NULL; - DFStorage *storage = TestCaseOpenPackage(&error); - if (storage == NULL) { - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - DFErrorRelease(error); - return; - } - - if (!WordCollapseBookmarks(storage,&error)) { - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - DFErrorRelease(error); - DFStorageRelease(storage); - return; - } - - DFHashTable *parts = DFHashTableNew((DFCopyFunction)strdup,free); - DFHashTableAdd(parts,"document",""); - - // Output the docx file - char *plain = Word_toPlain(storage,parts); - DFBufferFormat(utgetoutput(),"%s",plain); - free(plain); - DFHashTableRelease(parts); - DFStorageRelease(storage); -} - - -static void Word_testExpandBookmarks(void) -{ - DFError *error = NULL; - DFStorage *storage = TestCaseOpenPackage(&error); - if (storage == NULL) { - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - DFErrorRelease(error); - return; - } - - if (!WordExpandBookmarks(storage,&error)) { - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - DFErrorRelease(error); - DFStorageRelease(storage); - return; - } - - DFHashTable *parts = DFHashTableNew((DFCopyFunction)strdup,free); - DFHashTableAdd(parts,"document",""); - - // Output the docx file - char *plain = Word_toPlain(storage,parts); - DFBufferFormat(utgetoutput(),"%s",plain); - free(plain); - DFHashTableRelease(parts); - DFStorageRelease(storage); -} - -static void Word_testGet(void) -{ - DFError *error = NULL; - DFStorage *abstractStorage = NULL; - DFStorage *concreteStorage = NULL; - DFAbstractDocument *abstractDoc = NULL; - DFConcreteDocument *concreteDoc = NULL; - char *htmlPlain = NULL; - - concreteStorage = TestCaseOpenPackage(&error); - if (concreteStorage == NULL) - goto end; - - concreteDoc = DFConcreteDocumentNew(concreteStorage); - abstractStorage = DFStorageNewMemory(DFFileFormatHTML); - abstractDoc = DFAbstractDocumentNew(abstractStorage); - - if (!DFGet(concreteDoc,abstractDoc,&error)) - goto end;; - - DFDocument *htmlDoc = DFAbstractDocumentGetHTML(abstractDoc); - if (htmlDoc == NULL) { - DFErrorFormat(&error,"Abstract document has no HTML"); - goto end; - } - - htmlPlain = HTML_toPlain(htmlDoc,abstractStorage,&error); - -end: - if (htmlPlain != NULL) - DFBufferFormat(utgetoutput(),"%s",htmlPlain); - else - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - - DFErrorRelease(error); - DFStorageRelease(abstractStorage); - DFStorageRelease(concreteStorage); - DFAbstractDocumentRelease(abstractDoc); - DFConcreteDocumentRelease(concreteDoc); - free(htmlPlain); -} - -static DFHashTable *getFlags(void) -{ - if (utgetargc() == 0) - return NULL;; - DFHashTable *set = DFHashTableNew((DFCopyFunction)strdup,free); - for (int i = 0; i < utgetargc(); i++) { - const char *colon = strchr(utgetargv()[i],':'); - if (colon == NULL) - continue; - size_t colonPos = colon - utgetargv()[i]; - char *rawName = DFSubstring(utgetargv()[i],0,colonPos); - char *rawValue = DFSubstring(utgetargv()[i],colonPos+1,strlen(utgetargv()[i])); - char *name = DFStringTrimWhitespace(rawName); - char *value = DFStringTrimWhitespace(rawValue); - if (!strcasecmp(value,"true")) - DFHashTableAdd(set,name,name); - - free(rawName); - free(rawValue); - free(name); - free(value); - } - return set; -} - -static void Word_testCreate(void) -{ - DFError *error = NULL; - DFDocument *htmlDoc = NULL; - DFStorage *abstractStorage = DFStorageNewMemory(DFFileFormatHTML); - DFStorage *concreteStorage = DFStorageNewMemory(DFFileFormatDocx); - DFAbstractDocument *abstractDoc = DFAbstractDocumentNew(abstractStorage); - DFConcreteDocument *concreteDoc = DFConcreteDocumentNew(concreteStorage); - DFHashTable *parts = NULL; - char *wordPlain = NULL; - - // Read input.html - htmlDoc = TestCaseGetHTML(abstractStorage,&error); - if (htmlDoc == NULL) - goto end; - - DFAbstractDocumentSetHTML(abstractDoc,htmlDoc); - - if (!DFCreate(concreteDoc,abstractDoc,&error)) - goto end; - - parts = getFlags(); - wordPlain = Word_toPlain(concreteStorage,parts); - -end: - if (wordPlain != NULL) - DFBufferFormat(utgetoutput(),"%s",wordPlain); - else - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - - DFErrorRelease(error); - DFDocumentRelease(htmlDoc); - DFStorageRelease(abstractStorage); - DFStorageRelease(concreteStorage); - DFAbstractDocumentRelease(abstractDoc); - DFConcreteDocumentRelease(concreteDoc); - DFHashTableRelease(parts); - free(wordPlain); -} - -static void Word_testUpdate(void) -{ - DFError *error = NULL; - DFDocument *htmlDoc = NULL; - DFStorage *abstractStorage = NULL; - DFStorage *concreteStorage = NULL; - DFAbstractDocument *abstractDoc = NULL; - DFConcreteDocument *concreteDoc = NULL; - DFHashTable *parts = NULL; - char *wordPlain = NULL; - - concreteStorage = TestCaseOpenPackage(&error); - if (concreteStorage == NULL) - goto end; - - abstractStorage = DFStorageNewMemory(DFFileFormatHTML); - abstractDoc = DFAbstractDocumentNew(abstractStorage); - concreteDoc = DFConcreteDocumentNew(concreteStorage); - - // Read input.html - htmlDoc = TestCaseGetHTML(abstractStorage,&error); - if (htmlDoc == NULL) - goto end; - - DFAbstractDocumentSetHTML(abstractDoc,htmlDoc); - - // Update the docx file based on the contents of the HTML file - if (!DFPut(concreteDoc,abstractDoc,&error)) - goto end; - - // Output the updated docx file - parts = getFlags(); - wordPlain = Word_toPlain(concreteStorage,parts); +// BDTTests.c +void test_move(void); +void test_removeChildren(void); -end: - if (wordPlain != NULL) - DFBufferFormat(utgetoutput(),"%s",wordPlain); - else - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - - DFErrorRelease(error); - DFDocumentRelease(htmlDoc); - DFStorageRelease(abstractStorage); - DFStorageRelease(concreteStorage); - DFAbstractDocumentRelease(abstractDoc); - DFConcreteDocumentRelease(concreteDoc); - DFHashTableRelease(parts); - free(wordPlain); -} - -static void LaTeX_testCreate(void) -{ - DFError *error = NULL; - DFStorage *htmlStorage = DFStorageNewMemory(DFFileFormatHTML); - DFDocument *htmlDoc = TestCaseGetHTML(htmlStorage,&error); - DFStorageRelease(htmlStorage); - if (htmlDoc == NULL) { - DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error)); - DFErrorRelease(error); - return; - } - - HTML_normalizeDocument(htmlDoc); - char *latex = HTMLToLaTeX(htmlDoc); - DFBufferFormat(utgetoutput(),"%s",latex); - free(latex); - DFDocumentRelease(htmlDoc); -} - -static void HTML_testNormalize(void) -{ - const char *inputHtml = DFHashTableLookup(utgetdata(),"input.html"); - if (inputHtml == NULL) { - DFBufferFormat(utgetoutput(),"input.html not defined"); - return; - } - DFError *error = NULL; - DFDocument *doc = DFParseHTMLString(inputHtml,0,&error); - if (doc == NULL) { - DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); - DFErrorRelease(error); - return; - } - HTML_normalizeDocument(doc); - HTML_safeIndent(doc->docNode,0); - char *docStr = DFSerializeXMLString(doc,0,0); - DFBufferFormat(utgetoutput(),"%s",docStr); - free(docStr); - DFDocumentRelease(doc); -} - -static void HTML_showChanges(void) -{ - const char *input1 = DFHashTableLookup(utgetdata(),"input1.html"); - if (input1 == NULL) { - DFBufferFormat(utgetoutput(),"input.html not defined"); - return; - } - - const char *input2 = DFHashTableLookup(utgetdata(),"input2.html"); - if (input2 == NULL) { - DFBufferFormat(utgetoutput(),"input.html not defined"); - return; - } +// CSSTests.c +void test_CSS_setHeadingNumbering(void); +void test_CSS_parse(void); +// WordTests.c +void test_Word_testCollapseBookmarks(void); +void test_Word_testExpandBookmarks(void); +void test_Word_testGet(void); +void test_Word_testCreate(void); +void test_Word_testUpdate(void); - DFError *error = NULL; - DFDocument *doc1 = DFParseHTMLString(input1,0,&error); - if (doc1 == NULL) { - DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); - DFErrorRelease(error); - return; - } +// LaTeXTests.c +void test_LaTeX_testCreate(void); - DFDocument *doc2 = DFParseHTMLString(input2,0,&error); - if (doc2 == NULL) { - DFBufferFormat(utgetoutput(),"%s",DFErrorMessage(&error)); - DFErrorRelease(error); - DFDocumentRelease(doc1); - } +// HTMLTests.c +void test_HTML_testNormalize(void); +void test_HTML_showChanges(void); - DFComputeChanges(doc1->root,doc2->root,HTML_ID); - char *changesStr = DFChangesToString(doc1->root); - DFBufferFormat(utgetoutput(),"%s",changesStr); - free(changesStr); - DFDocumentRelease(doc1); - DFDocumentRelease(doc2); -} -static void CSS_test(void) -{ - const char *inputCSS = DFHashTableLookup(utgetdata(),"input.css"); - if (inputCSS == NULL) { - DFBufferFormat(utgetoutput(),"input.css not defined"); - return; - } - CSSSheet *styleSheet = CSSSheetNew(); - CSSSheetUpdateFromCSSText(styleSheet,inputCSS); - char *text = CSSSheetCopyText(styleSheet); - DFBufferFormat(utgetoutput(),"%s",text); - free(text); - DFBufferFormat(utgetoutput(), - "================================================================================\n"); - char *cssText = CSSSheetCopyCSSText(styleSheet); - DFBufferFormat(utgetoutput(),"%s",cssText); - free(cssText); - CSSSheetRelease(styleSheet); -} +typedef void (*TestFunction)(void); static struct { const char *name; TestFunction fun; } testFunctions[] = { - { "CSS_setHeadingNumbering", CSS_setHeadingNumbering }, - { "Word_testCollapseBookmarks", Word_testCollapseBookmarks }, - { "Word_testExpandBookmarks", Word_testExpandBookmarks }, - { "Word_testGet", Word_testGet }, - { "Word_testCreate", Word_testCreate }, - { "Word_testUpdate", Word_testUpdate }, - { "LaTeX_testCreate", LaTeX_testCreate }, - { "HTML_testNormalize", HTML_testNormalize }, - { "HTML_showChanges", HTML_showChanges }, - { "CSS_test", CSS_test }, - { "move", move }, - { "remove", removeChildren }, + { "CSS_setHeadingNumbering", test_CSS_setHeadingNumbering }, + { "Word_testCollapseBookmarks", test_Word_testCollapseBookmarks }, + { "Word_testExpandBookmarks", test_Word_testExpandBookmarks }, + { "Word_testGet", test_Word_testGet }, + { "Word_testCreate", test_Word_testCreate }, + { "Word_testUpdate", test_Word_testUpdate }, + { "LaTeX_testCreate", test_LaTeX_testCreate }, + { "HTML_testNormalize", test_HTML_testNormalize }, + { "HTML_showChanges", test_HTML_showChanges }, + { "CSS_test", test_CSS_parse }, + { "move", test_move }, + { "remove", test_removeChildren }, { NULL, NULL }, }; http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/TestFunctions.h ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/TestFunctions.h b/consumers/dfutil/src/TestFunctions.h index f89d8fe..d98d59d 100644 --- a/consumers/dfutil/src/TestFunctions.h +++ b/consumers/dfutil/src/TestFunctions.h @@ -15,8 +15,6 @@ #ifndef dfutil_TestFunctions_h #define dfutil_TestFunctions_h -#include "TestCase.h" - void runTest(const char *name); #endif http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/d1aca0ca/consumers/dfutil/src/main.c ---------------------------------------------------------------------- diff --git a/consumers/dfutil/src/main.c b/consumers/dfutil/src/main.c index 6a0ac69..5348cc0 100644 --- a/consumers/dfutil/src/main.c +++ b/consumers/dfutil/src/main.c @@ -13,7 +13,7 @@ // limitations under the License. #include "Commands.h" -#include "BDTTest.h" +#include "BDTTests.h" #include "Test.h" #include "WordPlain.h" #include "HTMLPlain.h"