forked from jhipster/prettier-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern-matching-spec.js
More file actions
92 lines (81 loc) · 2.93 KB
/
pattern-matching-spec.js
File metadata and controls
92 lines (81 loc) · 2.93 KB
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
import { expect } from "chai";
import * as javaParser from "../../src/index.js";
describe("Pattern matching", () => {
it("should handle Java instanceof with reference types", () => {
const input = `a instanceof Point`;
expect(() => javaParser.parse(input, "binaryExpression")).to.not.throw();
});
it("should handle Java instanceof with pattern matching", () => {
const input = `a instanceof Point p`;
expect(() => javaParser.parse(input, "binaryExpression")).to.not.throw();
});
it("should handle Java instanceof with pattern matching inside method", () => {
const input = `
static String formatter(Object o) {
String formatted = "unknown";
if (o instanceof Integer i) {
formatted = String.format("int %d", i);
} else if (o instanceof Long l) {
formatted = String.format("long %d", l);
} else if (o instanceof Double d) {
formatted = String.format("double %f", d);
} else if (o instanceof String s) {
formatted = String.format("String %s", s);
}
return formatted;
}
`;
expect(() => javaParser.parse(input, "methodDeclaration")).to.not.throw();
});
it("should support pattern matching guards", () => {
const input = `
package com.vimat.model;
public record Buyer(String name, double bestPrice, double joker) {
public boolean hasBestOffer(Buyer other) {
return switch (other) {
case null -> true;
case Buyer b when this.bestPrice > b.bestPrice -> true;
default -> false;
};
}
}
`;
expect(() => javaParser.parse(input, "compilationUnit")).to.not.throw();
});
it("should parse pattern list", () => {
const input = `A a, B b`;
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
it("should parse pattern list with dims", () => {
const input = `A a, B[] b`;
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
it("should parse pattern list with nested records", () => {
const input =
"int a, Location (String name, GPSPoint(int latitude, int longitude))";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
it("should parse pattern list with var", () => {
const input =
"int a, Location (var name, GPSPoint(var latitude, var longitude))";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
it("should parse pattern list with nested records and annotation", () => {
const input = "int a, @not Location (String name)";
expect(() =>
javaParser.parse(input, "componentPatternList")
).to.not.throw();
});
it("should parse switch label with multiple qualified case patterns", () => {
const input = "case b.B _, c.C _";
expect(() => javaParser.parse(input, "switchLabel")).to.not.throw();
});
});