|
| 1 | +package codenav |
| 2 | + |
| 3 | +// This file is a Lithe addition: cases found by comparing against JDT LS on |
| 4 | +// Nacos where this index answered differently or not at all. |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +var overloadProject = map[string]string{ |
| 12 | + "src/o/Error.java": `package o; |
| 13 | +
|
| 14 | +public class Error { |
| 15 | + public Error() {} |
| 16 | + public Error(int code) {} |
| 17 | + public Error(String msg) {} |
| 18 | + public Error(Error cause) {} |
| 19 | +} |
| 20 | +`, |
| 21 | + "src/o/Cause.java": `package o; |
| 22 | +
|
| 23 | +public class Cause extends Error {} |
| 24 | +`, |
| 25 | + "src/o/Cache.java": `package o; |
| 26 | +
|
| 27 | +import java.util.Set; |
| 28 | +
|
| 29 | +public class Cache { |
| 30 | + public void init(String packageName) {} |
| 31 | + public void init(Set<Class<?>> classes) {} |
| 32 | + public void init(long ttl, boolean lazy) {} |
| 33 | + public void init(Object key, boolean lazy) {} |
| 34 | + public void put(char c) {} |
| 35 | + public void put(double d) {} |
| 36 | + public void put(Cache other) {} |
| 37 | + public static <T> Cache builder() { return null; } |
| 38 | + public static void check(boolean ok, Object message) {} |
| 39 | + public static void check(boolean ok, String template, Object... args) {} |
| 40 | + public Cache self() { return this; } |
| 41 | +} |
| 42 | +`, |
| 43 | + "src/o/Use.java": `package o; |
| 44 | +
|
| 45 | +import java.util.HashSet; |
| 46 | +import java.util.Set; |
| 47 | +
|
| 48 | +class Use { |
| 49 | + void run(Set<Class<?>> classes, Cache cache) { |
| 50 | + Error a = new Error("user not found!"); |
| 51 | + Error b = new Error(404); |
| 52 | + Error c = new Error(new Cause()); |
| 53 | + Error d = new Error("a" + 1); |
| 54 | + cache.init(classes); |
| 55 | + cache.init("com.acme"); |
| 56 | + cache.init(new HashSet<Class<?>>()); |
| 57 | + cache.init(5L, true); |
| 58 | + cache.init(cache, false); |
| 59 | + cache.put('x'); |
| 60 | + cache.put(1.5); |
| 61 | + cache.put(-2); |
| 62 | + cache.put(cache); |
| 63 | + cache.put((Cache) null); |
| 64 | + Cache.<String>builder().self(); |
| 65 | + Cache.check(classes.isEmpty(), "no classes"); |
| 66 | + Cache.check(true, "%s missing", "x"); |
| 67 | + } |
| 68 | +} |
| 69 | +`, |
| 70 | +} |
| 71 | + |
| 72 | +func TestOverloadsWithTheSameArity(t *testing.T) { |
| 73 | + ix := NewIndex() |
| 74 | + for p, src := range overloadProject { |
| 75 | + ix.Put(ParseFile(p, []byte(src))) |
| 76 | + } |
| 77 | + const use = "src/o/Use.java" |
| 78 | + at := func(path, needle string) Location { |
| 79 | + src := overloadProject[path] |
| 80 | + off := strings.Index(src, needle) |
| 81 | + if off < 0 { |
| 82 | + t.Fatalf("%q not found in %s", needle, path) |
| 83 | + } |
| 84 | + return Location{Path: path, Line: strings.Count(src[:off], "\n"), Column: off - (strings.LastIndexByte(src[:off], '\n') + 1)} |
| 85 | + } |
| 86 | + const errT, cache = "src/o/Error.java", "src/o/Cache.java" |
| 87 | + cases := []struct { |
| 88 | + name, click string |
| 89 | + skip int |
| 90 | + want Location |
| 91 | + }{ |
| 92 | + {"string literal picks the String constructor", `Error("user`, 0, at(errT, "Error(String msg)")}, |
| 93 | + {"int literal picks the int constructor", `Error(404)`, 0, at(errT, "Error(int code)")}, |
| 94 | + {"subtype argument picks the supertype parameter", `Error(new Cause())`, 0, at(errT, "Error(Error cause)")}, |
| 95 | + {"string concatenation is a String", `Error("a" + 1)`, 0, at(errT, "Error(String msg)")}, |
| 96 | + {"typed parameter picks the matching overload", `init(classes)`, 0, at(cache, "init(Set")}, |
| 97 | + {"string literal picks String over Set", `init("com`, 0, at(cache, "init(String")}, |
| 98 | + {"new expression type", `init(new HashSet`, 0, at(cache, "init(Set")}, |
| 99 | + {"long literal", `init(5L`, 0, at(cache, "init(long")}, |
| 100 | + {"reference argument goes to Object", `init(cache, false)`, 0, at(cache, "init(Object")}, |
| 101 | + {"char literal", `put('x')`, 0, at(cache, "put(char")}, |
| 102 | + {"double literal", `put(1.5)`, 0, at(cache, "put(double")}, |
| 103 | + {"negative int widens to double, not char", `put(-2)`, 0, at(cache, "put(double")}, |
| 104 | + {"project type", `put(cache)`, 0, at(cache, "put(Cache")}, |
| 105 | + {"cast", `put((Cache)`, 0, at(cache, "put(Cache")}, |
| 106 | + {"explicit type arguments", `builder()`, 0, at(cache, "builder()")}, |
| 107 | + {"a fixed-arity overload beats varargs", `check(classes`, 0, at(cache, "check(boolean ok, Object")}, |
| 108 | + {"varargs when the fixed one cannot take the arguments", `check(true`, 0, at(cache, "check(boolean ok, String")}, |
| 109 | + {"call after explicit type arguments", `self();`, 0, at(cache, "self()")}, |
| 110 | + } |
| 111 | + src := []byte(overloadProject[use]) |
| 112 | + for _, c := range cases { |
| 113 | + off := strings.Index(string(src), c.click) |
| 114 | + if off < 0 { |
| 115 | + t.Fatalf("%s: %q not found", c.name, c.click) |
| 116 | + } |
| 117 | + got, ok := ix.Resolve(use, src, off+c.skip) |
| 118 | + if !ok { |
| 119 | + t.Errorf("%s: no declaration, want %v", c.name, c.want) |
| 120 | + continue |
| 121 | + } |
| 122 | + if got.Path != c.want.Path || got.Line != c.want.Line || got.Column != c.want.Column { |
| 123 | + t.Errorf("%s: got %v, want %v", c.name, got, c.want) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +var jdtlsCaseProject = map[string]string{ |
| 129 | + "src/q/Manager.java": `package q; |
| 130 | +
|
| 131 | +public class Manager { |
| 132 | + public @interface Extractor { |
| 133 | + Class<?> rpc(); |
| 134 | + } |
| 135 | +} |
| 136 | +`, |
| 137 | + "src/q/Handler.java": `package q; |
| 138 | +
|
| 139 | +class Handler { |
| 140 | + @Manager.Extractor(rpc = Handler.class) |
| 141 | + void handle() {} |
| 142 | +
|
| 143 | + static void check(Class<?> raw, final Type... typeArguments) { |
| 144 | + if (typeArguments.length != 0) { |
| 145 | + return; |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + byte[] key(String... keys) { return keys[0].getBytes(); } |
| 150 | +} |
| 151 | +`, |
| 152 | +} |
| 153 | + |
| 154 | +func TestJDTLSCases(t *testing.T) { |
| 155 | + ix := NewIndex() |
| 156 | + for p, src := range jdtlsCaseProject { |
| 157 | + ix.Put(ParseFile(p, []byte(src))) |
| 158 | + } |
| 159 | + const h = "src/q/Handler.java" |
| 160 | + at := func(path, needle string, nth int) Location { |
| 161 | + src := jdtlsCaseProject[path] |
| 162 | + off := offsetOf(t, src, needle, nth, 0) |
| 163 | + return Location{Path: path, Line: strings.Count(src[:off], "\n"), Column: off - (strings.LastIndexByte(src[:off], '\n') + 1)} |
| 164 | + } |
| 165 | + cases := []struct { |
| 166 | + name, click string |
| 167 | + nth int |
| 168 | + want Location |
| 169 | + }{ |
| 170 | + {"qualified annotation", "Extractor(", 0, at("src/q/Manager.java", "Extractor {", 0)}, |
| 171 | + {"attribute of a qualified annotation", "rpc =", 0, at("src/q/Manager.java", "rpc()", 0)}, |
| 172 | + {"varargs parameter declaration", "typeArguments)", 0, at(h, "typeArguments)", 0)}, |
| 173 | + {"varargs parameter use", "typeArguments.length", 0, at(h, "typeArguments)", 0)}, |
| 174 | + {"varargs parameter after a plain type", "keys)", 0, at(h, "keys)", 0)}, |
| 175 | + {"varargs parameter indexed", "keys[0]", 0, at(h, "keys)", 0)}, |
| 176 | + } |
| 177 | + src := []byte(jdtlsCaseProject[h]) |
| 178 | + for _, c := range cases { |
| 179 | + off := offsetOf(t, string(src), c.click, c.nth, 0) |
| 180 | + got, ok := ix.Resolve(h, src, off) |
| 181 | + if !ok { |
| 182 | + t.Errorf("%s: no declaration, want %v", c.name, c.want) |
| 183 | + continue |
| 184 | + } |
| 185 | + if got.Path != c.want.Path || got.Line != c.want.Line || got.Column != c.want.Column { |
| 186 | + t.Errorf("%s: got %v, want %v", c.name, got, c.want) |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments