1559a8a2b4a48b193efb47f290f12d044dfdc4fb
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / YT1437Test.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
18
19 public class YT1437Test {
20     @Test
21     public void testDecimalFractionDigits() {
22         final var module = YangParserTestUtils.parseYang("""
23             module yt1437 {
24               namespace yt1437;
25               prefix yt1437;
26
27               leaf foo {
28                 type decimal64 {
29                   fraction-digits 2;
30                   range "20.0..30.01 | 50";
31                 }
32               }
33             }""").findModule("yt1437").orElseThrow();
34         final var foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo")).orElseThrow();
35         assertThat(foo, instanceOf(LeafSchemaNode.class));
36
37         final TypeDefinitionAwareCodec<?, ?> codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
38         assertThat(codec, instanceOf(DecimalStringCodec.class));
39         final var cast = (DecimalStringCodec) codec;
40
41         final var one = cast.deserialize("20.0");
42         assertEquals(2, one.scale());
43         assertEquals("20.0", cast.serialize(one));
44
45         final var two = cast.deserialize("20.00");
46         assertEquals(2, two.scale());
47         assertEquals("20.0", cast.serialize(two));
48
49         final var three = cast.deserialize("20.000");
50         assertEquals(2, three.scale());
51         assertEquals("20.0", cast.serialize(three));
52     }
53 }