source: josm/trunk/test/unit/org/openstreetmap/josm/tools/TextTagParserTest.java@ 9504

Last change on this file since 9504 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.Map;
7
8import org.junit.Assert;
9import org.junit.BeforeClass;
10import org.junit.Test;
11import org.openstreetmap.josm.JOSMFixture;
12
13/**
14 * Unit tests of {@link TextTagParser} class.
15 */
16public class TextTagParserTest {
17
18 /**
19 * Setup test.
20 */
21 @BeforeClass
22 public static void setUp() {
23 JOSMFixture.createUnitTestFixture().init();
24 }
25
26 /**
27 * Test of {@link TextTagParser#unescape} method.
28 */
29 @Test
30 public void testUnescape() {
31 String s, s1;
32 s = "\"2 3 4\""; s1 = "2 3 4";
33 Assert.assertEquals(s1, TextTagParser.unescape(s));
34
35 s = "\"2 \\\"3\\\" 4\""; s1 = "2 \"3\" 4";
36 Assert.assertEquals(s1, TextTagParser.unescape(s));
37
38 s = "\"2 3 ===4===\""; s1 = "2 3 ===4===";
39 Assert.assertEquals(s1, TextTagParser.unescape(s));
40
41 s = "\"2 3 \\\\\\\\===4===\""; s1 = "2 3 \\\\===4===";
42 Assert.assertEquals(s1, TextTagParser.unescape(s));
43 }
44
45 /**
46 * Test of {@link TextTagParser#readTagsFromText} method with tabs and new lines.
47 */
48 @Test
49 public void testTNformat() {
50 String txt = " a \t 1 \n\n\n b\t2 \n c \t the value with \"quotes\"";
51 Map<String, String> correctTags = new HashMap<String, String>() { {
52 put("a", "1"); put("b", "2"); put("c", "the value with \"quotes\"");
53 } };
54 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
55 Assert.assertEquals(correctTags, tags);
56 }
57
58 /**
59 * Test of {@link TextTagParser#readTagsFromText} method with quotes.
60 */
61 @Test
62 public void testEQformat() {
63 String txt = "key1=value key2=\"long value\" tag3=\"hotel \\\"Quote\\\"\"";
64 Map<String, String> correctTags = new HashMap<String, String>() { {
65 put("key1", "value"); put("key2", "long value");
66 put("tag3", "hotel \"Quote\"");
67 } };
68 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
69 Assert.assertEquals(correctTags, tags);
70 }
71
72 /**
73 * Test of {@link TextTagParser#readTagsFromText} method with JSON.
74 */
75 @Test
76 public void testJSONformat() {
77 String txt;
78 Map<String, String> tags, correctTags;
79 txt = "{ \"a\":\"1\", \"b\":\"2 3 4\" }";
80 correctTags = new HashMap<String, String>() { { put("a", "1"); put("b", "2 3 4"); } };
81 tags = TextTagParser.readTagsFromText(txt);
82 Assert.assertEquals(correctTags, tags);
83
84 txt = "\"a\" : \"1 1 1\", \"b2\" :\"2 \\\"3 qwe\\\" 4\"";
85 correctTags = new HashMap<String, String>() { { put("a", "1 1 1"); put("b2", "2 \"3 qwe\" 4"); } };
86 tags = TextTagParser.readTagsFromText(txt);
87 Assert.assertEquals(correctTags, tags);
88
89 txt = " \"aыыы\" : \"val\\\"\\\"\\\"ue1\"";
90 correctTags = new HashMap<String, String>() { { put("aыыы", "val\"\"\"ue1"); } };
91 tags = TextTagParser.readTagsFromText(txt);
92 Assert.assertEquals(correctTags, tags);
93 }
94
95 /**
96 * Test of {@link TextTagParser#readTagsFromText} method with free format.
97 */
98 @Test
99 public void testFreeformat() {
100 String txt = "a 1 b=2 c=\"hello === \\\"\\\"world\"";
101 Map<String, String> correctTags = new HashMap<String, String>() { {
102 put("a", "1"); put("b", "2"); put("c", "hello === \"\"world");
103 } };
104 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
105 Assert.assertEquals(correctTags, tags);
106 }
107
108 /**
109 * Test of {@link TextTagParser#readTagsFromText} method (error detection).
110 */
111 @Test
112 public void errorDetect() {
113 String txt = "a=2 b=3 4";
114 Map<String, String> tags = TextTagParser.readTagsFromText(txt);
115 Assert.assertEquals(Collections.EMPTY_MAP, tags);
116 }
117
118 /**
119 * Test of {@link TextTagParser#readTagsFromText} method with tabs.
120 */
121 @Test
122 public void testTab() {
123 Assert.assertEquals(TextTagParser.readTagsFromText("shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
124 Assert.assertEquals(TextTagParser.readTagsFromText("!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
125 Assert.assertEquals(TextTagParser.readTagsFromText("!!!shop\tjewelry"), Collections.singletonMap("shop", "jewelry"));
126 Assert.assertEquals(TextTagParser.readTagsFromText("shop\t\t\tjewelry"), Collections.singletonMap("shop", "jewelry"));
127 }
128}
Note: See TracBrowser for help on using the repository browser.