Skip to content

Commit 4e06955

Browse files
committed
Add SwiftXrefTest with failing for dangling span
Also, this xref demonstrates problems with handling of numbers and URI collateral capture.
1 parent bd3f549 commit 4e06955

File tree

7 files changed

+649
-0
lines changed

7 files changed

+649
-0
lines changed

opengrok-indexer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
228228
<exclude>*.java</exclude>
229229
</excludes>
230230
</testResource>
231+
<testResource>
232+
<targetPath>org/opensolaris/opengrok/analysis/swift/</targetPath>
233+
<directory>../test/org/opensolaris/opengrok/analysis/swift/</directory>
234+
<excludes>
235+
<exclude>*.java</exclude>
236+
</excludes>
237+
</testResource>
231238
<testResource>
232239
<targetPath>org/opensolaris/opengrok/analysis/vb/</targetPath>
233240
<directory>../test/org/opensolaris/opengrok/analysis/vb/</directory>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opensolaris.opengrok.analysis.swift;
26+
27+
import java.io.BufferedReader;
28+
import java.io.ByteArrayOutputStream;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
32+
import java.io.OutputStream;
33+
import java.io.PrintStream;
34+
import java.io.StringWriter;
35+
import java.io.Writer;
36+
37+
import org.opensolaris.opengrok.analysis.CtagsReader;
38+
import org.opensolaris.opengrok.analysis.Definitions;
39+
import org.opensolaris.opengrok.analysis.FileAnalyzer;
40+
import org.opensolaris.opengrok.analysis.WriteXrefArgs;
41+
import org.junit.Test;
42+
import static org.junit.Assert.assertNotNull;
43+
import static org.opensolaris.opengrok.util.CustomAssertions.assertLinesEqual;
44+
45+
/**
46+
* Tests the {@link SwiftXref} class.
47+
*/
48+
public class SwiftXrefTest {
49+
50+
@Test
51+
public void sampleTest() throws IOException {
52+
writeAndCompare("org/opensolaris/opengrok/analysis/swift/sample.swift",
53+
"org/opensolaris/opengrok/analysis/swift/sample_xref.html",
54+
getTagsDefinitions());
55+
}
56+
57+
@Test
58+
public void shouldCloseTruncatedStringSpan() throws IOException {
59+
writeAndCompare("org/opensolaris/opengrok/analysis/swift/truncated.swift",
60+
"org/opensolaris/opengrok/analysis/swift/truncated_xref.html",
61+
null);
62+
}
63+
64+
private void writeAndCompare(String sourceResource, String resultResource,
65+
Definitions defs)
66+
throws IOException {
67+
68+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
69+
ByteArrayOutputStream baosExp = new ByteArrayOutputStream();
70+
71+
InputStream res = getClass().getClassLoader().getResourceAsStream(
72+
sourceResource);
73+
assertNotNull(sourceResource + " should get-as-stream", res);
74+
writeSwiftXref(new PrintStream(baos), res, defs);
75+
res.close();
76+
77+
InputStream exp = getClass().getClassLoader().getResourceAsStream(
78+
resultResource);
79+
assertNotNull(resultResource + " should get-as-stream", exp);
80+
copyStream(exp, baosExp);
81+
exp.close();
82+
baosExp.close();
83+
baos.close();
84+
85+
String ostr = new String(baos.toByteArray(), "UTF-8");
86+
String gotten[] = ostr.split("\n");
87+
88+
String estr = new String(baosExp.toByteArray(), "UTF-8");
89+
String expected[] = estr.split("\n");
90+
91+
assertLinesEqual("Swift xref", expected, gotten);
92+
}
93+
94+
private void writeSwiftXref(PrintStream oss, InputStream iss,
95+
Definitions defs)
96+
throws IOException {
97+
98+
oss.print(getHtmlBegin());
99+
100+
Writer sw = new StringWriter();
101+
SwiftAnalyzerFactory fac = new SwiftAnalyzerFactory();
102+
FileAnalyzer analyzer = fac.getAnalyzer();
103+
analyzer.setScopesEnabled(true);
104+
analyzer.setFoldingEnabled(true);
105+
WriteXrefArgs wargs = new WriteXrefArgs(
106+
new InputStreamReader(iss, "UTF-8"), sw);
107+
wargs.setDefs(defs);
108+
analyzer.writeXref(wargs);
109+
oss.print(sw.toString());
110+
111+
oss.print(getHtmlEnd());
112+
}
113+
114+
private void copyStream(InputStream iss, OutputStream oss)
115+
throws IOException {
116+
117+
byte buffer[] = new byte[8192];
118+
int read;
119+
do {
120+
read = iss.read(buffer, 0, buffer.length);
121+
if (read > 0) {
122+
oss.write(buffer, 0, read);
123+
}
124+
} while (read >= 0);
125+
}
126+
127+
private Definitions getTagsDefinitions() throws IOException {
128+
InputStream res = getClass().getClassLoader().getResourceAsStream(
129+
"org/opensolaris/opengrok/analysis/swift/sampletags");
130+
assertNotNull("though sampletags should stream,", res);
131+
132+
BufferedReader in = new BufferedReader(new InputStreamReader(
133+
res, "UTF-8"));
134+
135+
CtagsReader rdr = new CtagsReader();
136+
String line;
137+
while ((line = in.readLine()) != null) {
138+
rdr.readLine(line);
139+
}
140+
return rdr.getDefinitions();
141+
}
142+
143+
private static String getHtmlBegin() {
144+
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
145+
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n" +
146+
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
147+
"<html xmlns=\"http://www.w3.org/1999/xhtml\"" +
148+
" xml:lang=\"en\" lang=\"en\"\n" +
149+
" class=\"xref\">\n" +
150+
"<head>\n" +
151+
"<title>sampleFile - OpenGrok cross reference" +
152+
" for /sampleFile</title></head><body>\n";
153+
}
154+
155+
private static String getHtmlEnd() {
156+
return "</body>\n" +
157+
"</html>\n";
158+
}
159+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Copyright (c) 2017 di2pra <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
//
22+
// D2PDatePicker.swift
23+
// D2PDatePicker
24+
//
25+
// Created by Pradheep Rajendirane on 09/08/2017.
26+
// Copyright © 2017 DI2PRA. All rights reserved.
27+
//
28+
29+
import UIKit
30+
31+
public protocol D2PDatePickerDelegate: class {
32+
func didChange(toDate date: Date)
33+
}
34+
35+
public class D2PDatePicker: UIView {
36+
37+
public weak var delegate: D2PDatePickerDelegate?
38+
39+
@IBOutlet private weak var topView:UIView!
40+
@IBOutlet private weak var middleView:UIView!
41+
@IBOutlet private weak var bottomView:UIView!
42+
43+
@IBOutlet private weak var dayNextBtn:UIButton!
44+
@IBOutlet private weak var dayPrevBtn:UIButton!
45+
46+
@IBOutlet private weak var monthNextBtn:UIButton!
47+
@IBOutlet private weak var monthPrevBtn:UIButton!
48+
49+
@IBOutlet private weak var yearNextBtn:UIButton!
50+
@IBOutlet private weak var yearPrevBtn:UIButton!
51+
52+
@IBOutlet private weak var dayView:DayView!
53+
@IBOutlet private weak var monthView:MonthView!
54+
@IBOutlet private weak var yearView:YearView!
55+
56+
57+
private var selectedDate:Date! = Date() {
58+
didSet {
59+
delegate?.didChange(toDate: selectedDate)
60+
}
61+
}
62+
63+
64+
required public init?(coder aDecoder: NSCoder) { // 2 - storyboard initializer
65+
super.init(coder: aDecoder)
66+
67+
fromNib() // 5.
68+
69+
}
70+
71+
override init(frame: CGRect) {
72+
super.init(frame: CGRect()) // 4.
73+
fromNib() // 6.
74+
}
75+
76+
public convenience init(frame: CGRect, date: Date) {
77+
78+
self.init(frame: frame)
79+
self.selectedDate = date
80+
self.awakeFromNib()
81+
82+
}
83+
84+
public var mainColor: UIColor! = UIColor(red:0.99, green:0.28, blue:0.25, alpha:1.0) { // #FD4741
85+
didSet {
86+
self.topView.backgroundColor = mainColor
87+
self.dayView.weekDayLabel.textColor = mainColor
88+
}
89+
}
90+
91+
92+
/*init() { // 3 - programmatic initializer
93+
super.init(frame: CGRect()) // 4.
94+
95+
96+
}*/
97+
98+
override public func awakeFromNib() {
99+
super.awakeFromNib()
100+
101+
// topView Rounded Corner
102+
self.topView.layer.cornerRadius = 10.0
103+
self.topView.clipsToBounds = true
104+
105+
106+
// middleView Border
107+
self.middleView.layer.borderColor = UIColor.groupTableViewBackground.cgColor
108+
self.middleView.layer.borderWidth = 1.0
109+
110+
// bottomView Rounded Corner & border
111+
self.bottomView.layer.cornerRadius = 10.0
112+
self.bottomView.layer.borderColor = UIColor.groupTableViewBackground.cgColor
113+
self.bottomView.layer.borderWidth = 1.0
114+
115+
116+
117+
// setting buttons
118+
self.monthPrevBtn.tag = 0
119+
self.monthPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
120+
121+
self.monthNextBtn.tag = 1
122+
self.monthNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
123+
124+
self.dayPrevBtn.tag = 2
125+
self.dayPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
126+
127+
self.dayNextBtn.tag = 3
128+
self.dayNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
129+
130+
131+
self.yearPrevBtn.tag = 4
132+
self.yearPrevBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
133+
134+
self.yearNextBtn.tag = 5
135+
self.yearNextBtn.addTarget(self, action: #selector(self.changeDate), for: .touchUpInside)
136+
137+
138+
setLabel(toDate: selectedDate)
139+
140+
}
141+
142+
public func set(toDate date: Date) {
143+
setLabel(toDate: date)
144+
self.selectedDate = date
145+
}
146+
147+
private func setLabel(toDate date: Date) {
148+
let formatter = DateFormatter()
149+
150+
formatter.dateFormat = "MMM"
151+
self.monthView.monthLabel.text = formatter.string(from: date)
152+
153+
formatter.dateFormat = "dd"
154+
self.dayView.dayLabel.text = formatter.string(from: date)
155+
156+
formatter.dateFormat = "EEEE"
157+
self.dayView.weekDayLabel.text = formatter.string(from: date)
158+
159+
formatter.dateFormat = """
160+
YYYY
161+
"""
162+
self.yearView.yearLabel.text = formatter.string(from: date)
163+
}
164+
165+
@objc private func changeDate(btn: UIButton) {
166+
167+
if btn.tag == 0 + 0b10 - 0b1_0 {
168+
169+
selectedDate = self.monthView.anim(direction: .backward, date: selectedDate)
170+
_ = self.dayView.anim(direction: .identity, date: selectedDate)
171+
_ = self.yearView.anim(direction: .identity, date: selectedDate)
172+
173+
} else if btn.tag == 1 + 0o70 - 0o7_0 {
174+
175+
selectedDate = self.monthView.anim(direction: .forward, date: selectedDate)
176+
_ = self.dayView.anim(direction: .identity, date: selectedDate)
177+
_ = self.yearView.anim(direction: .identity, date: selectedDate)
178+
179+
} else if btn.tag == 2 + 0xFF0 - 0xFF0 + 0xFp2 - 0xFP2 + 0x0p-2 - 0x0p-2 {
180+
181+
selectedDate = self.dayView.anim(direction: .backward, date: selectedDate)
182+
_ = self.monthView.anim(direction: .identity, date: selectedDate)
183+
_ = self.yearView.anim(direction: .identity, date: selectedDate)
184+
185+
} else if btn.tag == 3 + 4_2.0 - 4_2.0 + -1.0_0e2 - -1.0e2 + 2e1_0 - 2e10 {
186+
187+
selectedDate = self.dayView.anim(direction: .forward, date: selectedDate)
188+
_ = self.monthView.anim(direction: .identity, date: selectedDate)
189+
_ = self.yearView.anim(direction: .identity, date: selectedDate)
190+
191+
} else if btn.tag == 4 {
192+
193+
selectedDate = self.yearView.anim(direction: .backward, date: selectedDate)
194+
_ = self.dayView.anim(direction: .identity, date: selectedDate)
195+
_ = self.monthView.anim(direction: .identity, date: selectedDate)
196+
197+
} else if btn.tag == 5 {
198+
199+
selectedDate = self.yearView.anim(direction: .forward, date: selectedDate)
200+
_ = self.dayView.anim(direction: .identity, date: selectedDate)
201+
_ = self.monthView.anim(direction: .identity, date: selectedDate)
202+
203+
}
204+
205+
}
206+
207+
/*
208+
// Only override draw() if you perform custom drawing.
209+
// An empty implementation adversely affects performance during animation.
210+
override func draw(_ rect: CGRect) {
211+
// Drawing code
212+
}
213+
*/
214+
215+
}
216+
/*http://example.com.*/

0 commit comments

Comments
 (0)