|
| 1 | +package com.genexus; |
| 2 | + |
| 3 | +import com.genexus.specific.java.Connect; |
| 4 | +import com.genexus.specific.java.LogManager; |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +public class TestCommonUtil { |
| 9 | + |
| 10 | + private void initialize() |
| 11 | + { |
| 12 | + Connect.init(); |
| 13 | + LogManager.initialize("."); |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + public void testFormat() { |
| 18 | + initialize(); |
| 19 | + |
| 20 | + // Test case 1: Pass in a string with no parameter markers |
| 21 | + String value = "Hello world"; |
| 22 | + String expectedResult = "Hello world"; |
| 23 | + String result = CommonUtil.format(value, "", "", "", "", "", "", "", "", ""); |
| 24 | + Assert.assertEquals(expectedResult, result); |
| 25 | + |
| 26 | + // Test case 2: Pass in a string with maximum parameter markers |
| 27 | + value = "This is a string with %1, %2, %3, %4, %5, %6, %7, %8, and %9"; |
| 28 | + expectedResult = "This is a string with 1, 2, 3, 4, 5, 6, 7, 8, and 9"; |
| 29 | + result = CommonUtil.format(value, "1", "2", "3", "4", "5", "6", "7", "8", "9"); |
| 30 | + Assert.assertEquals(expectedResult, result); |
| 31 | + |
| 32 | + // Test case 3: Pass in a string with one parameter marker that's also the end of the string |
| 33 | + value = "Hello %1"; |
| 34 | + expectedResult = "Hello world"; |
| 35 | + result = CommonUtil.format(value, "world", "", "", "", "", "", "", "", ""); |
| 36 | + Assert.assertEquals(expectedResult, result); |
| 37 | + |
| 38 | + // Test case 4: Pass in a string with one parameter marker that's not the end of the string |
| 39 | + value = "Alex is %1 years old"; |
| 40 | + expectedResult = "Alex is 26 years old"; |
| 41 | + result = CommonUtil.format(value, "26", "", "", "", "", "", "", "", ""); |
| 42 | + Assert.assertEquals(expectedResult, result); |
| 43 | + |
| 44 | + // Test case 5: Pass in a string with multiple parameter markers |
| 45 | + value = "%1 is %2 years old"; |
| 46 | + expectedResult = "Alex is 26 years old"; |
| 47 | + result = CommonUtil.format(value, "Alex", "26", "", "", "", "", "", "", ""); |
| 48 | + Assert.assertEquals(expectedResult, result); |
| 49 | + |
| 50 | + // Test case 6: Pass in an empty string |
| 51 | + value = ""; |
| 52 | + expectedResult = ""; |
| 53 | + result = CommonUtil.format(value, "1", "2", "3", "4", "5", "6", "7", "8", "9"); |
| 54 | + Assert.assertEquals(expectedResult, result); |
| 55 | + |
| 56 | + // Test case 7: Pass in a string containing the "/%" sequence |
| 57 | + value = "%1/%2/%3"; |
| 58 | + expectedResult = "2022/12/31"; |
| 59 | + result = CommonUtil.format(value, "2022","12","31", "", "", "", "", "", ""); |
| 60 | + Assert.assertEquals(expectedResult, result); |
| 61 | + |
| 62 | + // Test case 8: Pass in a string containing one parameter marker followed by a % sign at the end of the string |
| 63 | + value = "%1%"; |
| 64 | + expectedResult = "10%"; |
| 65 | + result = CommonUtil.format(value, "10","","", "", "", "", "", "", ""); |
| 66 | + Assert.assertEquals(expectedResult, result); |
| 67 | + |
| 68 | + // Test case 9: Pass in a string containing one parameter marker followed by a % sign that's not at the end of the string |
| 69 | + value = "The price is %1% off today"; |
| 70 | + expectedResult = "The price is 10% off today"; |
| 71 | + result = CommonUtil.format(value, "10","","", "", "", "", "", "", ""); |
| 72 | + Assert.assertEquals(expectedResult, result); |
| 73 | + |
| 74 | + // Test case 9: Pass in a string containing multiple parameter markers separated by a % sign that also are at the end of the string |
| 75 | + value = "Your discount code is %1%%2"; |
| 76 | + expectedResult = "Your discount code is 10%10"; |
| 77 | + result = CommonUtil.format(value, "10","10","", "", "", "", "", "", ""); |
| 78 | + Assert.assertEquals(expectedResult, result); |
| 79 | + |
| 80 | + // Test case 10: Pass in a string containing multiple parameter markers separated by a % sign that are not at the end of the string |
| 81 | + value = "sample text %1%%2%%3%%4%%5 sample text"; |
| 82 | + expectedResult = "sample text 10%10%10%10%10 sample text"; |
| 83 | + result = CommonUtil.format(value, "10","10","10", "10", "10", "", "", "", ""); |
| 84 | + Assert.assertEquals(expectedResult, result); |
| 85 | + |
| 86 | + // Test case 11: Pass in a string with a parameter marker that should be ignored |
| 87 | + value = "sample text \\%1 sample text"; |
| 88 | + expectedResult = "sample text %1 sample text"; |
| 89 | + result = CommonUtil.format(value, "10","","", "", "", "", "", "", ""); |
| 90 | + Assert.assertEquals(expectedResult, result); |
| 91 | + |
| 92 | + // Test case 12: Pass in a string with appended parameter markers at the end of the string |
| 93 | + value = "Your discount code is %1%2"; |
| 94 | + expectedResult = "Your discount code is 1020"; |
| 95 | + result = CommonUtil.format(value, "10","20","", "", "", "", "", "", ""); |
| 96 | + Assert.assertEquals(expectedResult, result); |
| 97 | + |
| 98 | + // Test case 13: Pass in a string with appended parameter markers at the middle of the string |
| 99 | + value = "Your discount code is %1%2, use it within 24 hours"; |
| 100 | + expectedResult = "Your discount code is 1020, use it within 24 hours"; |
| 101 | + result = CommonUtil.format(value, "10","20","", "", "", "", "", "", ""); |
| 102 | + Assert.assertEquals(expectedResult, result); |
| 103 | + |
| 104 | + // Test case 14: Pass in a string with appended parameter markers at the start of the string |
| 105 | + value = "%1%2 is your discount code"; |
| 106 | + expectedResult = "1020 is your discount code"; |
| 107 | + result = CommonUtil.format(value, "10","20","", "", "", "", "", "", ""); |
| 108 | + Assert.assertEquals(expectedResult, result); |
| 109 | + |
| 110 | + // Test case 15: Pass in more parameter markers than needed |
| 111 | + value = "%1 is %2 years old"; |
| 112 | + expectedResult = "Alex is 26 years old"; |
| 113 | + result = CommonUtil.format(value, "Alex", "26", "10", "10", "", "", "", "", ""); |
| 114 | + Assert.assertEquals(expectedResult, result); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments