# PRE: if if-failed-xlat
#
string result_string
string dummy_string

if (('${feature.regex-pcre}' == 'yes') || ('${feature.regex-pcre2}' == 'yes')) {
	request += {
		NAS-Port = '123456789'
		Port-Limit = 1
	}

# Check failure when no previous capture - full capture
if (%regex()) {
	test_fail
}

if (!(Module-Failure-Message[*] == "No previous regex capture")) {
	test_fail
}

request -= Module-Failure-Message[*]

# Check failure when no previous capture - named group
if (%regex(foo)) {
	test_fail
}

if (!(Module-Failure-Message[*] == "No previous named regex capture group")) {
	test_fail
}

request -= Module-Failure-Message[*]

# Check failure when no previous capture - numbered group
if (%regex(%{Port-Limit})) {
	test_fail
}

if (!(Module-Failure-Message[*] == "No previous numbered regex capture group")) {
	test_fail
}

request -= Module-Failure-Message[*]

# uncompiled - ref - named capture groups
if (User-Name =~ /^(?<one>[0-9])_(?<two>[0-9])?_(?<three>[0-9]*)_(?<four>[0-9]+)_(?<five>[^_])_(?<six>6)_(?<seven>[7-8])%{dummy_string}/) {
	result_string := "%regex(seven)_%regex(six)_%regex(five)_%regex(four)_%regex(three)_%regex(two)_%regex(one)_%{0}"
	if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) {
		test_fail
	}
}
else {
	test_fail
}

# Checking capture groups are cleared out correctly
if (User-Name =~ /^(?<one>[0-9])_%{dummy_string}/) {
	result_string := "%{0}%regex(one)%regex(two)%regex(three)%regex(four)%regex(five)%regex(six)%regex(seven)"
	if (!(result_string == '1_1')) {
		test_fail
	}
}
else {
	test_fail
}

# Checking capture groups are cleared out correctly when there are no matches
if (User-Name =~ /^.%{dummy_string}/) {
	result_string := "%{0}%regex(one)%regex(two)%regex(three)%regex(four)%regex(five)%regex(six)%regex(seven)"
	if (!(result_string == '1')) {
		test_fail
	}
}
else {
	test_fail
}

# compiled - ref - named capture groups
if (User-Name =~ /^(?<one>[0-9])_(?<two>[0-9])?_(?<three>[0-9]*)_(?<four>[0-9]+)_(?<five>[^_])_(?<six>6)_(?<seven>[7-8])/) {
	result_string := "%regex(seven)_%regex(six)_%regex(five)_%regex(four)_%regex(three)_%regex(two)_%regex(one)_%{0}"
	if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) {
		test_fail
	}
}
else {
	test_fail
}

# compiled - xlat - named capture groups
if ('1_2_3_4_5_6_7' =~ /^(?<one>[0-9])_(?<two>[0-9])?_(?<three>[0-9]*)_(?<four>[0-9]+)_(?<five>[^_])_(?<six>6)_(?<seven>[7-8])/) {
	result_string := "%regex(seven)_%regex(six)_%regex(five)_%regex(four)_%regex(three)_%regex(two)_%regex(one)_%{0}"
	if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) {
		test_fail
	}
}
else {
	test_fail
}

# compiled - ref - named capture groups (numeric indexes)
if (User-Name =~ /^(?<one>[0-9])_(?<two>[0-9])?_(?<three>[0-9]*)_(?<four>[0-9]+)_(?<five>[^_])_(?<six>6)_(?<seven>[7-8])/) {
	result_string := "%{7}_%{6}_%{5}_%{4}_%{3}_%{2}_%{1}_%{0}"
	if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) {
		test_fail
	}

}
else {
	test_fail
}

# compiled - ref - named capture groups (numeric indexes using expansion)
if (User-Name =~ /^(?<one>[0-9])_(?<two>[0-9])?_(?<three>[0-9]*)_(?<four>[0-9]+)_(?<five>[^_])_(?<six>6)_(?<seven>[7-8])/) {
	uint32 a
	uint32 b
	uint32 c
	uint32 d
	uint32 e
	uint32 f
	uint32 g
	uint32 h
	a := 0
	b := 1
	c := 2
	d := 3
	e := 4
	f := 5
	g := 6
	h := 7

	result_string := "%regex(%{h})_%regex(%{g})_%regex(%{f})_%regex(%{e})_%regex(%{d})_%regex(%{c})_%regex(%{b})_%regex(%{a})"
	if (!(result_string == '7_6_5_4_3_2_1_1_2_3_4_5_6_7')) {
		test_fail
	}
}
else {
	test_fail
}
}
