z452_dflts_exec_tst.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2017 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx.gfml; import gplx.*;
  13. import org.junit.*;
  14. public class z452_dflts_exec_tst {
  15. @Before public void setup() {
  16. type = make_("gfml.point", "point", "x");
  17. } GfmlDefaultItem item; GfmlType type; GfmlDefaultItem_fxt fx_item = GfmlDefaultItem_fxt.new_();
  18. @Test public void Basic() {
  19. tst_SubFldDefault(type, "x", null);
  20. item = fx_item.make_("gfml.point", "x", "10");
  21. item.Exec_bgn(type);
  22. tst_SubFldDefault(type, "x", "10");
  23. item.Exec_end(type);
  24. tst_SubFldDefault(type, "x", null);
  25. }
  26. @Test public void Overwrite() {
  27. ini_SubFldDefault_add(type, "x", "0");
  28. tst_SubFldDefault(type, "x", "0");
  29. item = fx_item.make_("gfml.point", "x", "10");
  30. item.Exec_bgn(type);
  31. tst_SubFldDefault(type, "x", "10");
  32. item.Exec_end(type);
  33. tst_SubFldDefault(type, "x", "0");
  34. }
  35. @Test public void CreateDefault() {
  36. tst_SubFldExists(type, "y", false);
  37. item = fx_item.make_("gfml.point", "y", "10");
  38. item.Exec_bgn(type);
  39. tst_SubFldDefault(type, "y", "10");
  40. item.Exec_end(type);
  41. tst_SubFldExists(type, "y", false);
  42. }
  43. @Test public void DefaultTkn() {
  44. Object[] ary = ini_eval_("0");
  45. GfmlTkn varTkn = (GfmlTkn)ary[0];
  46. GfmlVarItm varItem = (GfmlVarItm)ary[1];
  47. item = fx_item.make_("gfml.point", "x", varTkn);
  48. varItem.Tkn_set(GfmlTkn_.raw_("10"));
  49. item.Exec_bgn(type);
  50. tst_SubFldDefault(type, "x", "10");
  51. }
  52. Object[] ini_eval_(String text) {
  53. GfmlVarCtx evalContext = GfmlVarCtx.new_("testContext");
  54. GfmlTkn valTkn = GfmlTkn_.raw_(text);
  55. GfmlVarItm varItem = GfmlVarItm.new_("varKey", valTkn, "testContext");
  56. evalContext.Add_if_dupe_use_nth(varItem);
  57. GfmlVarTkn rv = new GfmlVarTkn("eval", GfmlTknAry_.ary_(valTkn), evalContext, "varKey");
  58. return new Object[] {rv, varItem};
  59. }
  60. void ini_SubFldDefault_add(GfmlType type, String subFldKey, String defaultVal) {
  61. GfmlFld subFld = type.SubFlds().Get_by(subFldKey);
  62. GfmlTkn defaultTkn = GfmlTkn_.raw_(defaultVal);;
  63. subFld.DefaultTkn_(defaultTkn);
  64. }
  65. void tst_SubFldDefault(GfmlType type, String subFldKey, String expdDefaultVal) {
  66. GfmlFld subFld = type.SubFlds().Get_by(subFldKey);
  67. GfmlTkn defaultTkn = GfmlTkn_.as_(subFld.DefaultTkn());
  68. String actlDefaultVal = defaultTkn == null || defaultTkn == GfmlTkn_.Null ? null : defaultTkn.Val();
  69. Tfds.Eq(expdDefaultVal, actlDefaultVal);
  70. }
  71. void tst_SubFldExists(GfmlType type, String subFldKey, boolean expd) {
  72. GfmlFld subFld = type.SubFlds().Get_by(subFldKey);
  73. Tfds.Eq(expd, subFld != null);
  74. }
  75. GfmlType make_(String key, String name, String... atrs) {
  76. GfmlTypeMakr typeMakr = GfmlTypeMakr.new_();
  77. GfmlType rv = typeMakr.MakeRootType(key, name);
  78. for (String atr : atrs)
  79. typeMakr.AddSubFld(GfmlFld.new_(true, atr, GfmlType_.StringKey));
  80. return rv;
  81. }
  82. }