|
| 1 | +package com.stripe.util; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import lombok.Data; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +public class StringUtilsTest { |
| 12 | + @Test |
| 13 | + public void testContainsWhitespace() { |
| 14 | + @Data |
| 15 | + class TestCase { |
| 16 | + private final String data; |
| 17 | + private final Boolean want; |
| 18 | + } |
| 19 | + |
| 20 | + List<TestCase> testCases = |
| 21 | + new ArrayList<TestCase>() { |
| 22 | + private static final long serialVersionUID = 1L; |
| 23 | + |
| 24 | + { |
| 25 | + add(new TestCase("sk_test_123", false)); |
| 26 | + add(new TestCase("sk_test_4eC39HqLyjWDarjtT1zdp7dc", false)); |
| 27 | + add(new TestCase("abc", false)); |
| 28 | + add(new TestCase("sk-test-123", false)); |
| 29 | + add(new TestCase("", false)); |
| 30 | + add(new TestCase("sk_test_123\n", true)); |
| 31 | + add(new TestCase("\nsk_test_123", true)); |
| 32 | + add(new TestCase("sk_test_\n123", true)); |
| 33 | + add(new TestCase("sk_test_123 ", true)); |
| 34 | + add(new TestCase(" sk_test_123", true)); |
| 35 | + add(new TestCase("sk_test_ 123", true)); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + for (TestCase testCase : testCases) { |
| 40 | + assertTrue( |
| 41 | + testCase.getWant() == StringUtils.containsWhitespace(testCase.getData()), |
| 42 | + String.format( |
| 43 | + "Expected containsWhitespace(\"%s\") to be %s", |
| 44 | + testCase.getData(), testCase.getWant() ? "true" : "false")); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testSecureCompare() { |
| 50 | + @Data |
| 51 | + class TestCase { |
| 52 | + private final String[] data; |
| 53 | + private final Boolean want; |
| 54 | + } |
| 55 | + |
| 56 | + List<TestCase> testCases = |
| 57 | + new ArrayList<TestCase>() { |
| 58 | + private static final long serialVersionUID = 1L; |
| 59 | + |
| 60 | + { |
| 61 | + add(new TestCase(new String[] {"Hello", "Hello"}, true)); |
| 62 | + add(new TestCase(new String[] {"Hello", "hello"}, false)); |
| 63 | + add(new TestCase(new String[] {"Hello", "Helloo"}, false)); |
| 64 | + add(new TestCase(new String[] {"Hello", "Hell"}, false)); |
| 65 | + add(new TestCase(new String[] {"Hello", ""}, false)); |
| 66 | + add(new TestCase(new String[] {"", "Hello"}, false)); |
| 67 | + add(new TestCase(new String[] {"", ""}, true)); |
| 68 | + add(new TestCase(new String[] {"\0AAAAAAAAA", "\0BBBBBBBBBBBB"}, false)); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + for (TestCase testCase : testCases) { |
| 73 | + assertTrue( |
| 74 | + testCase.getWant() |
| 75 | + == StringUtils.secureCompare(testCase.getData()[0], testCase.getData()[1]), |
| 76 | + String.format( |
| 77 | + "Expected secureCompare(\"%s\", \"%s\") to be %s", |
| 78 | + testCase.getData()[0], testCase.getData()[1], testCase.getWant() ? "true" : "false")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testToSnakeCase() { |
| 84 | + @Data |
| 85 | + class TestCase { |
| 86 | + private final String data; |
| 87 | + private final String want; |
| 88 | + } |
| 89 | + |
| 90 | + List<TestCase> testCases = |
| 91 | + new ArrayList<TestCase>() { |
| 92 | + private static final long serialVersionUID = 1L; |
| 93 | + |
| 94 | + { |
| 95 | + add(new TestCase("Foo", "foo")); |
| 96 | + add(new TestCase("FooBar", "foo_bar")); |
| 97 | + add(new TestCase("FooBar123", "foo_bar123")); |
| 98 | + add(new TestCase("Foo123Bar", "foo123_bar")); |
| 99 | + add(new TestCase("FOOBar", "foo_bar")); |
| 100 | + add(new TestCase("FooBAR", "foo_bar")); |
| 101 | + add(new TestCase("FOOBAR", "foobar")); |
| 102 | + add(new TestCase("FOO_BAR", "foo_bar")); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + for (TestCase testCase : testCases) { |
| 107 | + assertEquals(testCase.getWant(), StringUtils.toSnakeCase(testCase.getData())); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments