1 package org.unicode.cldr.api; 2 3 import com.google.common.collect.ImmutableMap; 4 import com.ibm.icu.dev.test.TestFmwk; 5 6 /** 7 * Tests for the core CLDR value representation. Since this is an immutable value type, the tests 8 * largely focus on things like parsing and validity checking. 9 */ 10 public class CldrValueTest extends TestFmwk { 11 TestNoValueAttributes()12 public void TestNoValueAttributes() { 13 String dpath = "//ldml/localeDisplayNames/territories/territory[@type=\"US\"]"; 14 CldrValue value = CldrValue.parseValue(dpath, "Hello World"); 15 assertEquals("value", "Hello World", value.getValue()); 16 assertTrue("no attributes", value.getValueAttributes().isEmpty()); 17 assertEquals("path", dpath, value.getPath().toString()); 18 } 19 TestValueAttributes()20 public void TestValueAttributes() { 21 CldrValue v = CldrValue.parseValue("//supplementalData/info[@digits=\"2\"][@iso4217=\"AMD\"]" 22 + "[@cashRounding=\"0\"][@rounding=\"0\"][@cashDigits=\"0\"]", ""); 23 assertEquals("value", "", v.getValue()); 24 // The map contains only the value attributes (no "iso4217") and in DTD order. 25 ImmutableMap<AttributeKey, String> expected = ImmutableMap.of( 26 AttributeKey.keyOf("info", "digits"), "2", 27 AttributeKey.keyOf("info", "rounding"), "0", 28 AttributeKey.keyOf("info", "cashDigits"), "0", 29 AttributeKey.keyOf("info", "cashRounding"), "0"); 30 assertEquals("attributes", expected, v.getValueAttributes()); 31 // Attribute order is normalized to DTD order. 32 assertEquals("attribute order", 33 expected.keySet().asList(), v.getValueAttributes().keySet().asList()); 34 } 35 TestRetainExplicitNonDefault()36 public void TestRetainExplicitNonDefault() { 37 // The "deprecated" attribute has a default value of "false" and should be ignored, even if 38 // explicitly present. 39 CldrValue v = CldrValue.parseValue( 40 "//ldmlBCP47/keyword" 41 + "/key[@name=\"cu\"][@description=\"Currency type key\"][@alias=\"currency\"]" 42 + "/type[@name=\"xau\"][@description=\"Gold\"][@deprecated=\"true\"]", 43 ""); 44 assertEquals("path string", 45 "//ldmlBCP47/keyword/key[@name=\"cu\"]/type[@name=\"xau\"]", v.getPath().toString()); 46 AttributeKey key = AttributeKey.keyOf("type", "deprecated"); 47 assertTrue("deprecated", v.getValueAttributes().containsKey(key)); 48 } 49 TestValueEquality()50 public void TestValueEquality() { 51 CldrValue v = CldrValue.parseValue( 52 "//supplementalData/info[@iso4217=\"AMD\"][@digits=\"2\"]" 53 + "[@rounding=\"0\"][@cashDigits=\"0\"][@cashRounding=\"0\"]", "value"); 54 // Same value, but attributes in a different order in the string (does not matter). 55 CldrValue vDiffOrder = CldrValue.parseValue( 56 "//supplementalData/info[@iso4217=\"AMD\"][@cashRounding=\"0\"]" 57 + "[@cashDigits=\"0\"][@rounding=\"0\"][@digits=\"2\"]", "value"); 58 // Different value does matter. 59 CldrValue vDiffValue = CldrValue.parseValue( 60 "//supplementalData/info[@iso4217=\"AMD\"][@digits=\"2\"]" 61 + "[@rounding=\"0\"][@cashDigits=\"0\"][@cashRounding=\"0\"]", "different"); 62 // Different "value" attribute value does matter. 63 CldrValue vDiffAttr = CldrValue.parseValue( 64 "//supplementalData/info[@iso4217=\"AMD\"][@digits=\"3\"]" 65 + "[@rounding=\"0\"][@cashDigits=\"0\"][@cashRounding=\"0\"]", "value"); 66 // Differing "distinguishing" attribute does matter (that's part of the CldrPath). 67 CldrValue vSameDiffPath = CldrValue.parseValue( 68 "//supplementalData/info[@iso4217=\"XXX\"][@digits=\"2\"]" 69 + "[@rounding=\"0\"][@cashDigits=\"0\"][@cashRounding=\"0\"]", "value"); 70 assertEquals("equal instances", v, vDiffOrder); 71 assertNotEquals("unequal path", v, vSameDiffPath); 72 assertNotEquals("unequal values", v, vDiffValue); 73 assertNotEquals("unequal attributes", v, vDiffAttr); 74 75 // Also do some hashcode checking (not all combinations). 76 assertEquals("equal hashcode", v.hashCode(), vDiffOrder.hashCode()); 77 // Clash is technically possible but effectively impossible. 78 assertNotEquals("unequal hashcode", v.hashCode(), vDiffValue.hashCode()); 79 assertNotEquals("unequal hashcode", v.hashCode(), vDiffAttr.hashCode()); 80 } 81 } 82