z601_edit_atr_tst.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 z601_edit_atr_tst {
  15. GfmlUpdateFx fx = GfmlUpdateFx.new_();
  16. @Test public void Basic() {
  17. fx .Raw_("a=1;").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "2"))
  18. .tst_("a=2;");
  19. }
  20. @Test public void WhiteSpaceComment() {
  21. fx .Raw_("a = /*comment*/1;").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "2"))
  22. .tst_("a = /*comment*/2;");
  23. }
  24. @Test public void Quoted() {
  25. fx .Raw_("a='1';").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "2"))
  26. .tst_("a='2';");
  27. }
  28. @Test public void EmbeddedQuote() {
  29. fx .Raw_("a=1;").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "1'2"))
  30. .tst_("a='1''2';");
  31. }
  32. @Test public void ReuseQuote() {
  33. fx .Raw_("a=|'1'|;").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "2"))
  34. .tst_("a=|'2'|;");
  35. }
  36. @Test public void ReuseQuoteWithEmbeddedQuote() {
  37. fx .Raw_("a=|'1'2'|;").Update_(fx.atr_().NdeIdxs_(0).Atr_("a", "2'3"))
  38. .tst_("a=|'2'3'|;");
  39. }
  40. // @Test public void AtrIdx() {
  41. // fx .Raw_("1;").Update_(fx.atr_().NdeIdxs_(0).AtrIdx_(0, "2"))
  42. // .tst_("2;");
  43. // }
  44. // @Test public void AtrIdx_parens() {
  45. // fx .Raw_("A_('1');").Update_(fx.atr_().NdeIdxs_(0).AtrIdx_(0, "2"))
  46. // .tst_("A_('2');");
  47. // }
  48. @Test public void AddNew() {
  49. fx .Raw_("a:;").Update_(fx.atr_().NdeIdxs_(0).Atr_("b", "1"))
  50. .tst_("a:b='1';");
  51. }
  52. @Test public void AddExisting() {
  53. fx .Raw_("a='1';").Update_(fx.atr_().NdeIdxs_(0).Atr_("b", "2"))
  54. .tst_("a='1' b='2';");
  55. }
  56. @Test public void Add_many() {
  57. fx .Raw_("a='1';").Update_(fx.atr_().NdeIdxs_(0).Atr_("b", "2")).Update_(fx.atr_().NdeIdxs_(0).Atr_("c", "3"))
  58. .tst_("a='1' b='2' c='3';");
  59. }
  60. @Test public void AddNode() {
  61. fx .Raw_("a(){}").Update_(fx.nde_().NdeIdxs_(0).Nest_("b"))
  62. .tst_("a(){b(){}}");
  63. }
  64. @Test public void AddNodeMany() {
  65. fx .Raw_("a(){}").Update_(fx.nde_().NdeIdxs_(0).Nest_("b")).Update_(fx.nde_().NdeIdxs_(0).Nest_("c"))
  66. .tst_("a(){b(){}c(){}}");
  67. }
  68. @Test public void EditNode() {
  69. fx .Raw_("a(){b(){}}").Update_(fx.nde_().NdeIdxs_(0).Nest_("b"))
  70. .tst_("a(){b(){}}");
  71. }
  72. }
  73. interface GfmlUpdateCmd {
  74. void Exec(GfmlDoc gdoc);
  75. }
  76. class GfmlUpdateCmd_atr implements GfmlUpdateCmd {
  77. public GfmlUpdateCmd_atr NdeIdxs_(int... v) {ndeIdxs = v; return this;} int[] ndeIdxs;
  78. public GfmlUpdateCmd_atr Atr_(String key, String val) {atrKey = key; atrVal = val; return this;} String atrKey, atrVal;
  79. // public GfmlUpdateCmd_atr AtrIdx_(int idx, String val) {atrIdx = idx; atrVal = val; return this;} int atrIdx = -1;
  80. public void Exec(GfmlDoc gdoc) {
  81. GfmlNde nde = GetNde(ndeIdxs, gdoc.RootNde());
  82. nde.UpdateAtr(atrKey, atrVal);
  83. // GfmlAtr atr = atrIdx != -1 ? GfmlAtr.as_(nde.SubKeys().Get_at(atrIdx)) : GfmlAtr.as_(nde.SubKeys().Get_by(atrKey));
  84. // atr.UpdateAtr(atrKey, atrVal);
  85. atrKey = ""; atrVal = "";//atrIdx = -1;
  86. }
  87. public static GfmlNde GetNde(int[] ndeIdxs, GfmlNde owner) {
  88. GfmlNde nde = owner;
  89. for (int i = 0; i < ndeIdxs.length; i++) {
  90. int ndeIdx = ndeIdxs[i];
  91. nde = (GfmlNde)owner.SubHnds().Get_at(ndeIdx);
  92. }
  93. return nde;
  94. }
  95. public static GfmlUpdateCmd_atr new_() {return new GfmlUpdateCmd_atr();} GfmlUpdateCmd_atr() {}
  96. }
  97. class GfmlUpdateCmd_nde implements GfmlUpdateCmd {
  98. public GfmlUpdateCmd_nde NdeIdxs_(int... v) {ndeIdxs = v; return this;} int[] ndeIdxs;
  99. public GfmlUpdateCmd_nde Nest_(String hnd) {ndeHnd = hnd; return this;} String ndeHnd;
  100. public void Exec(GfmlDoc gdoc) {
  101. GfmlNde nde = GfmlUpdateCmd_atr.GetNde(ndeIdxs, gdoc.RootNde());
  102. nde.UpdateNde(ndeHnd);
  103. ndeHnd = "";
  104. }
  105. public static GfmlUpdateCmd_nde new_() {return new GfmlUpdateCmd_nde();} GfmlUpdateCmd_nde() {}
  106. }
  107. class GfmlUpdateFx {
  108. public GfmlUpdateCmd_atr atr_() {return GfmlUpdateCmd_atr.new_();}
  109. public GfmlUpdateCmd_nde nde_() {return GfmlUpdateCmd_nde.new_();}
  110. public String Raw() {return raw;} public GfmlUpdateFx Raw_(String v) {raw = v; return this;} private String raw;
  111. public GfmlUpdateFx Update_(GfmlUpdateCmd cmd) {cmds.Add(cmd); return this;} List_adp cmds = List_adp_.New();
  112. public GfmlUpdateFx tst_(String expd) {
  113. GfmlDoc actlDoc = GfmlDataNde.new_any_eol_(raw).Doc();
  114. for (int i = 0; i < cmds.Count(); i++) {
  115. GfmlUpdateCmd cmd = (GfmlUpdateCmd)cmds.Get_at(i);
  116. cmd.Exec(actlDoc);
  117. }
  118. String actl = actlDoc.RootNde().To_str();
  119. Tfds.Eq(expd, actl);
  120. cmds.Clear();
  121. return this;
  122. }
  123. public static GfmlUpdateFx new_() {return new GfmlUpdateFx();} GfmlUpdateFx() {}
  124. }