• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2022 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 package com.ibm.icu.dev.test.message2;
5 
6 import java.util.Date;
7 import java.util.Locale;
8 import java.util.Map;
9 
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.junit.runners.JUnit4;
13 
14 import com.ibm.icu.dev.test.TestFmwk;
15 import com.ibm.icu.message2.MessageFormatter;
16 import com.ibm.icu.text.MessageFormat;
17 import com.ibm.icu.util.Calendar;
18 import com.ibm.icu.util.GregorianCalendar;
19 
20 /**
21  * Ported the unit tests from {@link com.ibm.icu.text.MessageFormat} to show that they work.
22  *
23  * <p>It does not include all the tests for edge cases and error handling, only the ones that show real functionality.</p>
24  */
25 @RunWith(JUnit4.class)
26 @SuppressWarnings("javadoc")
27 public class Mf2IcuTest extends TestFmwk {
28 
29     @Test
testSample()30     public void testSample() {
31         MessageFormatter form = MessageFormatter.builder()
32                 .setPattern("{There are {$count} files on {$where}}")
33                 .build();
34         assertEquals("format", "There are abc files on def",
35                 form.formatToString(Args.of("count", "abc", "where", "def")));
36     }
37 
38     @Test
testStaticFormat()39     public void testStaticFormat() {
40         Map<String, Object> arguments = Args.of("planet", new Integer(7), "when", new Date(871068000000L), "what",
41                 "a disturbance in the Force");
42 
43         assertEquals("format", "At 12:20:00\u202FPM on Aug 8, 1997, there was a disturbance in the Force on planet 7.",
44                 MessageFormatter.builder()
45                 .setPattern("{At {$when :datetime timestyle=default} on {$when :datetime datestyle=default}, "
46                         + "there was {$what} on planet {$planet :number kind=integer}.}")
47                 .build()
48                 .formatToString(arguments));
49     }
50 
51     static final int FieldPosition_DONT_CARE = -1;
52 
53     @Test
testSimpleFormat()54     public void testSimpleFormat() {
55         Map<String, Object> testArgs1 = Args.of("fileCount", new Integer(0), "diskName", "MyDisk");
56         Map<String, Object> testArgs2 = Args.of("fileCount", new Integer(1), "diskName", "MyDisk");
57         Map<String, Object> testArgs3 = Args.of("fileCount", new Integer(12), "diskName", "MyDisk");
58 
59         MessageFormatter form = MessageFormatter.builder()
60                 .setPattern("{The disk \"{$diskName}\" contains {$fileCount} file(s).}")
61                 .build();
62 
63         assertEquals("format", "The disk \"MyDisk\" contains 0 file(s).", form.formatToString(testArgs1));
64 
65         form.formatToString(testArgs2);
66         assertEquals("format", "The disk \"MyDisk\" contains 1 file(s).", form.formatToString(testArgs2));
67 
68         form.formatToString(testArgs3);
69         assertEquals("format", "The disk \"MyDisk\" contains 12 file(s).", form.formatToString(testArgs3));
70     }
71 
72     @Test
testSelectFormatToPattern()73     public void testSelectFormatToPattern() {
74         String pattern = ""
75                 + "match {$userGender :select}\n"
76                 + "  when female {{$userName} est all\u00E9e \u00E0 Paris.}"
77                 + "  when  *     {{$userName} est all\u00E9 \u00E0 Paris.}"
78                 ;
79 
80             MessageFormatter mf = MessageFormatter.builder()
81                     .setPattern(pattern)
82                     .build();
83             assertEquals("old icu test",
84                     "Charlotte est allée à Paris.",
85                     mf.formatToString(Args.of("userName", "Charlotte", "userGender", "female")));
86             assertEquals("old icu test",
87                     "Guillaume est allé à Paris.",
88                     mf.formatToString(Args.of("userName", "Guillaume", "userGender", "male")));
89             assertEquals("old icu test",
90                     "Dominique est allé à Paris.",
91                     mf.formatToString(Args.of("userName", "Dominique", "userGender", "unnown")));
92     }
93 
doTheRealDateTimeSkeletonTesting(Date date, String messagePattern, Locale locale, String expected)94     private static void doTheRealDateTimeSkeletonTesting(Date date, String messagePattern, Locale locale,
95             String expected) {
96 
97         MessageFormatter msgf = MessageFormatter.builder()
98                 .setPattern(messagePattern).setLocale(locale)
99                 .build();
100         assertEquals(messagePattern, expected, msgf.formatToString(Args.of("when", date)));
101     }
102 
103     @Test
testMessageFormatDateTimeSkeleton()104     public void testMessageFormatDateTimeSkeleton() {
105         Date date = new GregorianCalendar(2021, Calendar.NOVEMBER, 23, 16, 42, 55).getTime();
106 
107         doTheRealDateTimeSkeletonTesting(date, "{{$when :datetime skeleton=MMMMd}}",
108                 Locale.ENGLISH, "November 23");
109         doTheRealDateTimeSkeletonTesting(date, "{{$when :datetime skeleton=yMMMMdjm}}",
110                 Locale.ENGLISH, "November 23, 2021 at 4:42\u202FPM");
111         doTheRealDateTimeSkeletonTesting(date, "{{$when :datetime skeleton=(   yMMMMd   )}}",
112                 Locale.ENGLISH, "November 23, 2021");
113         doTheRealDateTimeSkeletonTesting(date, "{{$when :datetime skeleton=yMMMMd}}",
114                 Locale.FRENCH, "23 novembre 2021");
115         doTheRealDateTimeSkeletonTesting(date, "{Expiration: {$when :datetime skeleton=yMMM}!}",
116                 Locale.ENGLISH, "Expiration: Nov 2021!");
117         doTheRealDateTimeSkeletonTesting(date, "{{$when :datetime pattern=('::'yMMMMd)}}",
118                 Locale.ENGLISH, "::2021November23"); // pattern
119     }
120 
121     @Test
checkMf1Behavior()122     public void checkMf1Behavior() {
123         Date testDate = new Date(1671782400000L); // 2022-12-23
124         Map<String, Object> goodArg = Args.of("user", "John", "today", testDate);
125         Map<String, Object> badArg = Args.of("userX", "John", "todayX", testDate);
126 
127         MessageFormat mf1 = new MessageFormat("Hello {user}, today is {today,date,long}.");
128         assertEquals("old icu test", "Hello {user}, today is {today}.", mf1.format(badArg));
129         assertEquals("old icu test", "Hello John, today is December 23, 2022.", mf1.format(goodArg));
130 
131         MessageFormatter mf2 = MessageFormatter.builder()
132                 .setPattern("{Hello {$user}, today is {$today :datetime datestyle=long}.}")
133                 .build();
134         assertEquals("old icu test", "Hello {$user}, today is {$today}.", mf2.formatToString(badArg));
135         assertEquals("old icu test", "Hello John, today is December 23, 2022.", mf2.formatToString(goodArg));
136     }
137 }
138