-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmessage.wlt
114 lines (100 loc) · 2.93 KB
/
message.wlt
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<|
"message" -> <|
"init" -> {
Global`message = SetReplace`PackageScope`message;
Global`declareMessage = SetReplace`PackageScope`declareMessage;
Global`throw = SetReplace`PackageScope`throw;
Attributes[Global`testUnevaluated] = {HoldAll};
Global`testUnevaluated[args___] := SetReplace`PackageScope`testUnevaluated[VerificationTest, args];
HalfInteger[x_] := ModuleScope[
result = Catch[halfInteger[x], _ ? FailureQ, message[HalfInteger]];
result /; !FailureQ[result]
];
halfInteger[x_ ? EvenQ] := x / 2;
declareMessage[HalfInteger::odd, "The number `number` is odd."];
halfInteger[x_ ? OddQ] := throw[Failure["odd", <|"number" -> x|>]];
},
"tests" -> {
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb::msg, <|"a" -> 0, "b" -> 1|>]
,
Null
,
{symb::msg}
],
VerificationTest[
declareMessage[General::msg, "Msg `a` of `b`."];
message[symb::msg, <|"a" -> 0, "b" -> 1|>]
,
Null
,
{symb::msg}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb::wrongName, <|"a" -> 0, "b" -> 1|>]
,
Null
,
{SetReplace`PackageScope`message::messageNotFound}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb::msg, <|"a" -> 0|>]
,
Null
,
{SetReplace`PackageScope`message::missingArgs}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb, Failure["msg", <|"a" -> 0, "b" -> 1|>]]
,
Failure["msg", <|"a" -> 0, "b" -> 1|>]
,
{symb::msg}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb][Failure["msg", <|"a" -> 0, "b" -> 1|>]]
,
Failure["msg", <|"a" -> 0, "b" -> 1|>]
,
{symb::msg}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
message[symb][Failure["msg", <|"a" -> 0, "b" -> 1|>], Failure["msg", <|"a" -> 0, "b" -> 1|>]]
,
Failure["msg", <|"a" -> 0, "b" -> 1|>]
,
{symb::msg}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a`, `b` and `c`."];
message[symb, Failure["msg", <|"a" -> 0, "b" -> 1|>], <|"c" -> 2|>]
,
Failure["msg", <|"a" -> 0, "b" -> 1, "c" -> 2|>]
,
{symb::msg}
],
VerificationTest[
declareMessage[symb::msg, "Msg `a` of `b`."];
Catch[throw[Failure["msg", <|"a" -> 0, "b" -> 1|>]], _ ? FailureQ, message[symb]]
,
Failure["msg", <|"a" -> 0, "b" -> 1|>]
,
{symb::msg}
],
VerificationTest[
HalfInteger[2],
1
],
testUnevaluated[
HalfInteger[1],
HalfInteger::odd
]
}
|>
|>