/******************************************************************************
 * 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
 *   Baranyi, Botond
 *   Raduly, Csaba
 *
 ******************************************************************************/
module UseType{
modulepar boolean UseType_verbose := false;
#define verbose UseType_verbose
#include "../macros.ttcnin"

type component Shop {}

type record Shirt {
  charstring color,
  charstring make,
  integer    size
}

type record Trousers {
  boolean available,
  charstring color,
  charstring make
}
with {
  variant (available) "attribute"
}

type record Shoes {
  boolean available,
  charstring color,
  integer    size
}
with {
  variant (available) "attribute"
}

type union ProductUT {
  Shirt shirt,
  Trousers trousers,
  Shoes shoes
}
with {
  variant "element";
  variant "useType";
  variant "namespace as 'http://www.example.com' prefix 'exm'"
}

DECLARE_XER_ENCODERS(ProductUT, prodUT);
DECLARE_EXER_ENCODERS(ProductUT, prodUT);

const ProductUT shoo := { shoes := { available := false, color := "brown", size := 42 } }

const universal charstring str_shoo_e :=
"<exm:ProductUT xmlns:exm=\'http://www.example.com\' " &
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type=\'shoes\' available='false'>\n" &
"\t<color>brown</color>\n" &
"\t<size>42</size>\n" &
"</exm:ProductUT>\n" &
"\n";

const universal charstring str_shoo_b :=
"<ProductUT>\n" &
"\t<shoes>\n" &
"\t\t<available><false/></available>\n" &
"\t\t<color>brown</color>\n" &
"\t\t<size>42</size>\n" &
"\t</shoes>\n" &
"</ProductUT>\n" &
"\n";

testcase enc_ut() runs on Shop {
  CHECK_METHOD(bxer_enc_prodUT, shoo, str_shoo_b);
  CHECK_METHOD(exer_enc_prodUT, shoo, str_shoo_e);
}

testcase dec_ut() runs on Shop
{
  CHECK_DECODE(bxer_dec_prodUT, str_shoo_b, ProductUT, shoo);
  CHECK_DECODE(exer_dec_prodUT, str_shoo_e, ProductUT, shoo);
}

/* * * An escapee from Name * * */

type union U {
  boolean first,
  integer second
}
with {
  variant "useType";
  variant (first) "name  as 'One'";
  variant (second) "name as 'Two'";
}

DECLARE_XER_ENCODERS(U, u);
DECLARE_EXER_ENCODERS(U, u);

const U v := { second := 123 }

const universal charstring str_12_e :=
"<U xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' xsi:type=\'Two\'>123</U>\n\n";

const universal charstring str_12_b :=
"<U>\n" &
"\t<second>123</second>\n" &
"</U>\n\n";



testcase encode_12() runs on Shop
{
  CHECK_METHOD(bxer_enc_u, v, str_12_b);
  CHECK_METHOD(exer_enc_u, v, str_12_e);
}

testcase decode_12() runs on Shop
{
  CHECK_DECODE(bxer_dec_u, str_12_b, U, v);
  CHECK_DECODE(exer_dec_u, str_12_e, U, v);
}

group QualifiedUseType {

type union Number {
  integer i,
  float f,
  octetstring os
}
with {
  variant "useType";
  variant "name as uncapitalized";
  variant(i) "name as 'integer'";
  variant(f) "name as 'real'";
  variant(os) "name as 'hexadecimal'";
}

type record of Number Data
with {
  variant "name as uncapitalized";
}

} // group QualifiedUseType
with {
  variant "elementFormQualified";
  variant "namespace as 'http://www.example.org/Number' prefix 'nr'";
}

DECLARE_EXER_ENCODERS(Data, data);

const Data c_data := { { i := 10 }, { f := 3.3 }, { os := '5DA8'O } };

const universal charstring str_data :=
"<nr:data xmlns:nr='http://www.example.org/Number' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n" &
"\t<nr:number>10</nr:number>\n" &
"\t<nr:number xsi:type='nr:real'>3.300000</nr:number>\n" &
"\t<nr:number xsi:type='nr:hexadecimal'>5DA8</nr:number>\n" &
"</nr:data>\n\n";

testcase encode_qualified() runs on Shop
{
  CHECK_METHOD(exer_enc_data, c_data, str_data);
}

testcase decode_qualified() runs on Shop
{
  CHECK_DECODE(exer_dec_data, str_data, Data, c_data);
}

control {
  execute(enc_ut());
  execute(dec_ut());

  execute(encode_12());
  execute(decode_12());
  
  execute(encode_qualified());
  execute(decode_qualified());
}

}
with {
encode "XML"
variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'"
}

