comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTagTest.java @ 21647:ae601ad0b023

Truffle/Source: add some tests for SourceSections, including a reported design issue with empty Soruces.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Sun, 31 May 2015 17:19:04 -0700
parents graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTest.java@c8b83aa6cc82
children
comparison
equal deleted inserted replaced
21615:838f005f9aec 21647:ae601ad0b023
1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.test.source;
24
25 import static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.source.*;
30
31 public class SourceTagTest {
32
33 @Test
34 public void sourceTagTest() {
35
36 // Private tag
37 final SourceTag testTag = new SourceTag() {
38
39 public String name() {
40 return null;
41 }
42
43 public String getDescription() {
44 return null;
45 }
46 };
47
48 // No sources exist with the private tag
49 assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0);
50
51 // Create a new source
52 final Source source = Source.fromText("test1 source", "test1 source");
53
54 // Initially has only the default tag
55 assertEquals(source.getSourceTags().size(), 1);
56
57 assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
58 assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
59 assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
60
61 assertFalse(source.isTaggedAs(testTag));
62 assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0);
63
64 // Add a private tag
65 source.tagAs(testTag);
66
67 // Now there are exactly two tags
68 assertEquals(source.getSourceTags().size(), 2);
69
70 assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
71 assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
72 assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
73
74 assertTrue(source.getSourceTags().contains(testTag));
75 assertTrue(source.isTaggedAs(testTag));
76 assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1);
77 assertTrue(Source.findSourcesTaggedAs(testTag).contains(source));
78
79 // Add the private tag again
80 source.tagAs(testTag);
81
82 // Nothing has changed
83 assertEquals(source.getSourceTags().size(), 2);
84
85 assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
86 assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
87 assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
88
89 assertTrue(source.getSourceTags().contains(testTag));
90 assertTrue(source.isTaggedAs(testTag));
91 assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1);
92 assertTrue(Source.findSourcesTaggedAs(testTag).contains(source));
93 }
94
95 @Test
96 public void sourceListenerTest() {
97
98 // Private tag
99 final SourceTag testTag = new SourceTag() {
100
101 public String name() {
102 return null;
103 }
104
105 public String getDescription() {
106 return null;
107 }
108 };
109
110 final int[] newSourceEvents = {0};
111 final Source[] newSource = {null};
112
113 final int[] newTagEvents = {0};
114 final Source[] taggedSource = {null};
115 final SourceTag[] newTag = {null};
116
117 Source.addSourceListener(new SourceListener() {
118
119 public void sourceCreated(Source source) {
120 newSourceEvents[0] = newSourceEvents[0] + 1;
121 newSource[0] = source;
122 }
123
124 public void sourceTaggedAs(Source source, SourceTag tag) {
125 newTagEvents[0] = newTagEvents[0] + 1;
126 taggedSource[0] = source;
127 newTag[0] = tag;
128 }
129 });
130
131 // New source has a default tag applied.
132 // Get one event for the new source, another one when it gets tagged
133 final Source source = Source.fromText("testSource", "testSource");
134 assertEquals(newSourceEvents[0], 1);
135 assertEquals(newSource[0], source);
136 assertEquals(newTagEvents[0], 1);
137 assertEquals(taggedSource[0], source);
138 assertEquals(newTag[0], Source.Tags.FROM_LITERAL);
139
140 // reset
141 newSource[0] = null;
142 taggedSource[0] = null;
143 newTag[0] = null;
144
145 // Add a tag; only get one event (the new tag)
146 source.tagAs(testTag);
147 assertEquals(newSourceEvents[0], 1);
148 assertEquals(newSource[0], null);
149 assertEquals(newTagEvents[0], 2);
150 assertEquals(taggedSource[0], source);
151 assertEquals(newTag[0], testTag);
152
153 // Add the same tag; no events, and nothing changes.
154 source.tagAs(testTag);
155 assertEquals(newSourceEvents[0], 1);
156 assertEquals(newSource[0], null);
157 assertEquals(newTagEvents[0], 2);
158 assertEquals(taggedSource[0], source);
159 assertEquals(newTag[0], testTag);
160
161 }
162 }