commit fd60f7bf93336af8d21d886f03f962f3eaad8095
parent 9ec6dd74fc7b91501d359cefc1466a06b2dcb934
Author: Jan Pobrislo <ccx@te2000.cz>
Date: Tue, 29 Apr 2025 02:31:32 +0000
Organize and parametrize tests
Diffstat:
| M | doc/tests.pl | | | 100 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
1 file changed, 51 insertions(+), 49 deletions(-)
diff --git a/doc/tests.pl b/doc/tests.pl
@@ -12,6 +12,9 @@ test_result_matches(Template, Goal, Expected) :-
test_one_result(Template, Goal, Output),
assertion(Output =@= Expected).
+test_decode_dcg(Bytes, Goal, Expected1) :-
+ test_result_matches(Result, phrase(call(Goal, Result), Bytes), Expected1).
+
test_encode(Goal, ExpectedBytes) :-
test_one_result(Bytes, call(Goal, Bytes), OutputBytes),
string_bytes(Output, OutputBytes, octet),
@@ -24,55 +27,7 @@ test_encode_dcg(Goal, ExpectedBytes) :-
string_bytes(Expected, ExpectedBytes, octet),
assertion(Output =@= Expected).
-:- begin_tests(spec_netstring).
-
-test(prefix_encode_empty) :-
- PayloadBytes = ``,
- phrase(netstring_prefix_for_payload(PayloadBytes), PrefixBytes),
- assertion(PrefixBytes =@= `0:`).
-
-test(prefix_decode_empty) :-
- PrefixBytes = `0:`,
- phrase(netstring_prefix_for_payload(PayloadBytes), PrefixBytes),
- assertion(PayloadBytes =@= ``).
-
-test(encode_empty) :-
- netstring_encoding(netstring(``), Bytes),
- assertion(Bytes =@= `0:,`).
-
-test(decode_empty) :-
- netstring_encoding(netstring(PayloadBytes), `0:,`),
- assertion(PayloadBytes =@= ``).
-
-test(encode_single_null) :-
- netstring_encoding(netstring(`\0`), Bytes),
- assertion(Bytes =@= `1:\0,`).
-
-test(decode_single_null) :-
- netstring_encoding(netstring(PayloadBytes), `1:\0,`),
- assertion(PayloadBytes =@= `\0`).
-
-test(encode_ten_bytes) :-
- netstring_encoding(netstring(`1234567890`), Bytes),
- assertion(Bytes =@= `10:1234567890,`).
-
-test(decode_ten_bytes) :-
- netstring_encoding(netstring(PayloadBytes), `10:1234567890,`),
- assertion(PayloadBytes =@= `1234567890`).
-
-test(encode_nested) :-
- InnerPayload = [0, 255],
- PayloadGoal = netstring_encoding(netstring(InnerPayload)),
- netstring_of(PayloadGoal, Bytes),
- flatten([`5:2:\0`, 255, `,,`], ExpectedBytes),
- assertion(Bytes =@= ExpectedBytes).
-
-test(decode_nested) :-
- flatten([`5:2:\0`, 255, `,,`], Bytes),
- PayloadGoal = netstring_encoding(netstring(InnerPayload)),
- netstring_of(PayloadGoal, Bytes),
- assertion(InnerPayload =@= [0, 255]).
-
+:- begin_tests(spec_helpers).
test(dcgappend1) :-
phrase_length(``, Bytes, [], Length),
@@ -104,6 +59,53 @@ test(dcgappend5) :-
assertion(Next =@= `efgh`),
assertion(Length1 =@= 4).
+:- end_tests(spec_helpers).
+
+:- begin_tests(spec_netstring).
+
+test(prefix_encode_empty) :-
+ PayloadBytes = ``,
+ phrase(netstring_prefix_for_payload(PayloadBytes), PrefixBytes),
+ assertion(PrefixBytes =@= `0:`).
+
+test(prefix_decode_empty) :-
+ PrefixBytes = `0:`,
+ phrase(netstring_prefix_for_payload(PayloadBytes), PrefixBytes),
+ assertion(PayloadBytes =@= ``).
+
+values_netstring_bytes(empty, ``, `0:,`).
+values_netstring_bytes(single_null, `\0`, `1:\0,`).
+values_netstring_bytes(ten_bytes, `1234567890`, `10:1234567890,`).
+
+test(netstring_bytes_encode, [
+ forall(values_netstring_bytes(_Name, PayloadBytes, Bytes))
+]) :-
+ test_encode_dcg(netstring_bytes(PayloadBytes), Bytes).
+
+test(netstring_bytes_decode, [
+ forall(values_netstring_bytes(_Name, PayloadBytes, Bytes))
+]) :-
+ test_result_matches(Result, phrase(netstring_bytes(Result), Bytes), PayloadBytes).
+
+test(netstring_bytes_decode_variant, [
+ forall(values_netstring_bytes(_Name, PayloadBytes, Bytes))
+]) :-
+ test_decode_dcg(Bytes, netstring_bytes, PayloadBytes).
+
+test(encode_nested) :-
+ InnerPayload = [0, 255],
+ flatten([`5:2:\0`, 255, `,,`], ExpectedBytes),
+ PayloadGoal = netstring_encoding(netstring(InnerPayload)),
+ netstring_of(PayloadGoal, Bytes),
+ assertion(Bytes =@= ExpectedBytes).
+
+test(decode_nested) :-
+ ExpectedPayload = [0, 255],
+ flatten([`5:2:\0`, 255, `,,`], Bytes),
+ PayloadGoal = netstring_encoding(netstring(InnerPayload)),
+ netstring_of(PayloadGoal, Bytes),
+ assertion(InnerPayload =@= ExpectedPayload).
+
test(encode_netstring_call_dcg) :-
InnerPayload = [0, 255],
flatten([`5:2:\0`, 255, `,,`], ExpectedBytes),