Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ab6f0371c49e627ff67b1576eae2af1c9920a5c (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
/*******************************************************************************
 * Copyright (c) 2010 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 static org.junit.Assert.assertEquals;

import org.eclipse.dltk.sh.ui.Activator;
import org.eclipse.dltk.sh.ui.IShellColorConstants;
import org.eclipse.dltk.sh.ui.text.DoubleQuoteScanner;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.junit.Test;

/**
 * Test the scanner for double quote partitions.
 */
public class DoubleQuoteScannerTest extends AbstractScannerTester {

	@Override
	protected RuleBasedScanner getScanner() {
		return new DoubleQuoteScanner(cm, Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected String getText() {
		return "x${var}x$var`eval`x$(eval)";
	}

	/**
	 * Tests that we correctly highlight $dollar style parameter expansions
	 * within interpolated strings.
	 */
	@Test
	public void testDollar() {
		IToken token = getNthToken(4);
		assertEquals(4, scanner.getTokenLength());
		assertEquals(8, scanner.getTokenOffset());
		TextAttribute ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_VARIABLE));
	}

	/**
	 * Tests that we correctly highlight ${dollar-brace} style parameter
	 * expansions within interpolated strings.
	 */
	@Test
	public void testDollarBrace() {
		IToken token = getNthToken(2);
		assertEquals(6, scanner.getTokenLength());
		assertEquals(1, scanner.getTokenOffset());
		TextAttribute ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_VARIABLE));
	}

	/**
	 * Tests that we correctly highlight `back-tick` style command substitutions
	 * within interpolated strings.
	 */
	@Test
	public void testEval() {
		IToken token = getNthToken(5);
		assertEquals(6, scanner.getTokenLength());
		assertEquals(12, scanner.getTokenOffset());
		TextAttribute ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_EVAL));
	}

	/**
	 * Tests that we correctly highlight $(dollar-brace) style command
	 * substitutions within interpolated strings.
	 */
	@Test
	public void testDollarEval() {
		IToken token = getNthToken(7);
		assertEquals(7, scanner.getTokenLength());
		assertEquals(19, scanner.getTokenOffset());
		TextAttribute ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_EVAL));
	}

	/**
	 * Everything else should have the default highlighting for this partition
	 * type.
	 */
	@Test
	public void testDefault() {
		IToken token = getNthToken(1);
		assertEquals(1, scanner.getTokenLength());
		assertEquals(0, scanner.getTokenOffset());
		TextAttribute ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_DOUBLE_QUOTE));

		token = getNthToken(2);
		assertEquals(1, scanner.getTokenLength());
		assertEquals(7, scanner.getTokenOffset());
		ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_DOUBLE_QUOTE));

		token = getNthToken(3);
		assertEquals(1, scanner.getTokenLength());
		assertEquals(18, scanner.getTokenOffset());
		ta = (TextAttribute) token.getData();
		assertEquals(ta.getForeground(), cm.getColor(IShellColorConstants.SHELL_DOUBLE_QUOTE));
	}
}

Back to the top