Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
e9c0e204
Commit
e9c0e204
authored
Jul 13, 2020
by
nmilojevic1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor reference filter spec
- Replace changed_nodes with new_nodes
parent
486ad968
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
54 deletions
+39
-54
lib/banzai/filter/reference_filter.rb
lib/banzai/filter/reference_filter.rb
+15
-15
spec/lib/banzai/filter/reference_filter_spec.rb
spec/lib/banzai/filter/reference_filter_spec.rb
+24
-39
No files found.
lib/banzai/filter/reference_filter.rb
View file @
e9c0e204
...
...
@@ -26,7 +26,7 @@ module Banzai
super
if
update_nodes_enabled?
@
changed
_nodes
=
{}
@
new
_nodes
=
{}
@nodes
=
self
.
result
[
:reference_filter_nodes
]
end
end
...
...
@@ -166,31 +166,31 @@ module Banzai
def
replace_text_with_html
(
node
,
index
,
html
)
if
update_nodes_enabled?
replace_and_update_
changed
_nodes
(
node
,
index
,
html
)
replace_and_update_
new
_nodes
(
node
,
index
,
html
)
else
node
.
replace
(
html
)
end
end
def
replace_and_update_
changed
_nodes
(
node
,
index
,
html
)
def
replace_and_update_
new
_nodes
(
node
,
index
,
html
)
previous_node
=
node
.
previous
next_node
=
node
.
next
parent_node
=
node
.
parent
# Unfortunately node.replace(html) returns re-parented nodes, not the actual replaced nodes in the doc
# We need to find the actual nodes in the doc that were replaced
node
.
replace
(
html
)
@
changed
_nodes
[
index
]
=
[]
@
new
_nodes
[
index
]
=
[]
# We
find first replaced
node. If previous_node is nil, we take first parent child
replaced
_node
=
previous_node
?
previous_node
.
next
:
parent_node
&
.
children
&
.
first
# We
replaced node with new nodes, so we find first new
node. If previous_node is nil, we take first parent child
new
_node
=
previous_node
?
previous_node
.
next
:
parent_node
&
.
children
&
.
first
# We iterate from first to last replaced node and store replaced nodes in @
changed
_nodes
while
replaced_node
&&
replaced
_node
!=
next_node
@
changed_nodes
[
index
]
<<
replaced
_node
.
xpath
(
query
)
replaced_node
=
replaced
_node
.
next
# We iterate from first to last replaced node and store replaced nodes in @
new
_nodes
while
new_node
&&
new
_node
!=
next_node
@
new_nodes
[
index
]
<<
new
_node
.
xpath
(
query
)
new_node
=
new
_node
.
next
end
@
changed
_nodes
[
index
].
flatten!
@
new
_nodes
[
index
].
flatten!
end
def
only_path?
...
...
@@ -198,14 +198,14 @@ module Banzai
end
def
with_update_nodes
@
changed
_nodes
=
{}
@
new
_nodes
=
{}
yield
.
tap
{
update_nodes!
}
end
# Once Filter completes replacing nodes, we update nodes with @
changed
_nodes
# Once Filter completes replacing nodes, we update nodes with @
new
_nodes
def
update_nodes!
@
changed_nodes
.
sort_by
{
|
index
,
_changed_nodes
|
-
index
}.
each
do
|
index
,
changed
_nodes
|
nodes
[
index
,
1
]
=
changed
_nodes
@
new_nodes
.
sort_by
{
|
index
,
_new_nodes
|
-
index
}.
each
do
|
index
,
new
_nodes
|
nodes
[
index
,
1
]
=
new
_nodes
end
result
[
:reference_filter_nodes
]
=
nodes
end
...
...
spec/lib/banzai/filter/reference_filter_spec.rb
View file @
e9c0e204
...
...
@@ -59,10 +59,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
end
RSpec
.
shared_context
'
changed
nodes'
do
RSpec
.
shared_context
'
new
nodes'
do
let
(
:nodes
)
{
[{
value:
"1"
},
{
value:
"2"
},
{
value:
"3"
}]
}
let
(
:expected_nodes
)
{
[{
value:
"1.1"
},
{
value:
"1.2"
},
{
value:
"1.3"
},
{
value:
"2.1"
},
{
value:
"2.2"
},
{
value:
"2.3"
},
{
value:
"3.1"
},
{
value:
"3.2"
},
{
value:
"3.3"
}]
}
let
(
:
changed
_nodes
)
do
let
(
:
new
_nodes
)
do
{
0
=>
[{
value:
"1.1"
},
{
value:
"1.2"
},
{
value:
"1.3"
}],
2
=>
[{
value:
"3.1"
},
{
value:
"3.2"
},
{
value:
"3.3"
}],
...
...
@@ -95,20 +95,20 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
expect
(
document
.
css
(
'a'
).
length
).
to
eq
1
end
it
'calls replace_and_update_
changed
_nodes'
do
expect
(
filter
).
to
receive
(
:replace_and_update_
changed
_nodes
).
with
(
filter
.
nodes
[
index
],
index
,
html
)
it
'calls replace_and_update_
new
_nodes'
do
expect
(
filter
).
to
receive
(
:replace_and_update_
new
_nodes
).
with
(
filter
.
nodes
[
index
],
index
,
html
)
filter
.
send
(
method_name
,
*
args
)
do
html
end
end
it
'stores filtered
replaced
nodes'
do
it
'stores filtered
new
nodes'
do
filter
.
send
(
method_name
,
*
args
)
do
html
end
expect
(
filter
.
instance_variable_get
(
:@
changed
_nodes
)).
to
eq
({
index
=>
[
filter
.
each_node
.
to_a
[
index
]]
})
expect
(
filter
.
instance_variable_get
(
:@
new
_nodes
)).
to
eq
({
index
=>
[
filter
.
each_node
.
to_a
[
index
]]
})
end
context
"with update_nodes_for_banzai_reference_filter feature flag disabled"
do
...
...
@@ -116,8 +116,8 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags
(
update_nodes_for_banzai_reference_filter:
false
)
end
it
'does not call replace_and_update_
changed
_nodes'
do
expect
(
filter
).
not_to
receive
(
:replace_and_update_
changed
_nodes
).
with
(
filter
.
nodes
[
index
],
index
,
html
)
it
'does not call replace_and_update_
new
_nodes'
do
expect
(
filter
).
not_to
receive
(
:replace_and_update_
new
_nodes
).
with
(
filter
.
nodes
[
index
],
index
,
html
)
filter
.
send
(
method_name
,
*
args
)
do
html
...
...
@@ -158,8 +158,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
end
RSpec
.
shared_examples
'replaces text when pattern
matches'
do
describe
'#replace_text_when_pattern_
matches'
do
include_context
'document nodes'
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'text @reference'
)
}
let
(
:ref_pattern
)
{
%r{(?<!
\w
)@(?<user>[a-zA-Z0-9_
\-\.
]*)}x
}
...
...
@@ -177,16 +178,18 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
end
RSpec
.
shared_examples
'replaces link node when
text'
do
describe
'#replace_link_node_with_
text'
do
include_context
'document nodes'
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'<a>end text</a>'
)
}
it_behaves_like
'replaces document node'
,
:replace_link_node_with_text
do
let
(
:existing_content
)
{
node
.
text
}
end
end
RSpec
.
shared_examples
'replaces link node with
href'
do
describe
'#replace_link_node_with_
href'
do
include_context
'document nodes'
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'<a href="link">end text</a>'
)
}
let
(
:href_link
)
{
CGI
.
unescape
(
node
.
attr
(
'href'
).
to_s
)
}
it_behaves_like
'replaces document node'
,
:replace_link_node_with_href
do
...
...
@@ -194,27 +197,9 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
end
describe
'#replace_text_when_pattern_matches'
do
it_behaves_like
'replaces text when pattern matches'
do
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'text @reference'
)
}
end
end
describe
'#replace_link_node_with_text'
do
it_behaves_like
'replaces link node when text'
do
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'<a>end text</a>'
)
}
end
end
describe
'#replace_link_node_with_href'
do
it_behaves_like
'replaces link node with href'
do
let
(
:node
)
{
Nokogiri
::
HTML
.
fragment
(
'<a href="link">end text</a>'
)
}
end
end
describe
"#call_and_update_nodes"
do
context
"with update_nodes_for_banzai_reference_filter feature flag enabled"
do
include_context
'
changed
nodes'
include_context
'
new
nodes'
let
(
:document
)
{
Nokogiri
::
HTML
.
fragment
(
'<a href="foo">foo</a>'
)
}
let
(
:filter
)
{
described_class
.
new
(
document
,
project:
project
)
}
...
...
@@ -222,10 +207,10 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags
(
update_nodes_for_banzai_reference_filter:
true
)
end
it
"
replaces all
nodes"
,
:aggregate_failures
do
it
"
updates all new
nodes"
,
:aggregate_failures
do
filter
.
instance_variable_set
(
'@nodes'
,
nodes
)
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
changed_nodes'
,
changed
_nodes
)
}
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
new_nodes'
,
new
_nodes
)
}
expect
(
filter
).
to
receive
(
:with_update_nodes
).
and_call_original
expect
(
filter
).
to
receive
(
:update_nodes!
).
and_call_original
...
...
@@ -236,7 +221,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
context
"with update_nodes_for_banzai_reference_filter feature flag disabled"
do
include_context
'
changed
nodes'
include_context
'
new
nodes'
before
do
stub_feature_flags
(
update_nodes_for_banzai_reference_filter:
false
)
...
...
@@ -247,7 +232,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
filter
=
described_class
.
new
(
document
,
project:
project
)
filter
.
instance_variable_set
(
'@nodes'
,
nodes
)
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
changed_nodes'
,
changed
_nodes
)
}
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
new_nodes'
,
new
_nodes
)
}
expect
(
filter
).
not_to
receive
(
:with_update_nodes
)
expect
(
filter
).
not_to
receive
(
:update_nodes!
)
...
...
@@ -260,7 +245,7 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
end
describe
".call"
do
include_context
'
changed
nodes'
include_context
'
new
nodes'
let
(
:document
)
{
Nokogiri
::
HTML
.
fragment
(
'<a href="foo">foo</a>'
)
}
...
...
@@ -270,11 +255,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags
(
update_nodes_for_banzai_reference_filter:
true
)
end
it
"
replac
es all nodes"
,
:aggregate_failures
do
it
"
updat
es all nodes"
,
:aggregate_failures
do
expect_next_instance_of
(
described_class
)
do
|
filter
|
expect
(
filter
).
to
receive
(
:call_and_update_nodes
).
and_call_original
expect
(
filter
).
to
receive
(
:with_update_nodes
).
and_call_original
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
changed_nodes'
,
changed
_nodes
)
}
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
new_nodes'
,
new
_nodes
)
}
expect
(
filter
).
to
receive
(
:update_nodes!
).
and_call_original
end
...
...
@@ -290,11 +275,11 @@ RSpec.describe Banzai::Filter::ReferenceFilter do
stub_feature_flags
(
update_nodes_for_banzai_reference_filter:
false
)
end
it
"
replace
all nodes"
,
:aggregate_failures
do
it
"
updates
all nodes"
,
:aggregate_failures
do
expect_next_instance_of
(
described_class
)
do
|
filter
|
expect
(
filter
).
to
receive
(
:call_and_update_nodes
).
and_call_original
expect
(
filter
).
not_to
receive
(
:with_update_nodes
)
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
changed_nodes'
,
changed
_nodes
)
}
expect
(
filter
).
to
receive
(
:call
)
{
filter
.
instance_variable_set
(
'@
new_nodes'
,
new
_nodes
)
}
expect
(
filter
).
not_to
receive
(
:update_nodes!
)
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment