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:
Gabriel Martinez
2014-09-18 12:28:10 -07:00
parent d6ed127cf4
commit 048638a971
10 changed files with 75 additions and 7 deletions

View File

@@ -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");
}
}
}

View File

@@ -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);

View File

@@ -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
View 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]; }
};

View File

@@ -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
View 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
View File

0
tests/MyGame/Example/Test.java Executable file → Normal file
View File

0
tests/MyGame/Example/Vec3.java Executable file → Normal file
View File