FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppspec/spec/lifecycle_spec.cpp at main · toroidal-code/cppspec · GitHub
toroidal-code
/
cppspec
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
14
Code
Issues
3
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppspec
/
spec
/
lifecycle_spec.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
141 lines (110 loc) · 3.7 KB
Breadcrumbs
cppspec
/
spec
/
lifecycle_spec.cpp
Copy path
File metadata and controls
141 lines (110 loc) · 3.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#
include
<
cstdlib
>
#
include
"
cppspec.hpp
"
using
namespace
CppSpec
;
namespace
{
int
before_each_calls =
0
;
int
after_each_state =
0
;
int
before_all_init =
0
;
int
after_all_x =
0
;
int
hook_n =
0
;
int
stacked_total =
0
;
}
//
namespace
//
before_each runs once per it, not once per describe
describe
before_each_ordering_spec
(
"
before_each ordering
"
, $ {
before_each
([] { before_each_calls++; });
it
(
"
first it: calls == 1
"
, _ {
expect
(before_each_calls).
to_equal
(
1
);
});
it
(
"
second it: calls == 2 (incremented again)
"
, _ {
expect
(before_each_calls).
to_equal
(
2
);
});
it
(
"
third it: calls == 3
"
, _ {
expect
(before_each_calls).
to_equal
(
3
);
});
});
//
after_each runs after every it
describe
after_each_ordering_spec
(
"
after_each ordering
"
, $ {
before_each
([] { after_each_state =
10
; });
after_each
([] { after_each_state =
0
; });
it
(
"
sees state == 10 at start
"
, _ {
expect
(after_each_state).
to_equal
(
10
);
after_each_state =
99
;
//
mutate — after_each resets it
});
it
(
"
sees state == 10 again (reset by after_each)
"
, _ {
expect
(after_each_state).
to_equal
(
10
);
});
it
(
"
state is always clean
"
, _ {
expect
(after_each_state).
to_equal
(
10
);
});
});
//
before_all runs exactly once at the start of the block
describe
before_all_spec
(
"
before_all runs once
"
, $ {
before_all
([] { before_all_init =
42
; });
it
(
"
sees the before_all value
"
, _ {
expect
(before_all_init).
to_equal
(
42
);
});
it
(
"
value is not reset between its
"
, _ {
expect
(before_all_init).
to_equal
(
42
);
before_all_init =
99
;
});
it
(
"
now sees the modified value (before_all did NOT re-run)
"
, _ {
expect
(before_all_init).
to_equal
(
99
);
});
});
//
after_all fires after all its in a context are done
describe
after_all_timing_spec
(
"
after_all timing
"
, $ {
context
(
"
inner context with after_all
"
, _ {
after_all
([] { after_all_x =
777
; });
it
(
"
runs while x is still 0
"
, _ {
expect
(after_all_x).
to_equal
(
0
);
});
it
(
"
still 0 before after_all fires
"
, _ {
expect
(after_all_x).
to_equal
(
0
);
});
//
after_all fires here
});
it
(
"
outer it sees x == 777 after inner after_all ran
"
, _ {
expect
(after_all_x).
to_equal
(
777
);
});
});
//
hooks propagate into nested contexts
describe
hook_propagation_spec
(
"
hook propagation into nested contexts
"
, $ {
before_each
([] { hook_n =
1
; });
after_each
([] { hook_n =
0
; });
it
(
"
outer it sees n == 1
"
, _ {
expect
(hook_n).
to_equal
(
1
);
});
context
(
"
nested context inherits outer hooks
"
, _ {
it
(
"
inner it also sees n == 1 from outer before_each
"
, _ {
expect
(hook_n).
to_equal
(
1
);
});
it
(
"
inner it gets fresh n each time
"
, _ {
expect
(hook_n).
to_equal
(
1
);
hook_n =
99
;
});
it
(
"
after_each reset: n is 1 again
"
, _ {
expect
(hook_n).
to_equal
(
1
);
});
});
it
(
"
outer it after nested still gets n == 1
"
, _ {
expect
(hook_n).
to_equal
(
1
);
});
});
//
additional before_each in nested context stacks on top of parent
describe
stacked_hooks_spec
(
"
stacked hooks in nested contexts
"
, $ {
before_each
([] { stacked_total =
0
; stacked_total +=
1
; });
//
outer: reset then add 1
context
(
"
inner adds more
"
, _ {
before_each
([] { stacked_total +=
10
; });
//
runs after outer: 0 + 1 + 10 = 11
it
(
"
sees total == 11 (1 from outer + 10 from inner)
"
, _ {
expect
(stacked_total).
to_equal
(
11
);
});
it
(
"
each it starts fresh: total == 11 again
"
, _ {
expect
(stacked_total).
to_equal
(
11
);
});
});
it
(
"
outer it only gets outer before_each: total == 1
"
, _ {
expect
(stacked_total).
to_equal
(
1
);
});
});
CPPSPEC_MAIN
(before_each_ordering_spec, after_each_ordering_spec, before_all_spec,
after_all_timing_spec, hook_propagation_spec, stacked_hooks_spec);
Back
|
FazBrowse Home
|
New Git URL