BiFunctionConvertTest

This commit is contained in:
redkale
2024-05-27 21:06:08 +08:00
parent f5479e13db
commit 0ce73064f5

View File

@@ -1,78 +1,79 @@
///* /*
// * To change this license header, choose License Headers in Project Properties. * To change this license header, choose License Headers in Project Properties.
// * To change this template file, choose Tools | Templates * To change this template file, choose Tools | Templates
// * and open the template in the editor. * and open the template in the editor.
// */ */
//package org.redkale.test.convert; package org.redkale.test.convert;
//
//import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
//import org.redkale.convert.ConvertField; import org.redkale.convert.ConvertField;
//import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
//import org.redkale.util.Attribute; import org.redkale.util.Attribute;
//
///* /*
// * test * test
// * *
// * @author zhangjx * @author zhangjx
// */ */
//public class BiFunctionConvertTest { public class BiFunctionConvertTest {
//
// @Test public static class GamePlayer {
// public void run() throws Throwable {
// GamePlayer player1 = new GamePlayer(); public int userid;
// player1.userid = 1;
// player1.username = "玩家1"; public String username;
// player1.cards = new int[] {11, 12, 13, 14, 15};
// GamePlayer player2 = new GamePlayer(); public int[] cards;
// player2.userid = 2; }
// player2.username = "玩家2";
// player2.cards = new int[] {21, 22, 23, 24, 25}; public static class GameTable {
// GamePlayer player3 = new GamePlayer();
// player3.userid = 3; public int tableid;
// player3.username = "玩家3";
// player3.cards = new int[] {31, 32, 33, 34, 35}; public GamePlayer[] players;
// GameTable table = new GameTable(); }
// table.tableid = 100;
// table.players = new GamePlayer[] {player1, player2, player3}; @Test
// JsonConvert convert1 = JsonConvert.root(); public void run() throws Throwable {
// System.out.println(convert1.convertTo(table)); GamePlayer player1 = new GamePlayer();
// JsonConvert convert2 = convert1.newConvert( player1.userid = 1;
// (Attribute t, Object u) -> { player1.username = "玩家1";
// if (t.field().equals("cards") && u instanceof GamePlayer) { player1.cards = new int[] {11, 12, 13, 14, 15};
// int userid = ((GamePlayer) u).userid; GamePlayer player2 = new GamePlayer();
// if (userid == 3) return null; // 玩家3的cards不输出 player2.userid = 2;
// return t.get(u); player2.username = "玩家2";
// } player2.cards = new int[] {21, 22, 23, 24, 25};
// return t.get(u); GamePlayer player3 = new GamePlayer();
// }, player3.userid = 3;
// (Object u) -> { player3.username = "玩家3";
// if (table != u) return null; player3.cards = new int[] {31, 32, 33, 34, 35};
// // return new ConvertField[]{new ConvertField("extcol1", 30), new ConvertField("extcol2", "扩展字段值")}; GameTable table = new GameTable();
// return ConvertField.ofArray("extcol1", 30, "extcol2", "扩展字段值"); table.tableid = 100;
// }); table.players = new GamePlayer[] {player1, player2, player3};
// System.out.println(convert2.convertTo(table)); JsonConvert convert1 = JsonConvert.root();
// Assertions.assertEquals( System.out.println(convert1.convertTo(table));
// "{\"players\":[{\"cards\":[11,12,13,14,15],\"userid\":1,\"username\":\"玩家1\"},{\"cards\":[21,22,23,24,25],\"userid\":2,\"username\":\"玩家2\"},{\"userid\":3,\"username\":\"玩家3\"}],\"tableid\":100,\"extcol1\":30,\"extcol2\":\"扩展字段值\"}", JsonConvert convert2 = convert1.newConvert(
// convert2.convertTo(table)); (Attribute t, Object u) -> {
// // {"players":[{"cards":[11,12,13,14,15],"userid":1,"username":"玩家1"},{"cards":[21,22,23,24,25],"userid":2,"username":"玩家2"},{"cards":[31,32,33,34,35],"userid":3,"username":"玩家3"}],"tableid":100} if (t.field().equals("cards") && u instanceof GamePlayer) {
// // {"players":[{"cards":[11,12,13,14,15],"userid":1,"username":"玩家1"},{"cards":[21,22,23,24,25],"userid":2,"username":"玩家2"},{"userid":3,"username":"玩家3"}],"tableid":100,"extcol1":30,"extcol2":"扩展字段值"} int userid = ((GamePlayer) u).userid;
// } if (userid == 3) return null; // 玩家3的cards不输出
// return t.get(u);
// public static class GamePlayer { }
// return t.get(u);
// public int userid; },
// (Object u) -> {
// public String username; if (table != u) return null;
// // return new ConvertField[]{new ConvertField("extcol1", 30),
// public int[] cards; // new ConvertField("extcol2", "扩展字段值")};
// } return ConvertField.ofArray("extcol1", 30, "extcol2", "扩展字段值");
// });
// public static class GameTable { System.out.println(convert2.convertTo(table));
// Assertions.assertEquals(
// public int tableid; "{\"players\":[{\"cards\":[11,12,13,14,15],\"userid\":1,\"username\":\"玩家1\"},"
// + "{\"cards\":[21,22,23,24,25],\"userid\":2,\"username\":\"玩家2\"},"
// public GamePlayer[] players; + "{\"userid\":3,\"username\":\"玩家3\"}],"
// } + "\"tableid\":100,\"extcol1\":30,\"extcol2\":\"扩展字段值\"}",
// convert2.convertTo(table));
//} }
}