Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6d73f3e9704c500ad2d9751af069bccd7fbc15b (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
/*******************************************************************************
 * Copyright (c) 2009 Mat Booth 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
 *******************************************************************************/
package org.eclipse.dltk.sh.ui.text.tests;

import org.eclipse.dltk.sh.ui.text.DollarBraceCountingRule;
import org.eclipse.dltk.sh.ui.text.IShellPartitions;
import org.eclipse.jface.text.rules.Token;
import org.junit.Assert;
import org.junit.Test;

/**
 * Tests the dollar-prefixed brace-counting single-line rule. Basically we're
 * testing to make sure syntax highlighting is correct for ${} and $() blocks.
 */
public class DollarBraceCountingRuleTest {

	// Mock objects
	private MockScanner fScanner;

	// Objects under test
	private static DollarBraceCountingRule fRule = new DollarBraceCountingRule(
			'{', '}', new Token(IShellPartitions.PARAM_CONTENT_TYPE), '\\');

	/**
	 * Highlight a simple pair of dollar-prefixed braces.
	 */
	@Test
	public void testSimpleMatch() {
		fScanner = new MockScanner("${basic}test}");
		Assert.assertFalse(fRule.evaluate(fScanner).isUndefined());
		Assert.assertEquals("${basic}", fScanner.getBuffer().substring(0,
				fScanner.getOffset()));
	}

	/**
	 * Highlight a pair of braces enclosing escaped braces.
	 */
	@Test
	public void testEscapedBraceMatch() {
		fScanner = new MockScanner("${basic\\}\\}}test}");
		Assert.assertFalse(fRule.evaluate(fScanner).isUndefined());
		Assert.assertEquals("${basic\\}\\}}", fScanner.getBuffer().substring(0,
				fScanner.getOffset()));
	}

	/**
	 * Highlight the correct amount of nested braces.
	 */
	@Test
	public void testNestedBraceMatch() {
		fScanner = new MockScanner("${a{b{cd}e}f}g}");
		Assert.assertFalse(fRule.evaluate(fScanner).isUndefined());
		Assert.assertEquals("${a{b{cd}e}f}", fScanner.getBuffer().substring(0,
				fScanner.getOffset()));
	}

	/**
	 * Highlight everything up to the end of the line, if that occurs before the
	 * final closing brace.
	 */
	@Test
	public void testEndOfLineMatch() {
		fScanner = new MockScanner("${a{b}c\nd}");
		Assert.assertFalse(fRule.evaluate(fScanner).isUndefined());
		Assert.assertEquals("${a{b}c\n", fScanner.getBuffer().substring(0,
				fScanner.getOffset()));
	}

	/**
	 * Do not highlight if EOF interrupts the pattern.
	 */
	@Test
	public void testEndOfFileMatch() {
		fScanner = new MockScanner("${end of file is here:");
		Assert.assertTrue(fRule.evaluate(fScanner).isUndefined());
		Assert.assertEquals("", fScanner.getBuffer().substring(0,
				fScanner.getOffset()));
	}
}

Back to the top