summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.foundation/src/test/java/net/wotonomy/foundation/NSXMLPropertyListTest.java
blob: 0427f9ee19cecf24fae7c491f1a7e5b925c6fae5 (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
package net.wotonomy.foundation;

import static org.junit.Assert.*;

import org.junit.Test;

public class NSXMLPropertyListTest {

	@Test
	public void testStringParse() {
		var res = NSXMLPropertyList.propertyListFromString("<plist><string>abc</string></plist>");
		if (res.isLeft()) {
			NSPropertyList val = res.forceLeft();
			if (val.type != NSPropertyList.Type.STRING) {
				fail("property list parsed incorrectly - got " + val.type.name() + " instead of string");
			}
			NSPropertyList.String castVal = (NSPropertyList.String) val;
			assertEquals("abc", castVal.getContents());
		} else {
			NSError err = res.forceRight();
			fail("failed to parse property list - " + err.getDescription());
		}
	}

	@Test
	public void testArrayParse() {
		var res = NSXMLPropertyList.propertyListFromString("<plist><array><string>abc</string><string>def</string></array></plist>");
		if (res.isLeft()) {
			NSPropertyList val = res.forceLeft();
			if (val.type != NSPropertyList.Type.ARRAY) {
				fail("property list parsed incorrectly - got " + val.type.name() + " instead of array");
			}
			NSPropertyList.Array castVal = (NSPropertyList.Array) val;
			NSArray<NSPropertyList> contents = castVal.getContents();
			
			assertEquals(2, contents.size());
			assertEquals(NSPropertyList.Type.STRING, contents.get(0).type);
			assertEquals(NSPropertyList.Type.STRING, contents.get(1).type);
			assertEquals("abc", ((NSPropertyList.String)contents.get(0)).getContents());
			assertEquals("def", ((NSPropertyList.String)contents.get(1)).getContents());
		} else {
			NSError err = res.forceRight();
			fail("failed to parse property list - " + err.getDescription());
		}
	}
	
	@Test
	public void testDictParse() {
		var res = NSXMLPropertyList.propertyListFromString("<plist><dict><key>abc</key><integer>5</integer></dict></plist>");
		if (res.isLeft()) {
			NSPropertyList val = res.forceLeft();
			if (val.type != NSPropertyList.Type.DICTIONARY) {
				fail("property list parsed incorrectly - got " + val.type.name() + " instead of dictionary");
			}
			NSPropertyList.Dictionary castVal = (NSPropertyList.Dictionary) val;
			NSDictionary<String, NSPropertyList> contents = castVal.getContents();
			
			assertEquals(1, contents.size());
			assertEquals(NSPropertyList.Type.INTEGER, contents.get("abc").type);
			assertEquals(5, ((NSPropertyList.Integer)contents.get("abc")).getContents());
		} else {
			NSError err = res.forceRight();
			StringBuilder underlyingErrors = new StringBuilder();
			for (NSError underlyingError : err.getUnderlyingErrors()) {
				underlyingErrors.append(underlyingError.getDescription());
				underlyingErrors.append(", ");
			}
			
			fail("failed to parse property list - " + err.getDescription() + "\n - " + underlyingErrors.toString());
		}
	}
}