summaryrefslogtreecommitdiff
path: root/src/unit/taf.unit
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit/taf.unit')
-rw-r--r--src/unit/taf.unit112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/unit/taf.unit b/src/unit/taf.unit
new file mode 100644
index 0000000..5588c85
--- /dev/null
+++ b/src/unit/taf.unit
@@ -0,0 +1,112 @@
1// vim: syntax=cpp
2/*
3 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
4 *
5 * This file is part of the libbu++ library and is released under the
6 * terms of the license contained in the file LICENSE.
7 */
8
9#include "bu/file.h"
10#include "bu/tafreader.h"
11#include "bu/tafwriter.h"
12#include "bu/membuf.h"
13
14#include <string.h>
15#include <unistd.h>
16
17{=Init}
18
19{%read1}
20{
21#define FN_TMP ("/tmp/tmpXXXXXXXX")
22 Bu::FString sFnTmp(FN_TMP);
23 Bu::File fOut = Bu::File::tempFile( sFnTmp );
24 const char *data =
25"{test: name=\"Bob\"}"
26;
27 fOut.write(data,strlen(data));
28 fOut.close();
29
30 Bu::File fIn(sFnTmp.getStr(), Bu::File::Read );
31 Bu::TafReader tr(fIn);
32
33 Bu::TafGroup *tn = tr.readGroup();
34 unitTest( !strcmp("Bob", tn->getProperty("name").getStr()) );
35 delete tn;
36
37 unlink(sFnTmp.getStr());
38#undef FN_TMP
39}
40
41{%encode1}
42{
43 Bu::MemBuf mb;
44 Bu::TafWriter tw( mb );
45
46 Bu::TafGroup g("Test data");
47 Bu::FString sData( 256 );
48 for( int j = 0; j < 256; j++ )
49 sData[j] = (unsigned char)j;
50 g.addChild( new Bu::TafProperty("Encoded", sData) );
51 tw.writeGroup( &g );
52
53 static const char *cmpdata = "{\"Test data\":\n \"Encoded\"=\""
54 "\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07"
55 "\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F"
56 "\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17"
57 "\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F"
58 " !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCD"
59 "EFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghi"
60 "jklmnopqrstuvwxyz{|}~\\x7F"
61 "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87"
62 "\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F"
63 "\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97"
64 "\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F"
65 "\\xA0\\xA1\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7"
66 "\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF"
67 "\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7"
68 "\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD\\xBE\\xBF"
69 "\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7"
70 "\\xC8\\xC9\\xCA\\xCB\\xCC\\xCD\\xCE\\xCF"
71 "\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7"
72 "\\xD8\\xD9\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF"
73 "\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7"
74 "\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF"
75 "\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7"
76 "\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\"\n}\n";
77 unitTest( mb.getString() == cmpdata );
78 mb.setPos( 0 );
79 Bu::TafReader tr( mb );
80 Bu::TafGroup *rg = tr.readGroup();
81 unitTest( rg->getProperty("Encoded") == sData );
82 delete rg;
83}
84
85{%emptyStr1}
86{
87 Bu::MemBuf mb;
88 Bu::TafWriter tw( mb );
89
90 Bu::TafGroup g("Test Group");
91 Bu::FString sVal;
92 g.addChild( new Bu::TafProperty("Lame", sVal) );
93 tw.writeGroup( &g );
94
95 unitTest(
96 mb.getString() == "{\"Test Group\":\n \"Lame\"=\"\"\n}\n" );
97}
98
99{%incomplete1}
100{
101 try
102 {
103 Bu::MemBuf mb("{Lame: \"Hello=\"");
104 Bu::TafReader tr( mb );
105 delete tr.readGroup();
106 unitFailed("Should have thrown an exception, didn't.");
107 }
108 catch( Bu::TafException &e )
109 {
110 // Woot
111 }
112}