Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e1816b33316077ef6fb1369b56328dc103ab06f8 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*******************************************************************************
 * Copyright (c) 2009 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.p2.tests.omniVersion;

import junit.framework.TestCase;
import org.eclipse.equinox.p2.metadata.Version;

/**
 * Tests format(s), and format(S)
 * 
 * a string group matching any character except any following 
 * explicit/optional delimiter. Use processing rules =[]; or =[^] to define the set of allowed characters.
 * 
 */
public class FormatSTest extends TestCase {
	public void testStringAcceptDigit() {
		Version v = Version.parseVersion("format(S):1");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'1'"), v);
		try {
			Version.parseVersion("format(s):1");
			fail("Uncaught error: s should not accept digits");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
	}

	public void testStringAcceptAlpha() {
		Version v = Version.parseVersion("format(s):a");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'a'"), v);

		assertNotNull(v = Version.parseVersion("format(S):a"));
		assertEquals(Version.parseVersion("raw:'a'"), v);
	}

	public void testStringDelimitedByNumeric() {
		Version v = Version.parseVersion("format(sn):foobar123");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'foobar'.123"), v);
		try {
			Version.parseVersion("format(Sn):foobar123");
			fail("Uncaught error: S should eat entire string, no n found at the end");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
	}

	public void testStringWithSpace() {
		Version v;

		assertNotNull(v = Version.parseVersion("format(S=[^0-9];n):foo bar123"));
		assertEquals(Version.parseVersion("raw:'foo bar'.123"), v);

		// Test 's' with attempt to include 'space' and delimiters
		//
		try {
			Version.parseVersion("format(s=[^];n):foo bar123");
			fail("Uncaught error: format(s) can not match non letters (space).");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
	}

	public void testStringDelimitedByDelimiter() {
		Version v = Version.parseVersion("format(s.n):foobar.123");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'foobar'.123"), v);

		assertNotNull(v = Version.parseVersion("format(S=[^.];.n):foobar.123"));
		assertEquals(Version.parseVersion("raw:'foobar'.123"), v);
	}

	public void testStringDelimitedByExplicitDelimiter() {
		Version v = Version.parseVersion("format(s=[^r];d=[r];n):foobar123");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'fooba'.123"), v);
	}

	public void testStringWithAllowedSet() {
		Version v = Version.parseVersion("format(s=[a-z];sn):fooBAR123");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'foo'.'BAR'.123"), v);
	}

	public void testStringWithDisallowedSet() {
		Version v = Version.parseVersion("format(s=[^a-z];sn):FOObar123");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'FOO'.'bar'.123"), v);
	}

	public void testExact() {
		Version v = Version.parseVersion("format(S={4};S):123abc456'def'");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'123a'.\"bc456'def'\""), v);

		assertNotNull(v = Version.parseVersion("format(s={2};s):abc"));
		assertEquals(Version.parseVersion("raw:'ab'.'c'"), v);

		assertNotNull(v = Version.parseVersion("format(S={2};S):abc"));
		assertEquals(Version.parseVersion("raw:'ab'.'c'"), v);

		assertNotNull(v = Version.parseVersion("format(S={2};S):123"));
		assertEquals(Version.parseVersion("raw:'12'.'3'"), v);

		assertNotNull(v = Version.parseVersion("format(S={4};S={1};S={3};):123abc45"));
		assertEquals(Version.parseVersion("raw:'123a'.'b'.'c45'"), v);

		assertNotNull(v = Version.parseVersion("format(S={2};.S={1};s={3};):12.3abc"));
		assertEquals(Version.parseVersion("raw:'12'.'3'.'abc'"), v);

		try {
			Version.parseVersion("format(s={4};.s):aaa.abc456'def'");
			fail("Uncaught error: first segment is less than 4 chars long");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		try {
			Version.parseVersion("format(s={4};.s):123.abc456'def'");
			fail("Uncaught error: first segment has digits");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		try {
			Version.parseVersion("format(S={4}=[^.];.S):123.abc456'def'");
			fail("Uncaught error: first segment has only 3 characters");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}

	}

	/**
	 * Test that unbound upper range is just a limit on lower range. Upper delimiter must be a delimiter.
	 */
	public void testAtLeast() {
		Version v = Version.parseVersion("format(S={2,};):123abc456'def'");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:\"123abc456'def'\""), v);

		assertNotNull(v = Version.parseVersion("format(S={2,};=[^.];.S):123a.bc456'def'"));
		assertEquals(Version.parseVersion("raw:'123a'.\"bc456'def'\""), v);

		try {
			Version.parseVersion("format(s={2,};.S):a.abc456'def'");
			fail("uncaught error: first segment is shorter than 2");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		try {
			Version.parseVersion("format(s={2,};.s={10,};):aa.abcd");
			fail("Uncaught error: secont segment too short");
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
	}

	public void testAtMost() {
		Version v = Version.parseVersion("format(S={1,3};S={1,2};S):123abc456'def'");
		assertNotNull(v);
		assertEquals(Version.parseVersion("raw:'123'.'ab'.\"c456'def'\""), v);

		// delimited by delimiter
		assertNotNull(v = Version.parseVersion("format(S={1,4};=[^.];.S={1,4};.S):123.abc4.56'def'"));
		assertEquals(Version.parseVersion("raw:'123'.'abc4'.\"56'def'\""), v);

		try {
			// fails because of delimiter after one char
			Version.parseVersion("format(s={2,3};s):a.abc456'def'");
			fail();
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		try {
			// fails because there are trailing characters after 'c'
			Version.parseVersion("format(s={2,3};.S={2,3};):aa.abc456'd'");
			fail();
		} catch (IllegalArgumentException e) {
			assertTrue(true);
		}
	}
}

Back to the top