mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-25 22:08:39 +00:00
Add enum name lookup method to Java/C# enums
Tested: on Linux for Java and C# Bug: 15781151 Change-Id: I7cb97bcc01d986cac2b24aaf7cb29521ddaa2f6b
This commit is contained in:
@@ -25,6 +25,7 @@ namespace FlatBuffers.Test
|
||||
{
|
||||
CanCreateNewFlatBufferFromScratch();
|
||||
CanReadCppGeneratedWireFile();
|
||||
TestEnums();
|
||||
}
|
||||
|
||||
public void CanCreateNewFlatBufferFromScratch()
|
||||
@@ -141,5 +142,13 @@ namespace FlatBuffers.Test
|
||||
var bb = new ByteBuffer(data);
|
||||
TestBuffer(bb);
|
||||
}
|
||||
|
||||
public void TestEnums()
|
||||
{
|
||||
Assert.AreEqual(Color.Name(Color.Red), "Red");
|
||||
Assert.AreEqual(Color.Name(Color.Blue), "Blue");
|
||||
Assert.AreEqual(Any.Name(Any.NONE), "NONE");
|
||||
Assert.AreEqual(Any.Name(Any.Monster), "Monster");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,9 +106,18 @@ class JavaTest {
|
||||
// (see Table.__string).
|
||||
TestBuffer(fbb.dataBuffer().asReadOnlyBuffer());
|
||||
|
||||
TestEnums();
|
||||
|
||||
System.out.println("FlatBuffers test: completed successfully");
|
||||
}
|
||||
|
||||
static void TestEnums() {
|
||||
TestEq(Color.name(Color.Red), "Red");
|
||||
TestEq(Color.name(Color.Blue), "Blue");
|
||||
TestEq(Any.name(Any.NONE), "NONE");
|
||||
TestEq(Any.name(Any.Monster), "Monster");
|
||||
}
|
||||
|
||||
static void TestBuffer(ByteBuffer bb) {
|
||||
TestEq(Monster.MonsterBufferHasIdentifier(bb), true);
|
||||
|
||||
|
||||
@@ -5,8 +5,12 @@ namespace MyGame.Example
|
||||
|
||||
public class Any
|
||||
{
|
||||
public static byte NONE = 0;
|
||||
public static byte Monster = 1;
|
||||
public static readonly byte NONE = 0;
|
||||
public static readonly byte Monster = 1;
|
||||
|
||||
private static readonly string[] names = { "NONE", "Monster", };
|
||||
|
||||
public static string Name(int e) { return names[e]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
4
tests/MyGame/Example/Any.java
Executable file → Normal file
4
tests/MyGame/Example/Any.java
Executable file → Normal file
@@ -5,5 +5,9 @@ package MyGame.Example;
|
||||
public class Any {
|
||||
public static final byte NONE = 0;
|
||||
public static final byte Monster = 1;
|
||||
|
||||
private static final String[] names = { "NONE", "Monster", };
|
||||
|
||||
public static String name(int e) { return names[e]; }
|
||||
};
|
||||
|
||||
|
||||
@@ -5,9 +5,13 @@ namespace MyGame.Example
|
||||
|
||||
public class Color
|
||||
{
|
||||
public static sbyte Red = 1;
|
||||
public static sbyte Green = 2;
|
||||
public static sbyte Blue = 8;
|
||||
public static readonly sbyte Red = 1;
|
||||
public static readonly sbyte Green = 2;
|
||||
public static readonly sbyte Blue = 8;
|
||||
|
||||
private static readonly string[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
|
||||
|
||||
public static string Name(int e) { return names[e - Red]; }
|
||||
};
|
||||
|
||||
|
||||
|
||||
4
tests/MyGame/Example/Color.java
Executable file → Normal file
4
tests/MyGame/Example/Color.java
Executable file → Normal file
@@ -6,5 +6,9 @@ public class Color {
|
||||
public static final byte Red = 1;
|
||||
public static final byte Green = 2;
|
||||
public static final byte Blue = 8;
|
||||
|
||||
private static final String[] names = { "Red", "Green", "", "", "", "", "", "Blue", };
|
||||
|
||||
public static String name(int e) { return names[e - Red]; }
|
||||
};
|
||||
|
||||
|
||||
0
tests/MyGame/Example/Monster.java
Executable file → Normal file
0
tests/MyGame/Example/Monster.java
Executable file → Normal file
0
tests/MyGame/Example/Test.java
Executable file → Normal file
0
tests/MyGame/Example/Test.java
Executable file → Normal file
0
tests/MyGame/Example/Vec3.java
Executable file → Normal file
0
tests/MyGame/Example/Vec3.java
Executable file → Normal file
Reference in New Issue
Block a user