|
| 1 | +package com.readdle.swiftjava.sample; |
| 2 | + |
| 3 | +import android.support.test.runner.AndroidJUnit4; |
| 4 | + |
| 5 | +import com.readdle.codegen.anotation.JavaSwift; |
| 6 | +import com.readdle.codegen.anotation.SwiftRuntimeError; |
| 7 | + |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Before; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | + |
| 13 | +@RunWith(AndroidJUnit4.class) |
| 14 | +public class StringTests { |
| 15 | + |
| 16 | + @Before |
| 17 | + public void setUp() { |
| 18 | + System.loadLibrary("SampleAppCore"); |
| 19 | + JavaSwift.init(); |
| 20 | + SwiftEnvironment.initEnvironment(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testZero() { |
| 25 | + Assert.assertEquals(StringTest.testZero(), ""); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testParam() { |
| 30 | + Assert.assertTrue(StringTest.testParam("")); |
| 31 | + Assert.assertFalse(StringTest.testParam("42")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testReturnType() { |
| 36 | + Assert.assertEquals(StringTest.testReturnType(), ""); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testOptionalParam() { |
| 41 | + Assert.assertTrue(StringTest.testOptionalParam("")); |
| 42 | + Assert.assertFalse(StringTest.testOptionalParam(null)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testOptionalReturnType() { |
| 47 | + try { |
| 48 | + StringTest.testOptionalReturnType(); |
| 49 | + } |
| 50 | + catch (Exception e) { |
| 51 | + Assert.assertTrue(e instanceof SwiftRuntimeError); |
| 52 | + Assert.assertEquals(e.getMessage(), "Invalid value \"" + Long.MAX_VALUE + "\": Not enough bits to represent Int []"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testProtocolParam() { |
| 58 | + boolean result = StringTest.testProtocolParam(param -> param.equals("")); |
| 59 | + Assert.assertTrue(result); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testProtocolReturnType() { |
| 64 | + String result = StringTest.testProtocolReturnType(() -> "42"); |
| 65 | + Assert.assertEquals(result, "42"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testProtocolOptionalParam() { |
| 70 | + boolean result = StringTest.testProtocolOptionalParam(param -> param != null && param.equals("")); |
| 71 | + Assert.assertTrue(result); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testProtocolOptionalReturnType() { |
| 76 | + String result = StringTest.testProtocolOptionalReturnType(() -> "42"); |
| 77 | + Assert.assertNotNull(result); |
| 78 | + Assert.assertEquals(result, "42"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testEncode() { |
| 83 | + StringTestStruct result = StringTest.testEncode(); |
| 84 | + Assert.assertEquals(result, new StringTestStruct()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testDecode() { |
| 89 | + StringTestStruct goodParam = new StringTestStruct(); |
| 90 | + StringTestStruct badParam = new StringTestStruct("42", "42", "42"); |
| 91 | + Assert.assertTrue(StringTest.testDecode(goodParam)); |
| 92 | + Assert.assertFalse(StringTest.testDecode(badParam)); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testEnumEncode() { |
| 97 | + Assert.assertEquals(StringEnum.ONE, StringTest.testEnumEncode(StringEnum.ONE.getRawValue())); |
| 98 | + Assert.assertEquals(StringEnum.TWO, StringTest.testEnumEncode(StringEnum.TWO.getRawValue())); |
| 99 | + Assert.assertEquals(StringEnum.THREE, StringTest.testEnumEncode(StringEnum.THREE.getRawValue())); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testEnumDecode() { |
| 104 | + Assert.assertEquals(StringEnum.ONE.getRawValue(), StringTest.testEnumDecode(StringEnum.ONE)); |
| 105 | + Assert.assertEquals(StringEnum.TWO.getRawValue(), StringTest.testEnumDecode(StringEnum.TWO)); |
| 106 | + Assert.assertEquals(StringEnum.THREE.getRawValue(), StringTest.testEnumDecode(StringEnum.THREE)); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testBlock() { |
| 111 | + Assert.assertTrue(StringTest.testBlock(value -> value)); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testOptionalBlock() { |
| 116 | + Assert.assertTrue(StringTest.testOptionalBlock(value -> value)); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments