Skip to content

Commit 2925f1f

Browse files
author
Tom Lienard
authored
test(regex): add cidr tests (#988)
1 parent d4072c6 commit 2925f1f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

packages/regex/src/__tests__/index.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import {
2424
fourDigitsCode,
2525
hexadecimal,
2626
ip,
27+
ipCidr,
2728
ipv4,
29+
ipv4Cidr,
2830
ipv6,
31+
ipv6Cidr,
2932
macAddress,
3033
phone,
3134
sixDigitsCode,
@@ -714,4 +717,54 @@ describe('@regex', () => {
714717
expect(ip.test(string)).toBe(expected)
715718
})
716719
})
720+
721+
describe('ipCidr', () => {
722+
test.each([
723+
['1:2:3:4:5:6:7::/48', true],
724+
['1:2:3:4:5:6::8/44', true],
725+
['::2:3:4:5:6:7:8/64', true],
726+
['::1.2.3.4/32', true],
727+
['192.168.1.1/32', true],
728+
['127.0.0.1/24', true],
729+
['127.0.0.1/64', false],
730+
['0.0.0.0/0', true],
731+
['255.255.255.255/32', true],
732+
['256.256.256.256/0', false],
733+
['1:2:3::5:6:7:900.2.3.4/0', false],
734+
])('should match regex %s to be %s', (string, expected) => {
735+
expect(ipCidr.test(string)).toBe(expected)
736+
})
737+
})
738+
739+
describe('ipv4Cidr', () => {
740+
test.each([
741+
['192.168.1.1/24', true],
742+
['127.0.0.1/32', true],
743+
['0.0.0.0/0', true],
744+
['255.255.255.255/32', true],
745+
['1.2.3.4/0 hi', false],
746+
['256.256.256.256/32', false],
747+
['999.999.999.999/999', false],
748+
['1.2.3/0', false],
749+
])('should match regex %s to be %s', (string, expected) => {
750+
expect(ipv4Cidr.test(string)).toBe(expected)
751+
})
752+
})
753+
754+
describe('ipv6Cidr', () => {
755+
test.each([
756+
['1:2:3:4:5:6:7::/48', true],
757+
['1:2:3:4:5:6::8/48', true],
758+
['1:2::4:5:6:7:8/36', true],
759+
['1::3:4:5:6:7:8/32', true],
760+
['::2:3:4:5:6:7:8/24', true],
761+
['::1.2.3.4/64', true],
762+
['1:2::4:5:6:7:8/48 hi', false],
763+
['192.168.1.1/0', false],
764+
['127.0.0.1/32', false],
765+
['256.256.256.256/32', false],
766+
])('should match regex %s to be %s', (string, expected) => {
767+
expect(ipv6Cidr.test(string)).toBe(expected)
768+
})
769+
})
717770
})

0 commit comments

Comments
 (0)