-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsing_test.go
109 lines (100 loc) · 3.82 KB
/
parsing_test.go
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
package eerror
import (
"testing"
)
func TestSerialization(t *testing.T) {
const errorMessage = "This is a test error"
const contextMessage = "This is some context"
err := NewError(E_TESTERROR, errorMessage)
if expected := E_TESTERROR + ": " + errorMessage; err.Error() != expected {
t.Error("Invalid error formatting (result, expected)\n", err.Error()+"\n", expected)
}
err.WithAttribute("some attribute", "some value")
err.WithAttribute("some typed attribute", 42)
if expected := E_TESTERROR + ": " + errorMessage + " [some attribute: some value, some typed attribute: (int)42]"; err.Error() != expected {
t.Error("Invalid error formatting (result, expected)\n", err.Error()+"\n", expected)
}
err.InContext(contextMessage)
if expected := E_TESTERROR + ": " + errorMessage + " (" + contextMessage + ") [some attribute: some value, some typed attribute: (int)42]"; err.Error() != expected {
t.Error("Invalid error formatting (result, expected)\n", err.Error()+"\n", expected)
}
failedTest, failedError, validTestsSuccess := (func() (test string, eerr Eerror, ok bool) {
for _, test = range []string{
"E_SOMEERROR: msg",
"E_SOMEERROR: \"\"",
"\":\": \"\"",
"E : \"\"",
"\t\n E: \"\" \t\t\n",
"E: m",
"E some type: error message",
"\"E some type: \": error message",
"E_SOMEERROR: message ",
"E_SOMEERROR: message ()",
"E_SOMEERROR: message (context) []",
"E_SOMEERROR: message (context)",
"E_SOMEERROR: message (a;context)",
"E_SOMEERROR: message (context) [attribute: value]",
"E_SOMEERROR: message (context; \"another, very long, context\")",
"E_SOMEERROR: message (context; \"a (special) one\")",
"E_SOMEERROR: message (context; another; more; contexts)",
"E_SOMEERROR: message (the same unique context but with spaces)",
"E_SOMEERROR: message (context) [attribute: and its value]",
"E_SOMEERROR: message (context) [attribute: \"and; its value\"]",
"E_SOMEERROR: message (context) [attribute: value, another attribute: value]",
"E_SOMEERROR: message (context) [attribute: (int)-1]",
"E_SOMEERROR: message (context) [attribute: \"(int)string value\"]",
"E_SOMEERROR: \"some long; and (very) [complex message]\" (context)",
} {
if eerr, ok = parse(test); !ok {
return
}
}
return
})()
if !validTestsSuccess {
t.Error("Parsing test should have succeed, but faild (test, built error)\n", failedTest+"\n", failedError)
return
}
failedTest, failedError, invalidTestsFailed := (func() (test string, eerr Eerror, ok bool) {
for _, test = range []string{
"E_SOMEERROR:",
"E_SOMEERROR: ",
"E_SOMEERROR:\"\"",
": \"\"",
"\"\"",
"E: \"",
"E some type: \"",
"\"E some type: \":",
"E_SOMEERROR: message (",
"E_SOMEERROR: message (context;)",
"E_SOMEERROR: message (;context)",
"E_SOMEERROR: message (context) [",
"E_SOMEERROR: message (context) ]",
"E_SOMEERROR: message (context) string",
"E_SOMEERROR: message (context",
"E_SOMEERROR: message[ (context)",
"E_SOMEERROR: message[] (context)",
"E_SOMEERROR: message (context) [attribute]",
"E_SOMEERROR: message [attribute: value] (context)",
"E_SOMEERROR: message (context) [attribute: value",
"E_SOMEERROR: message (context) [attribute: \"value]",
"E_SOMEERROR: message (context) [\"attribute: \"value]",
"E_SOMEERROR: message (context) \"attribute: \": value]",
"E_SOMEERROR: message [\"attribute: \" value]",
"E_SOMEERROR: message [attribute]",
} {
if eerr, ok = parse(test); ok {
return
}
}
ok = false
return
})()
if invalidTestsFailed {
t.Error("Parsing test should've failed, but succeeded (test, built error)", failedTest+"\n", failedError)
}
eerr := From(err.Error())
if eerr.Error() != err.Error() {
t.Error("Bad parsing, both should be equals (result, expected)\n", err.Error()+"\n", eerr.Error())
}
}