-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a macro for asserting if two xmlel's are equal #30
Conversation
src/exml_utils.erl
Outdated
|
||
-include_lib("exml/include/exml_stream.hrl"). | ||
|
||
-type xmlstreamelement() :: #xmlel{} | #xmlstreamstart{} | #xmlstreamend{}. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Compiler:
Warning: type xmlstreamelement() is unused
@mentels any chance for finishing this sooner or later? |
Will have a look this week. |
a17386d
to
41912ce
Compare
Codecov Report
@@ Coverage Diff @@
## master #30 +/- ##
==========================================
+ Coverage 87.31% 87.94% +0.62%
==========================================
Files 4 4
Lines 134 141 +7
==========================================
+ Hits 117 124 +7
Misses 17 17
Continue to review full report at Codecov.
|
@michalwski it's ready. @erszcz have a look if the credits are OK. |
@mentels More than OK. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pushing this forward. The code looks good. Tests are also great with a room for a small improvement (details in comments to the code).
children = lists:sort([ xml_sort(C) || C <- Children ]) | ||
}; | ||
xml_sort(#xmlstreamstart{ attrs = Attrs } = StreamStart) -> | ||
StreamStart#xmlstreamstart{ attrs = lists:sort(Attrs) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any chance to cover this function clause with tests? It's not covered at this moment.
src/exml.erl
Outdated
xml_sort(Elements) when is_list(Elements) -> | ||
lists:sort([ xml_sort(E) || E <- Elements ]); | ||
xml_sort(Unknown) -> | ||
Unknown. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we need that at all. According to the function spec everything is covered and we never get to this clause (as long as valid item() is passed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember adding it for the sake of convenience, when I was figuring out how to return stuff from the Rust parser to the Erlang layer and I returned some rubbish just to test the mechanism. It might not be needed outside experimental code and even if it would, it's trivial to add again.
xml_sort(#xmlstreamstart{ attrs = Attrs } = StreamStart) -> | ||
StreamStart#xmlstreamstart{ attrs = lists:sort(Attrs) }; | ||
xml_sort(#xmlstreamend{} = StreamEnd) -> | ||
StreamEnd; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to cover this one with tests as well.
src/exml.erl
Outdated
#xmlel{ attrs = Attrs, children = Children } = El, | ||
El#xmlel{ | ||
attrs = lists:sort(Attrs), | ||
children = lists:sort([ xml_sort(C) || C <- Children ]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XML is order sensitive so sorting children can cause false positives in macro. I know that it might be useful to not care about the order sometimes but I'd prefer this to be less implicit, if we're going to keep it like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, good catch. Attributes should be sorted, but children should not. The original did not sort here - https://github.com/erszcz/rxml/blob/e8483408663f0bc2af7896e786c1cdea2e86e43d/test/exml_test.hrl#L23
Yeah, I was thinking about it and it seemed to me that the children are
order-independent too. But apparently they're not (I assume you're right).
Will fix it. Thanks.
…On Tue, Nov 20, 2018 at 1:23 PM Radosław Szymczyszyn < ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/exml.erl
<#30 (comment)>:
> +%% Sorting is defined as calling `lists:sort/1` at:
+%% * all the `xmlel's provided (if there is a list of them) AND
+%% * all the `xmlel' elements' attributes recursively (the root and descendants) AND
+%% * all the `xmlel' children recursively (the root and descendants).
+%% The order is ascending.
+%%
+%% The implementation of this function is subtle modification of
+%% erszcz/rxml@e848340
+-spec <erszcz/rxml@e8483408663f0bc2af7896e786c1cdea2e86e43d+-spec> xml_sort(item() | [item()]) -> item() | [item()].
+xml_sort(#xmlcdata{} = Cdata) ->
+ Cdata;
+xml_sort(#xmlel{} = El) ->
+ #xmlel{ attrs = Attrs, children = Children } = El,
+ El#xmlel{
+ attrs = lists:sort(Attrs),
+ children = lists:sort([ xml_sort(C) || C <- Children ])
Indeed, good catch. Attributes should be sorted, but children should not.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30 (comment)>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AAtsQBTNMGTVfzGJTw-MIZotnD33kH1mks5uw_RWgaJpZM4Ott6r>
.
--
Szymon Mentel
M: +48 795 419 568
W: http://www.erlang-solutions.com
E: szymon.mentel@erlang-solutions.com
Code Sync and Erlang Solutions conferences:
Code BEAM Lite <https://codesync.global/conferences/cbl-amsterdam-2018/> -
Amsterdam - 30 November 2018
Code BEAM Lite <https://codesync.global/conferences/cbl-munich-2018/> -
Munich - 7 December 2018
Lambda Days <http://www.lambdadays.org/lambdadays2019> - Kraków -
21-22 February 2019
Code BEAM SF <https://codesync.global/conferences/code-beam-sf-2019/> - San
Francisco - 28 February - 1 March 2019
Code BEAM Lite
<https://codesync.global/conferences/code-beam-lite-bologna/>- Bologna -
22 March 2019
ElixirConf EU <https://www.elixirconf.eu/> - Prague - 8-9 April 2019
Code BEAM STO <https://codesync.global/conferences/code-beam-sto-2019/> -
Stockholm - 16-17 May 2019
Code Elixir LDN <https://codesync.global/conferences/code-elixir-ldn-2019/> -
London - 18 July 2019
Code Mesh LDN - London - 8-9 November 2019
*Erlang Solutions and Code Sync care about your data and privacy; please
find all details about the basis for communicating with you and the way we
process your data in our Privacy Policy
<https://www.erlang-solutions.com/privacy-policy.html> You can update
your email preferences or opt-out from receiving marketing emails here
<http://www2.erlang-solutions.com/emailpreference>.*
|
The PR is improved. Will squash the commits once accepted. Have a look @michalwski @erszcz @fenek |
test/exml_tests.erl
Outdated
sort_xmlstreamend_test() -> | ||
SE1 = #xmlstreamend{name = <<"n1">>}, | ||
SE2 = SE1, | ||
?assertEqual(SE1, SE2). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mentels Did you miss a call to exml:xml_sort on one of the arguments?
Ah, yeah, I did :)
…On Fri, Nov 23, 2018 at 1:33 PM Michał Piotrowski ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In test/exml_tests.erl
<#30 (comment)>:
> @@ -71,7 +71,20 @@ sort_xmlel_nested_test() ->
},
El2 = El1#xmlel{ children = lists:reverse(Children) },
?assertNotEqual(El1, El2),
- ?assertEqual(exml:xml_sort(El1), exml:xml_sort(El2)).
+ %% children order matters
+ ?assertNotEqual(exml:xml_sort(El1), exml:xml_sort(El2)).
+
+sort_xmlstreamstart_test() ->
+ Attrs = [{<<"attr1">>, <<"bar">>}, {<<"attr2">>, <<"baz">>}],
+ SS1 = #xmlstreamstart{name = <<"n1">>, attrs = Attrs},
+ SS2 = SS1#xmlstreamstart{attrs = lists:reverse(Attrs)},
+ ?assertNotEqual(SS1, SS2),
+ ?assertEqual(exml:xml_sort(SS1), exml:xml_sort(SS2)).
+
+sort_xmlstreamend_test() ->
+ SE1 = #xmlstreamend{name = <<"n1">>},
+ SE2 = SE1,
+ ?assertEqual(SE1, SE2).
@mentels <https://github.com/mentels> Did you miss a call to
exml:xml_sort on one of the arguments?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#30 (review)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAtsQEmUfz4WXktBBo9tpAE5hqIOAT8tks5ux-sIgaJpZM4Ott6r>
.
--
Szymon Mentel
M: +48 795 419 568
W: http://www.erlang-solutions.com
E: szymon.mentel@erlang-solutions.com
Code Sync and Erlang Solutions conferences:
Code BEAM Lite <https://codesync.global/conferences/cbl-amsterdam-2018/> -
Amsterdam - 30 November 2018
Code BEAM Lite <https://codesync.global/conferences/cbl-munich-2018/> -
Munich - 7 December 2018
Lambda Days <http://www.lambdadays.org/lambdadays2019> - Kraków -
21-22 February 2019
Code BEAM SF <https://codesync.global/conferences/code-beam-sf-2019/> - San
Francisco - 28 February - 1 March 2019
Code BEAM Lite
<https://codesync.global/conferences/code-beam-lite-bologna/>- Bologna -
22 March 2019
ElixirConf EU <https://www.elixirconf.eu/> - Prague - 8-9 April 2019
Code BEAM STO <https://codesync.global/conferences/code-beam-sto-2019/> -
Stockholm - 16-17 May 2019
Code Elixir LDN <https://codesync.global/conferences/code-elixir-ldn-2019/> -
London - 18 July 2019
Code Mesh LDN - London - 8-9 November 2019
*Erlang Solutions and Code Sync care about your data and privacy; please
find all details about the basis for communicating with you and the way we
process your data in our Privacy Policy
<https://www.erlang-solutions.com/privacy-policy.html> You can update
your email preferences or opt-out from receiving marketing emails here
<http://www2.erlang-solutions.com/emailpreference>.*
|
Looks good to me now. Thanks! |
@mentels Looks good. If you'd like to squash commits, then I think you may do so now. :) |
The assertion is meant to be used in tests.
28b9bf4
to
4c9f845
Compare
@fenek done. |
The assertion is meant to be used in tests.
It's still lacking: