summaryrefslogtreecommitdiff
path: root/src/unit/taf.cpp
blob: e4b3ccc264f94f8f6c18bfb9ebe3d9ce67de384b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/unitsuite.h"
#include "bu/file.h"
#include "bu/tafreader.h"
#include "bu/tafwriter.h"
#include "bu/membuf.h"

#include <string.h>
#include <unistd.h>

class Unit : public Bu::UnitSuite
{
public:
	Unit()
	{
		setName("taf");
		addTest( Unit::read1 );
		addTest( Unit::encode1 );
		addTest( Unit::emptyStr1 );
		addTest( Unit::incomplete1 );
	}

	virtual ~Unit()
	{
	}

	void read1()
	{
#define FN_TMP ("/tmp/tmpXXXXXXXX")
		Bu::FString sFnTmp(FN_TMP);
		Bu::File fOut = Bu::File::tempFile( sFnTmp );
		const char *data =
"{test: name=\"Bob\"}"
;
		fOut.write(data,strlen(data));
		fOut.close();

		Bu::File fIn(sFnTmp.getStr(), Bu::File::Read );
		Bu::TafReader tr(fIn);
		
		Bu::TafGroup *tn = tr.readGroup();
		unitTest( !strcmp("Bob", tn->getProperty("name").getStr()) );
		delete tn;

		unlink(sFnTmp.getStr());
#undef FN_TMP
	}

	void encode1()
	{
		Bu::MemBuf mb;
		Bu::TafWriter tw( mb );
		
		Bu::TafGroup g("Test data");
		Bu::FString sData( 256 );
		for( int j = 0; j < 256; j++ )
			sData[j] = (unsigned char)j;
		g.addChild( new Bu::TafProperty("Encoded", sData) );
		tw.writeGroup( &g );

		static const char *cmpdata = "{\"Test data\":\n    \"Encoded\"=\""
			"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07"
			"\\x08\\x09\\x0A\\x0B\\x0C\\x0D\\x0E\\x0F"
			"\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17"
			"\\x18\\x19\\x1A\\x1B\\x1C\\x1D\\x1E\\x1F"
			" !\\\"#$%&'()*+,-./0123456789:;<=>?@ABCD"
			"EFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghi"
			"jklmnopqrstuvwxyz{|}~\\x7F"
			"\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87"
			"\\x88\\x89\\x8A\\x8B\\x8C\\x8D\\x8E\\x8F"
			"\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97"
			"\\x98\\x99\\x9A\\x9B\\x9C\\x9D\\x9E\\x9F"
			"\\xA0\\xA1\\xA2\\xA3\\xA4\\xA5\\xA6\\xA7"
			"\\xA8\\xA9\\xAA\\xAB\\xAC\\xAD\\xAE\\xAF"
			"\\xB0\\xB1\\xB2\\xB3\\xB4\\xB5\\xB6\\xB7"
			"\\xB8\\xB9\\xBA\\xBB\\xBC\\xBD\\xBE\\xBF"
			"\\xC0\\xC1\\xC2\\xC3\\xC4\\xC5\\xC6\\xC7"
			"\\xC8\\xC9\\xCA\\xCB\\xCC\\xCD\\xCE\\xCF"
			"\\xD0\\xD1\\xD2\\xD3\\xD4\\xD5\\xD6\\xD7"
			"\\xD8\\xD9\\xDA\\xDB\\xDC\\xDD\\xDE\\xDF"
			"\\xE0\\xE1\\xE2\\xE3\\xE4\\xE5\\xE6\\xE7"
			"\\xE8\\xE9\\xEA\\xEB\\xEC\\xED\\xEE\\xEF"
			"\\xF0\\xF1\\xF2\\xF3\\xF4\\xF5\\xF6\\xF7"
			"\\xF8\\xF9\\xFA\\xFB\\xFC\\xFD\\xFE\\xFF\"\n}\n";
		unitTest( mb.getString() == cmpdata );
		mb.setPos( 0 );
		Bu::TafReader tr( mb );
		Bu::TafGroup *rg = tr.readGroup();
		unitTest( rg->getProperty("Encoded") == sData );
		delete rg;
	}

	void emptyStr1()
	{
		Bu::MemBuf mb;
		Bu::TafWriter tw( mb );

		Bu::TafGroup g("Test Group");
		Bu::FString sVal;
		g.addChild( new Bu::TafProperty("Lame", sVal) );
		tw.writeGroup( &g );

		unitTest(
			mb.getString() == "{\"Test Group\":\n    \"Lame\"=\"\"\n}\n" );
	}

	void incomplete1()
	{
		try
		{
			Bu::MemBuf mb("{Lame: \"Hello=\"");
			Bu::TafReader tr( mb );
			delete tr.readGroup();
			unitFailed("Should have thrown an exception, didn't.");
		}
		catch( Bu::TafException &e )
		{
			// Woot
		}
	}
};

int main( int argc, char *argv[] )
{
	return Unit().run( argc, argv );
}