1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2006-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%%-------------------------------------------------------------------
21%%% File    : xmerl_test_lib.erl
22%%% Author  : Bertil Karlsson <bertil@finrod>
23%%% Description :
24%%%
25%%% Created : 28 Apr 2006 by Bertil Karlsson <bertil@finrod>
26%%%-------------------------------------------------------------------
27-module(xmerl_test_lib).
28
29-compile(export_all).
30
31-include_lib("common_test/include/ct.hrl").
32-include_lib("xmerl/include/xmerl.hrl").
33
34%% cmp_element/2
35%% First argument result after parsing
36%% Second argument result after validation
37cmp_element(E,E) ->
38    ok;
39cmp_element(#xmlElement{name=N,attributes=A1,content=C1},
40	    #xmlElement{name=N,attributes=A2,content=C2}) ->
41    case cmp_attributes(A1,A2) of
42	ok ->
43	    cmp_elements(C1,C2);
44	Err -> Err
45    end;
46cmp_element(#xmlText{},#xmlText{}) ->
47    ok;
48cmp_element(A,B) ->
49    {error,{A,does_not_match,B}}.
50
51cmp_elements([H1|T1],[H2|T2]) ->
52    case cmp_element(H1,H2) of
53	ok ->
54	    cmp_elements(T1,T2);
55	Err ->
56	    Err
57    end;
58cmp_elements([],[]) ->
59    ok.
60
61%% All attributes in argument 1 must be present in 2
62cmp_attributes([A1|T1],Atts2) ->
63    case keysearch_delete(A1#xmlAttribute.name,#xmlAttribute.name,Atts2) of
64	{A2,NewAtts2} ->
65	    case A1#xmlAttribute.value == A2#xmlAttribute.value of
66		true ->
67		    cmp_attributes(T1,NewAtts2);
68		_ ->
69		    {error,{mismatching_values_in_attsibutes,A1,A2}}
70	    end;
71	_ ->
72	    {error,{no_matching_attsibute,A1,in,Atts2}}
73    end;
74cmp_attributes([],_) ->
75   ok.
76
77keysearch_delete(Key,N,List) ->
78    case lists:keysearch(Key,N,List) of
79	{value,Res} ->
80	    {Res,lists:keydelete(Key,N,List)};
81	_ ->
82	    false
83    end.
84
85
86%% Some test suites use the same testdata ("xmerl_sax_std_SUITE" and "xmerl_std_SUITE"),
87%% so the data directory is not cloned. This function retrieves the path to
88%% the original data directory.
89
90get_data_dir(Config) ->
91    Data = proplists:get_value(data_dir, Config),
92    Opts = [{return,list}],
93    re:replace(Data, "xmerl_sax_std_SUITE", "xmerl_std_SUITE", Opts).
94