22require_relative '../../lib/ruby-handlebars/context'
33
44describe Handlebars ::Context do
5- include Handlebars :: Context
5+ let ( :ctx ) { described_class . new ( nil , data ) }
66
77 context 'get' do
8- before do
9- @data = {
10- key_data : 'Some value'
11- }
8+ let ( :data ) { {
9+ key_data : 'Some value'
10+ } }
1211
13- @locals = {
14- key_locals : 'Some other value' ,
15- a_list : [ 'a' , 'b' , 'c' ] ,
16- a_hash : { key : 'A third value' }
17- }
12+ before do
13+ ctx . add_item ( :key_locals , 'Some other value' )
14+ ctx . add_item ( :a_list , [ 'a' , 'b' , 'c' ] )
15+ ctx . add_item ( :a_hash , { key : 'A third value' } )
1816 end
1917
2018 it 'fetches data stored in the context' do
21- expect ( get ( 'key_data' ) ) . to eq ( 'Some value' )
22- expect ( get ( 'key_locals' ) ) . to eq ( 'Some other value' )
19+ expect ( ctx . get ( 'key_data' ) ) . to eq ( 'Some value' )
20+ expect ( ctx . get ( 'key_locals' ) ) . to eq ( 'Some other value' )
2321 end
2422
2523 it 'uses data from @locals before @data' do
26- @locals [ :key_data ] = 'Now stored in @locals'
24+ ctx . add_item ( :key_data , 'Now stored in @locals' )
2725
28- expect ( get ( 'key_data' ) ) . to eq ( 'Now stored in @locals' )
26+ expect ( ctx . get ( 'key_data' ) ) . to eq ( 'Now stored in @locals' )
2927 end
3028
3129 context 'when digging inside data' do
3230 it 'uses keys separated by dots' do
33- expect ( get ( 'a_hash.key' ) ) . to eq ( 'A third value' )
31+ expect ( ctx . get ( 'a_hash.key' ) ) . to eq ( 'A third value' )
3432 end
3533
3634 it 'can also use methods' do
37- expect ( get ( 'a_list.first' ) ) . to eq ( 'a' )
38- expect ( get ( 'a_list.last' ) ) . to eq ( 'c' )
35+ expect ( ctx . get ( 'a_list.first' ) ) . to eq ( 'a' )
36+ expect ( ctx . get ( 'a_list.last' ) ) . to eq ( 'c' )
3937 end
4038 end
4139 end
4240
4341 context 'add_item' do
42+ let ( :data ) { { } }
43+
4444 it 'adds a new key to the stored data' do
45- expect ( get ( 'my_key' ) ) . to be nil
46- add_item ( 'my_key' , 'With some value' )
47- expect ( get ( 'my_key' ) ) . to eq ( 'With some value' )
45+ expect ( ctx . get ( 'my_key' ) ) . to be nil
46+ ctx . add_item ( 'my_key' , 'With some value' )
47+ expect ( ctx . get ( 'my_key' ) ) . to eq ( 'With some value' )
4848 end
4949
5050 it 'overrides existing values' do
51- add_item ( 'a' , 12 )
52- add_item ( 'a' , 25 )
51+ ctx . add_item ( 'a' , 12 )
52+ ctx . add_item ( 'a' , 25 )
5353
54- expect ( get ( 'a' ) ) . to eq ( 25 )
54+ expect ( ctx . get ( 'a' ) ) . to eq ( 25 )
5555 end
5656
5757 it 'does not make differences between string and sym keys' do
58- add_item ( 'a' , 12 )
59- add_item ( :a , 25 )
58+ ctx . add_item ( 'a' , 12 )
59+ ctx . add_item ( :a , 25 )
6060
61- expect ( get ( 'a' ) ) . to eq ( 25 )
61+ expect ( ctx . get ( 'a' ) ) . to eq ( 25 )
6262 end
6363 end
6464
6565 context 'add_items' do
66+ let ( :data ) { { } }
67+
6668 it 'is basically a wrapper around add_item to add multiple items' do
67- allow ( self ) . to receive ( :add_item )
69+ allow ( ctx ) . to receive ( :add_item )
6870
69- add_items ( a : 'One key' , b : 'A second key' , c : 'A third key' )
70- expect ( self ) . to have_received ( :add_item ) . at_most ( 3 ) . times
71- expect ( self ) . to have_received ( :add_item ) . once . with ( :a , 'One key' )
72- expect ( self ) . to have_received ( :add_item ) . once . with ( :b , 'A second key' )
73- expect ( self ) . to have_received ( :add_item ) . once . with ( :c , 'A third key' )
71+ ctx . add_items ( a : 'One key' , b : 'A second key' , c : 'A third key' )
72+ expect ( ctx ) . to have_received ( :add_item ) . at_most ( 3 ) . times
73+ expect ( ctx ) . to have_received ( :add_item ) . once . with ( :a , 'One key' )
74+ expect ( ctx ) . to have_received ( :add_item ) . once . with ( :b , 'A second key' )
75+ expect ( ctx ) . to have_received ( :add_item ) . once . with ( :c , 'A third key' )
7476 end
7577 end
7678
7779 context 'with_temporary_context' do
80+ let ( :data ) { { } }
81+
7882 before do
79- add_items (
83+ ctx . add_items (
8084 key : 'some key' ,
8185 value : 'some value'
8286 )
8387 end
8488
8589 it 'allows creating temporary variables' do
86- expect ( get ( 'unknown_key' ) ) . to be nil
90+ expect ( ctx . get ( 'unknown_key' ) ) . to be nil
8791
88- with_temporary_context ( unknown_key : 42 ) do
89- expect ( get ( 'unknown_key' ) ) . to eq ( 42 )
92+ ctx . with_temporary_context ( unknown_key : 42 ) do
93+ expect ( ctx . get ( 'unknown_key' ) ) . to eq ( 42 )
9094 end
9195 end
9296
9397 it 'can override an existing variable' do
94- with_temporary_context ( value : 'A completelly new value' ) do
95- expect ( get ( 'value' ) ) . to eq ( 'A completelly new value' )
98+ ctx . with_temporary_context ( value : 'A completelly new value' ) do
99+ expect ( ctx . get ( 'value' ) ) . to eq ( 'A completelly new value' )
96100 end
97101 end
98102
99103 it 'provides mutable variables (well, variables ...)' do
100- with_temporary_context ( unknown_key : 42 ) do
101- expect ( get ( 'unknown_key' ) ) . to eq ( 42 )
102- add_item ( 'unknown_key' , 56 )
103- expect ( get ( 'unknown_key' ) ) . to eq ( 56 )
104+ ctx . with_temporary_context ( unknown_key : 42 ) do
105+ expect ( ctx . get ( 'unknown_key' ) ) . to eq ( 42 )
106+ ctx . add_item ( 'unknown_key' , 56 )
107+ expect ( ctx . get ( 'unknown_key' ) ) . to eq ( 56 )
104108 end
105109 end
106110
107111 it 'after the block, existing variables are restored' do
108- with_temporary_context ( value : 'A completelly new value' ) do
109- expect ( get ( 'value' ) ) . to eq ( 'A completelly new value' )
112+ ctx . with_temporary_context ( value : 'A completelly new value' ) do
113+ expect ( ctx . get ( 'value' ) ) . to eq ( 'A completelly new value' )
110114 end
111115
112- expect ( get ( 'value' ) ) . to eq ( 'some value' )
116+ expect ( ctx . get ( 'value' ) ) . to eq ( 'some value' )
113117 end
114118
115119 it 'after the block, the declared variables are not available anymore' do
116- with_temporary_context ( unknown_key : 42 ) do
117- expect ( get ( 'unknown_key' ) ) . to eq ( 42 )
120+ ctx . with_temporary_context ( unknown_key : 42 ) do
121+ expect ( ctx . get ( 'unknown_key' ) ) . to eq ( 42 )
118122 end
119123
120- expect ( get ( 'unknown_key' ) ) . to be nil
124+ expect ( ctx . get ( 'unknown_key' ) ) . to be nil
121125 end
122126
123127 it 'returns the data executed by the block' do
124- expect ( with_temporary_context ( value : 'A completelly new value' ) { 12 } ) . to eq ( 12 )
128+ expect ( ctx . with_temporary_context ( value : 'A completelly new value' ) { 12 } ) . to eq ( 12 )
125129 end
126130
127131 context 'when data are stored in @data' do
128- before do
129- @data = { my_key : "With some value" }
130- end
132+ let ( :data ) { { my_key : "With some value" } }
131133
132134 it 'let them available after the block is executed' do
133- expect ( get ( 'my_key' ) ) . to eq ( 'With some value' )
135+ expect ( ctx . get ( 'my_key' ) ) . to eq ( 'With some value' )
134136
135- with_temporary_context ( my_key : 12 ) do
136- expect ( get ( 'my_key' ) ) . to eq ( 12 )
137+ ctx . with_temporary_context ( my_key : 12 ) do
138+ expect ( ctx . get ( 'my_key' ) ) . to eq ( 12 )
137139 end
138140
139- expect ( get ( 'my_key' ) ) . to eq ( 'With some value' )
141+ expect ( ctx . get ( 'my_key' ) ) . to eq ( 'With some value' )
140142 end
141143 end
142144 end
143- end
145+ end
0 commit comments