/******************************************************************************
 * Copyright (c) 2000-2025 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Raduly, Csaba
 *
 ******************************************************************************/
module Name
{
modulepar boolean Name_verbose := false;
#define verbose Name_verbose
#include "../macros.ttcnin"

type component alias {}

type record NM {
  charstring r,
  charstring blue,
  charstring black
}
with {
  variant (r)     "name as 'Red'";
  variant (blue)  "name as uppercased";
  variant (black) "name as capitalized";
}

DECLARE_XER_ENCODERS(NM, nm)
DECLARE_EXER_ENCODERS(NM, nm)

const NM outfit := { r := "shirt", blue := "trousers", black := "shoes" }

const universal charstring str_nm_e :=
"<NM>\n" &
"\t<Red>shirt</Red>\n" &
"\t<BLUE>trousers</BLUE>\n" &
"\t<Black>shoes</Black>\n" &
"</NM>\n\n";

const universal charstring str_nm_b :=
"<NM>\n" &
"\t<r>shirt</r>\n" &
"\t<blue>trousers</blue>\n" &
"\t<black>shoes</black>\n" &
"</NM>\n\n";

testcase enc_name() runs on alias
{
  CHECK_METHOD(bxer_enc_nm, outfit, str_nm_b);
  CHECK_METHOD(exer_enc_nm, outfit, str_nm_e);
}

testcase dec_name() runs on alias
{
  CHECK_DECODE(bxer_dec_nm, str_nm_b, NM, outfit);
  CHECK_DECODE(exer_dec_nm, str_nm_e, NM, outfit);
}

/* * * * * * Renaming the embedded type in a record-of  * * * * * * * */

type record ParamType {
  charstring caller
}

type record Call {
  record {
    record of ParamType param_list
  } params optional
}
with {
  variant "name as uncapitalized";
  variant (params.param_list[-]) "name as 'param'";
}

DECLARE_XER_ENCODERS(Call, c);
DECLARE_EXER_ENCODERS(Call, c);

const Call phone_home := {
  params := {
    param_list := {
      {
        caller := "E.T."
      }
    }
  }
}

const universal charstring ET := // for EXER
"<call>\n" &
"\t<params>\n" &
"\t\t<param_list>\n" &
"\t\t\t<param>\n" &
"\t\t\t\t<caller>E.T.</caller>\n" &
"\t\t\t</param>\n" &
"\t\t</param_list>\n" &
"\t</params>\n" &
"</call>\n\n";

const universal charstring BT := // for Basic XER
"<Call>\n" &
"\t<params>\n" &
"\t\t<param_list>\n" &
"\t\t\t<ParamType>\n" &
"\t\t\t\t<caller>E.T.</caller>\n" &
"\t\t\t</ParamType>\n" &
"\t\t</param_list>\n" &
"\t</params>\n" &
"</Call>\n\n";

testcase enc_name_recof_embed() runs on alias
{
  CHECK_METHOD(bxer_enc_c, phone_home, BT);
  CHECK_METHOD(exer_enc_c, phone_home, ET);
}

testcase dec_name_recof_embed() runs on alias
{
  CHECK_DECODE(bxer_dec_c, BT, Call, phone_home);
  CHECK_DECODE(exer_dec_c, ET, Call, phone_home);
}

/* * * * Record of a type with a "name as" (HL14920) * * * */

group HL14920 {

// <element name="foo" type="float"/>
type float Foo
with {
  variant "name as uncapitalized";
  variant "element";
};

//<element name="AllData">
//  <complexType>
//    <sequence>
//      <element ref="ns:foo" minOccurs="0" maxOccurs="unbounded"/>
//    </sequence>
//  </complexType>
//</element>
type record AllData {
  record of Foo foo_list
}
with {
  variant "element";
  variant (foo_list) "untagged";
}

}
with {
  // For HL14920 to fire, AllData.foo_list.<oftype> needs to have
  // a XER attribute. This variant will propagate to it. 
  variant "namespace as 'urn:trouble' prefix 'trbl'"
}

DECLARE_XER_ENCODERS(AllData, ad);
DECLARE_EXER_ENCODERS(AllData, ad);

const AllData everything := {
  { 3.1, 4.1, 5.9, 2.6 }
}

const universal charstring bstr_all :=
"<AllData>\n" &
"\t<foo_list>\n" &
"\t\t<Foo>3.100000</Foo>\n" &
"\t\t<Foo>4.100000</Foo>\n" &
"\t\t<Foo>5.900000</Foo>\n" &
"\t\t<Foo>2.600000</Foo>\n" &
"\t</foo_list>\n" &
"</AllData>\n\n";

const universal charstring estr_all :=
"<trbl:AllData xmlns:trbl='urn:trouble'>\n" &
"\t<trbl:foo>3.100000</trbl:foo>\n" &
"\t<trbl:foo>4.100000</trbl:foo>\n" &
"\t<trbl:foo>5.900000</trbl:foo>\n" &
"\t<trbl:foo>2.600000</trbl:foo>\n" &
"</trbl:AllData>\n\n";


testcase enc_recof_named() runs on alias
{
  CHECK_METHOD(bxer_enc_ad, everything, bstr_all);
  CHECK_METHOD(exer_enc_ad, everything, estr_all);
}

testcase dec_recof_named() runs on alias
{
  CHECK_DECODE(bxer_dec_ad, bstr_all, AllData, everything);
  CHECK_DECODE(exer_dec_ad, estr_all, AllData, everything);
}

control {
  execute(enc_name());
  execute(dec_name());
  
  execute(enc_name_recof_embed());
  execute(dec_name_recof_embed());
  
  execute(enc_recof_named());
  execute(dec_recof_named());
}

}
with {
encode "XML"
}
