Skip to content

Commit cfaef1e

Browse files
authored
feat(swift-testing): migrate KeyMatch and Utils tests to Swift Testing (phase 2) (#36)
1 parent e6be51f commit cfaef1e

File tree

2 files changed

+99
-82
lines changed

2 files changed

+99
-82
lines changed

Tests/CasbinTests/UtilsTests/KeyMatchTests.swift

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,86 @@
1-
2-
import XCTest
1+
import Testing
32
import Casbin
43

5-
final class KeyMatchTests: XCTestCase {
4+
@Suite("Key/Glob/IP match utilities")
5+
struct KeyMatchTests {
6+
@Test("keyMatch")
67
func testKeyMatch() {
7-
XCTAssertTrue(Util.keyMatch("/foo/bar", "/foo/*"))
8-
XCTAssertTrue(!Util.keyMatch("/bar/foo", "/foo/*"))
9-
XCTAssertTrue(Util.keyMatch("/bar", "/ba*"))
8+
#expect(Util.keyMatch("/foo/bar", "/foo/*"))
9+
#expect(!Util.keyMatch("/bar/foo", "/foo/*"))
10+
#expect(Util.keyMatch("/bar", "/ba*"))
1011
}
12+
13+
@Test("regexMatch")
1114
func testRegexMatch() {
12-
XCTAssertTrue(Util.regexMatch("foobar", "^foo*"))
13-
XCTAssertFalse(Util.regexMatch("barfoo", "^foo*"))
15+
#expect(Util.regexMatch("foobar", "^foo*"))
16+
#expect(!Util.regexMatch("barfoo", "^foo*"))
1417
}
18+
19+
@Test("keyMatch2 path params")
1520
func testKeyMatch2() {
16-
XCTAssertTrue(Util.keyMatch2("/foo/bar", "/foo/*"))
17-
XCTAssertTrue(Util.keyMatch2("/foo/baz", "/foo/:bar"))
18-
XCTAssertTrue(Util.keyMatch2("/foo/baz", "/:foo/:bar"))
19-
XCTAssertTrue(Util.keyMatch2("/foo/baz/foo", "/foo/:bar/foo"))
20-
XCTAssertTrue(!Util.keyMatch2("/baz", "/foo"))
21+
#expect(Util.keyMatch2("/foo/bar", "/foo/*"))
22+
#expect(Util.keyMatch2("/foo/baz", "/foo/:bar"))
23+
#expect(Util.keyMatch2("/foo/baz", "/:foo/:bar"))
24+
#expect(Util.keyMatch2("/foo/baz/foo", "/foo/:bar/foo"))
25+
#expect(!Util.keyMatch2("/baz", "/foo"))
2126
}
27+
28+
@Test("keyMatch3 braces params")
2229
func testKeyMatch3() {
23-
XCTAssertTrue(Util.keyMatch3("/foo/bar", "/foo/*"))
24-
XCTAssertTrue(Util.keyMatch3("/foo/baz", "/foo/{bar}"))
25-
XCTAssertTrue(Util.keyMatch3("/foo/baz/foo", "/foo/{bar}/foo"))
26-
XCTAssertTrue(!Util.keyMatch3("/baz", "/foo"))
30+
#expect(Util.keyMatch3("/foo/bar", "/foo/*"))
31+
#expect(Util.keyMatch3("/foo/baz", "/foo/{bar}"))
32+
#expect(Util.keyMatch3("/foo/baz/foo", "/foo/{bar}/foo"))
33+
#expect(!Util.keyMatch3("/baz", "/foo"))
2734
}
35+
36+
@Test("ipMatch IPv4/IPv6 + CIDR")
2837
func testIpMatch() {
29-
XCTAssertTrue(Util.ipMatch("::1", "::0:1"))
30-
XCTAssertTrue(Util.ipMatch("192.168.1.1", "192.168.1.1"))
31-
XCTAssertTrue(Util.ipMatch("192.168.2.123", "192.168.2.0/24"))
32-
XCTAssertTrue(Util.ipMatch("192.168.2.123", "192.168.2.123/16"))
33-
XCTAssertFalse(Util.ipMatch("::1", "127.0.0.2"))
34-
XCTAssertFalse(Util.ipMatch("192.168.2.189", "192.168.1.134/26"))
38+
#expect(Util.ipMatch("::1", "::0:1"))
39+
#expect(Util.ipMatch("192.168.1.1", "192.168.1.1"))
40+
#expect(Util.ipMatch("192.168.2.123", "192.168.2.0/24"))
41+
#expect(Util.ipMatch("192.168.2.123", "192.168.2.123/16"))
42+
#expect(!Util.ipMatch("::1", "127.0.0.2"))
43+
#expect(!Util.ipMatch("192.168.2.189", "192.168.1.134/26"))
3544
}
45+
46+
@Test("globMatch patterns")
3647
func testglobMatch() {
37-
XCTAssertTrue(Util.globMatch("/foo", "/foo"))
38-
XCTAssertTrue(Util.globMatch("/foo", "/foo*"))
39-
XCTAssertFalse(Util.globMatch("/foo", "/foo/*"))
40-
XCTAssertFalse(Util.globMatch("/foo/bar", "/foo"))
41-
XCTAssertFalse(Util.globMatch("/foo/bar", "/foo*"))
42-
XCTAssertTrue(Util.globMatch("/foo/bar", "/foo/*"))
43-
XCTAssertFalse(Util.globMatch("/foobar", "/foo"))
44-
XCTAssertTrue(Util.globMatch("/foobar", "/foo*"))
45-
XCTAssertFalse(Util.globMatch("/foobar", "/foo/*"))
46-
XCTAssertTrue(Util.globMatch("/foo", "*/foo"))
47-
XCTAssertTrue(Util.globMatch("/foo", "*/foo*"))
48-
XCTAssertFalse(Util.globMatch("/foo", "*/foo/*"))
49-
XCTAssertFalse(Util.globMatch("/foo/bar", "*/foo"))
50-
XCTAssertFalse(Util.globMatch("/foo/bar", "*/foo*"))
51-
XCTAssertTrue(Util.globMatch("/foo/bar", "*/foo/*"))
52-
XCTAssertFalse(Util.globMatch("/foobar", "*/foo"))
53-
XCTAssertTrue(Util.globMatch("/foobar", "*/foo*"))
54-
XCTAssertFalse(Util.globMatch("/foobar", "*/foo/*"))
55-
XCTAssertFalse(Util.globMatch("/prefix/foo", "*/foo"))
56-
XCTAssertFalse(Util.globMatch("/prefix/foo", "*/foo*"))
57-
XCTAssertFalse(Util.globMatch("/prefix/foo", "*/foo/*"))
58-
XCTAssertFalse(Util.globMatch("/prefix/foo/bar", "*/foo"))
59-
XCTAssertFalse(Util.globMatch("/prefix/foo/bar", "*/foo*"))
60-
XCTAssertFalse(Util.globMatch("/prefix/foo/bar", "*/foo/*"))
61-
XCTAssertFalse(Util.globMatch("/prefix/foobar", "*/foo"))
62-
XCTAssertFalse(Util.globMatch("/prefix/foobar", "*/foo*"))
63-
XCTAssertFalse(Util.globMatch("/prefix/foobar", "*/foo/*"))
64-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo", "*/foo"))
65-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo", "*/foo*"))
66-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo", "*/foo/*"))
67-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo/bar", "*/foo"))
68-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo/bar", "*/foo*"))
69-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foo/bar", "*/foo/*"))
70-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foobar", "*/foo"))
71-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foobar", "*/foo*"))
72-
XCTAssertFalse(Util.globMatch("/prefix/subprefix/foobar", "*/foo/*"))
48+
#expect(Util.globMatch("/foo", "/foo"))
49+
#expect(Util.globMatch("/foo", "/foo*"))
50+
#expect(!Util.globMatch("/foo", "/foo/*"))
51+
#expect(!Util.globMatch("/foo/bar", "/foo"))
52+
#expect(!Util.globMatch("/foo/bar", "/foo*"))
53+
#expect(Util.globMatch("/foo/bar", "/foo/*"))
54+
#expect(!Util.globMatch("/foobar", "/foo"))
55+
#expect(Util.globMatch("/foobar", "/foo*"))
56+
#expect(!Util.globMatch("/foobar", "/foo/*"))
57+
#expect(Util.globMatch("/foo", "*/foo"))
58+
#expect(Util.globMatch("/foo", "*/foo*"))
59+
#expect(!Util.globMatch("/foo", "*/foo/*"))
60+
#expect(!Util.globMatch("/foo/bar", "*/foo"))
61+
#expect(!Util.globMatch("/foo/bar", "*/foo*"))
62+
#expect(Util.globMatch("/foo/bar", "*/foo/*"))
63+
#expect(!Util.globMatch("/foobar", "*/foo"))
64+
#expect(Util.globMatch("/foobar", "*/foo*"))
65+
#expect(!Util.globMatch("/foobar", "*/foo/*"))
66+
#expect(!Util.globMatch("/prefix/foo", "*/foo"))
67+
#expect(!Util.globMatch("/prefix/foo", "*/foo*"))
68+
#expect(!Util.globMatch("/prefix/foo", "*/foo/*"))
69+
#expect(!Util.globMatch("/prefix/foo/bar", "*/foo"))
70+
#expect(!Util.globMatch("/prefix/foo/bar", "*/foo*"))
71+
#expect(!Util.globMatch("/prefix/foo/bar", "*/foo/*"))
72+
#expect(!Util.globMatch("/prefix/foobar", "*/foo"))
73+
#expect(!Util.globMatch("/prefix/foobar", "*/foo*"))
74+
#expect(!Util.globMatch("/prefix/foobar", "*/foo/*"))
75+
#expect(!Util.globMatch("/prefix/subprefix/foo", "*/foo"))
76+
#expect(!Util.globMatch("/prefix/subprefix/foo", "*/foo*"))
77+
#expect(!Util.globMatch("/prefix/subprefix/foo", "*/foo/*"))
78+
#expect(!Util.globMatch("/prefix/subprefix/foo/bar", "*/foo"))
79+
#expect(!Util.globMatch("/prefix/subprefix/foo/bar", "*/foo*"))
80+
#expect(!Util.globMatch("/prefix/subprefix/foo/bar", "*/foo/*"))
81+
#expect(!Util.globMatch("/prefix/subprefix/foobar", "*/foo"))
82+
#expect(!Util.globMatch("/prefix/subprefix/foobar", "*/foo*"))
83+
#expect(!Util.globMatch("/prefix/subprefix/foobar", "*/foo/*"))
7384
}
7485
}
7586

76-
Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,48 @@
1-
2-
import XCTest
1+
import Testing
32
import Casbin
43

5-
final class UtilsTests: XCTestCase {
4+
@Suite("Utilities: CSV + assertion escaping")
5+
struct UtilsTests {
6+
@Test("escapeAssertion replaces dots with underscores")
67
func testEscapeAssertion() {
78
let s = "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act"
89
let exp = "g(r_sub, p_sub) && r_obj == p_obj && r_act == p_act"
9-
XCTAssertEqual(exp, Util.escapeAssertion(s))
10-
10+
#expect(Util.escapeAssertion(s) == exp)
11+
1112
let s1 = "g(r2.sub, p2.sub) && r2.obj == p2.obj && r2.act == p2.act"
1213
let exp1 = "g(r2_sub, p2_sub) && r2_obj == p2_obj && r2_act == p2_act"
13-
XCTAssertEqual(exp1, Util.escapeAssertion(s1))
14+
#expect(Util.escapeAssertion(s1) == exp1)
1415
}
16+
17+
@Test("CSV parsing: simple")
1518
func testCsvParse1() {
16-
XCTAssertEqual(Util.parseCsvLine(line: "alice, domain1, data1, action1"), ["alice","domain1","data1","action1"])
19+
#expect(Util.parseCsvLine(line: "alice, domain1, data1, action1") == ["alice","domain1","data1","action1"])
1720
}
21+
22+
@Test("CSV parsing: quoted field with comma")
1823
func testCsvParse2() {
19-
XCTAssertEqual(Util.parseCsvLine(line: #"alice, "domain1, domain2", data1 , action1"#), ["alice","domain1, domain2","data1","action1"])
24+
#expect(Util.parseCsvLine(line: #"alice, "domain1, domain2", data1 , action1"#) == ["alice","domain1, domain2","data1","action1"])
2025
}
26+
27+
@Test("CSV parsing: only comma yields nil")
2128
func testCsvParse3() {
22-
XCTAssertEqual(Util.parseCsvLine(line: ","), nil)
29+
#expect(Util.parseCsvLine(line: ",") == nil)
2330
}
31+
32+
@Test("CSV parsing: edge cases")
2433
func testCsvParse4() {
25-
XCTAssertEqual(Util.parseCsvLine(line: ""), nil)
26-
XCTAssertEqual(Util.parseCsvLine(line: "#"), nil)
27-
XCTAssertEqual(Util.parseCsvLine(line: " #"), nil)
28-
XCTAssertEqual(Util.parseCsvLine(line: "\" "), ["\""])
29-
XCTAssertEqual(Util.parseCsvLine(line: "\" alice"), ["\" alice"])
30-
XCTAssertEqual(Util.parseCsvLine(line: "alice, \"domain1, domain2"), ["alice","\"domain1, domain2"])
31-
XCTAssertEqual(Util.parseCsvLine(line: "\"\""), [""])
32-
XCTAssertEqual(Util.parseCsvLine(line: "r.sub.Status == \"ACTIVE\", /data1, read"), ["r.sub.Status == \"ACTIVE\"","/data1","read"])
33-
34+
#expect(Util.parseCsvLine(line: "") == nil)
35+
#expect(Util.parseCsvLine(line: "#") == nil)
36+
#expect(Util.parseCsvLine(line: " #") == nil)
37+
#expect(Util.parseCsvLine(line: "\" ") == ["\""])
38+
#expect(Util.parseCsvLine(line: "\" alice") == ["\" alice"])
39+
#expect(Util.parseCsvLine(line: "alice, \"domain1, domain2") == ["alice","\"domain1, domain2"])
40+
#expect(Util.parseCsvLine(line: "\"\"") == [""])
41+
#expect(Util.parseCsvLine(line: "r.sub.Status == \"ACTIVE\", /data1, read") == ["r.sub.Status == \"ACTIVE\"","/data1","read"])
3442
}
43+
44+
@Test("CSV parsing: multiple quoted fields")
3545
func testCsvParse5() {
36-
XCTAssertEqual(Util.parseCsvLine(line: "alice, \"domain1, domain2\", \"data1, data2\", action1"), ["alice","domain1, domain2","data1, data2","action1"])
46+
#expect(Util.parseCsvLine(line: "alice, \"domain1, domain2\", \"data1, data2\", action1") == ["alice","domain1, domain2","data1, data2","action1"])
3747
}
38-
39-
40-
4148
}

0 commit comments

Comments
 (0)