1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
|
using NUnit.Framework;
using System;
using Mono.Documentation;
using System.Xml;
using System.Xml.XPath;
using System.Linq;
using Mono.Cecil;
using System.Collections.Generic;
using Mono.Documentation.Updater.Frameworks;
using Mono.Documentation.Updater;
namespace mdoc.Test
{
public class OneAttribute : Attribute { }
public class TwoAttribute : Attribute { }
[One]
public class MyClass
{
[One]
public void Meth (int a, string d, int c) { }
}
public class MyClass2
{
public void Meth (int a, string b, int c) { }
}
public class MyComplicatedClass
{
public IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback cback, object state) => null;
}
}
namespace mdoc.Test2
{
using mdoc.Test;
[One, Two]
public class MyClass
{
[One, Two]
public void Meth (int a, string b, int c) { }
}
[Two]
public class MyClass2
{
[Two]
public void Meth (int d, string e, int f) { }
}
}
namespace mdoc.Test3
{
public class MyClass
{
public void Meth(int a, string b, int c) { }
}
public class MyClass2
{
public void Meth(int A, string B, int C) { }
}
}
namespace mdoc.Test
{
/// <summary>
/// Tests functions that update the EcmaXMl under various conditions from
/// corresponding classes.
/// </summary>
[TestFixture ()]
public class XmlUpdateTests
{
class ParamContext {
public XmlDocument doc;
public string beforeXML;
public MethodReference method;
public List<ParameterDefinition> parameters;
public MDocUpdater updater;
public FrameworkIndex fx;
}
#region Myclass Tests
[Test ()]
public void Parameters_Updating_Normal ()
{
var context = InitContext <MyClass>(startingEmptyXml, 0);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
context.fx.Frameworks.RemoveAt(2);
context.fx.Frameworks.RemoveAt(1);
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize(normalSingleXml), afterXML);
}
[Test ()]
public void Parameters_Updating_Existing_MultiFramework ()
{
var context = InitContext <MyClass>(multiFrameworkXml, 0);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
// the original state was cleared out on account that this was the first of the frameworks
Assert.AreEqual (Normalize(XmlConsts.NormalSingleXml), Normalize(afterXML));
}
#endregion
#region MyClass2 tests
[Test ()]
public void Parameters2_Updating_Normal ()
{
var context = InitContext<MyClass2> (startingEmptyXml, 0);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (XmlConsts.NormalSingleXml2), afterXML);
}
[Test ()]
public void Parameters_Updating_Normal22 ()
{
var context = InitContext<MyClass2> (XmlConsts.NormalSingleXml2, 1);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[1].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (XmlConsts.MultiFrameworkXml2Second), afterXML);
}
[Test ()]
public void Parameters_Updating_Normal32 ()
{
var context = InitContext<MyClass2> (XmlConsts.MultiFrameworkXml2Second, 2);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[2].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (XmlConsts.MultiFrameworkXml2), afterXML);
}
[Test()]
public void Parameters_Updating_BeginRead()
{
FrameworkIndex fx = new FrameworkIndex("", 3, null);
fx.Frameworks.Add(new FrameworkEntry(fx.Frameworks, fx.Frameworks) { Id = "One", Name = "One", Replace = "mdoc.Test2", With = "mdoc.Test" });
fx.Frameworks.Add(new FrameworkEntry(fx.Frameworks, fx.Frameworks) { Id = "Two", Name = "Two", Replace = "mdoc.Test2", With = "mdoc.Test" });
fx.Frameworks.Add(new FrameworkEntry(fx.Frameworks, fx.Frameworks) { Id = "Three", Name = "Three", Replace = "mdoc.Test2", With = "mdoc.Test" });
var context = InitComplexContext<MyComplicatedClass>(XmlConsts.XML_METHOD_TESTMETHOD_BEFORE, "mdoc.Test", "BeginRead", fx);
var theType = context.method.DeclaringType.Resolve();
foreach (var it in fx.Frameworks)
{
var t = it.ProcessType(theType);
foreach(var m in theType.Methods)
{
t.ProcessMember(m);
}
}
var typeEntry = context.fx.Frameworks[2].Types.First();
bool fxAlternateTriggered = true;
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
// TODO: make representative post-method XML
Assert.AreEqual(Normalize(XmlConsts.XML_METHOD_TESTMETHOD_AFTER), afterXML);
}
[Test ()]
public void Parameters_Updating_Existing_MultiFramework22 ()
{
var context = InitContext<MyClass2> (XmlConsts.MultiFrameworkXml2, 0);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize(XmlConsts.NormalSingleXml2), Normalize (afterXML));
}
[Test ()]
public void Parameters_Updating_Existing_MultiFramework222 ()
{
var context = InitContext<MyClass2> (XmlConsts.MultiFrameworkXml2, 2);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[2].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (context.beforeXML, Normalize (afterXML));
}
[Test ()]
public void Parameters_Updating_Existing_Align2 ()
{
var context = InitContext<MyClass2> (XmlConsts.MultiFrameworkXml2, 1, forceAlignment: true);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[1].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.IsFalse (fxAlternateTriggered);
Assert.AreEqual (Normalize (XmlConsts.MultiFrameworkAligned2), Normalize (afterXML));
}
#endregion
#region Simple Parameter Change across multi FX
[Test ()]
public void Parameters_Updating_Existing_NameChange ()
{
var context = InitContext<MyClass2> (XmlConsts.NormalSingleXml2, 1, forceAlignment: false);
Func<int, FrameworkTypeEntry> typeEntry = i => context.fx.Frameworks[i].Types.First ();
bool fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry(0), ref fxAlternateTriggered);
// this will be false, since it clears out the parameters list on the first run
Assert.IsFalse (fxAlternateTriggered, "first run");
fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry (1), ref fxAlternateTriggered);
Assert.IsFalse (fxAlternateTriggered, "second run");
fxAlternateTriggered = false;
context.updater.MakeParameters (context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry (2), ref fxAlternateTriggered);
Assert.IsFalse (fxAlternateTriggered, "third run");
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (XmlConsts.MultiFrameworkAlignedOther), Normalize (afterXML));
}
#endregion
#region Parameter Name Alpha Diff
[Test()]
public void Parameters3_Making_NameDiff()
{
var context = InitContext<mdoc.Test3.MyClass>(startingEmptyXml, 0, false, ns1: "mdoc.Test3", ns2: "mdoc.Test3");
var context2 = InitContext<mdoc.Test3.MyClass2>(startingEmptyXml, 1, false, ns1: "mdoc.Test3", ns2: "mdoc.Test3");
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First();
FrameworkTypeEntry typeEntry2 = context.fx.Frameworks[1].Types.First();
bool fxAlternateTriggered = false;
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context2.method, context2.parameters, typeEntry2, ref fxAlternateTriggered);
var afterXML = context.doc.OuterXml;
Assert.IsTrue(afterXML.Contains("Parameter Name=\"a\""), "missing 'a'");
Assert.IsTrue(afterXML.Contains("Parameter Name=\"b\""), "missing 'b'");
Assert.IsTrue(afterXML.Contains("Parameter Name=\"c\""), "missing 'c'");
Assert.IsTrue(afterXML.Contains("Parameter Name=\"A\""), "missing 'A'");
Assert.IsTrue(afterXML.Contains("Parameter Name=\"B\""), "missing 'B'");
Assert.IsTrue(afterXML.Contains("Parameter Name=\"C\""), "missing 'C'");
}
[Test()]
public void Parameters3_Updating_NameDiff()
{
var context = InitContext<mdoc.Test3.MyClass>(startingEmptyXml, 0, false, ns1: "mdoc.Test3", ns2: "mdoc.Test3");
var context2 = InitContext<mdoc.Test3.MyClass2>(startingEmptyXml, 1, false, ns1: "mdoc.Test3", ns2: "mdoc.Test3");
var context3 = InitContext<mdoc.Test3.MyClass2>(startingEmptyXml, 2, false, ns1: "mdoc.Test3", ns2: "mdoc.Test3");
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First();
FrameworkTypeEntry typeEntry2 = context.fx.Frameworks[1].Types.First();
FrameworkTypeEntry typeEntry3 = context.fx.Frameworks[2].Types.First();
bool fxAlternateTriggered = false;
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context.method, context.parameters, typeEntry, ref fxAlternateTriggered);
context.updater.UpdateParameters(context.doc.FirstChild as XmlElement, "param", context.parameters.Select(p => p.Name).ToArray(), typeEntry);
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context2.method, context2.parameters, typeEntry2, ref fxAlternateTriggered);
context.updater.UpdateParameters(context.doc.FirstChild as XmlElement, "param", context2.parameters.Select(p => p.Name).ToArray(), typeEntry2);
context.updater.MakeParameters(context.doc.FirstChild as XmlElement, context3.method, context3.parameters, typeEntry3, ref fxAlternateTriggered);
context.updater.UpdateParameters(context.doc.FirstChild as XmlElement, "param", context3.parameters.Select(p => p.Name).ToArray(), typeEntry3);
var afterXML = context.doc.OuterXml;
Assert.IsTrue(afterXML.Contains("param name=\"a\""), "missing 'a'");
Assert.IsTrue(afterXML.Contains("param name=\"b\""), "missing 'b'");
Assert.IsTrue(afterXML.Contains("param name=\"c\""), "missing 'c'");
Assert.IsTrue(afterXML.Contains("param name=\"A\""), "missing 'A'");
Assert.IsTrue(afterXML.Contains("param name=\"B\""), "missing 'B'");
Assert.IsTrue(afterXML.Contains("param name=\"C\""), "missing 'C'");
}
#endregion
[Test ()]
public void MemberSignature_Updating_Existing_Align ()
{
var context = InitContext <MyClass>(SigmultiFrameworkXml, 0, forceAlignment: true);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry, fxAlternateTriggered:false);
var afterXML = context.doc.OuterXml;
// first framework looks like it already looked, so no need to update
Assert.AreEqual (Normalize (SigmultiFrameworkXml), Normalize (afterXML));
}
[Test ()]
public void MemberSignature_Updating_Existing_Align2 ()
{
var context = InitContext <MyClass>(SigmultiFrameworkXml, 2, forceAlignment: true);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[2].Types.First ();
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry, fxAlternateTriggered: false);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (SigmultiFrameworkAligned), Normalize (afterXML));
}
[Test ()]
public void MemberSignature_Updating_Existing_NoChange ()
{
var context = InitContext <MyClass>(SigmultiFrameworkXml, 2, forceAlignment: false);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[2].Types.First ();
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry, fxAlternateTriggered: false);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (SigmultiFrameworkXml), Normalize (afterXML));
}
[Test ()]
public void MemberSignature_Updating_Existing_NoChange_regular ()
{
var context = InitContext<MyClass> (SigRegular, 1, forceAlignment: false);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[1].Types.First ();
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry, fxAlternateTriggered: false);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (SigRegular), Normalize (afterXML));
}
[Test ()]
public void MemberSignature_Updating_Existing_NameChanged_SingleFX()
{
// handles the case
var context = InitContext<MyClass> (SigRegular, 2, forceAlignment: false);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[0].Types.First ();
context.fx.Frameworks.RemoveAt (2);
context.fx.Frameworks.RemoveAt (1);
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry, fxAlternateTriggered: true);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (SigRegularChanged), Normalize (afterXML));
}
[Test ()]
public void MemberSignature_Updating_Existing_NameChanged_MultiFX ()
{
// handles the case
var context = InitContext<MyClass> (SigRegular, 2, forceAlignment: false);
Func<int, FrameworkTypeEntry> typeEntry = i => context.fx.Frameworks[i].Types.First ();
var sig = new CSharpMemberFormatter ();
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry(0), fxAlternateTriggered: true);
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry(1), fxAlternateTriggered: false);
MDocUpdater.UpdateSignature (sig, context.method, context.doc.FirstChild as XmlElement, typeEntry(2), fxAlternateTriggered: false);
var afterXML = context.doc.OuterXml;
Assert.AreEqual (Normalize (SigRegularAllAligned), Normalize (afterXML));
}
[Test ()]
public void DocMemberEnumerator()
{
var context = InitContext <MyClass>(string.Format (typeFrameXml, multiFrameworkXml), 1, forceAlignment: true);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[1].Types.First ();
var enumerator = new DocumentationEnumerator ();
var matches = enumerator.GetDocumentationMembers (context.doc, context.method.DeclaringType.Resolve (), typeEntry);
Assert.IsTrue (matches.Any (m => m.Member == context.method && m.Node != null), "didn't match the member");
}
[Test ()]
public void DocMemberEnumerator2 ()
{
var context = InitContext <MyClass>(string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: true);
FrameworkTypeEntry typeEntry = context.fx.Frameworks[2].Types.First ();
var enumerator = new DocumentationEnumerator ();
var matches = enumerator.GetDocumentationMembers (context.doc, context.method.DeclaringType.Resolve (), typeEntry);
Assert.IsTrue (matches.Any (m => m.Member == context.method && m.Node != null), "didn't match the member");
}
[Test]
public void Attributes_TypeOrMethod()
{
var context = InitContext<MyClass>(string.Format(typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First();
string[] attributeList = new[] { "One" };
MDocUpdater.MakeAttributes(context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
var attrNode = context.doc.FirstChild.SelectSingleNode("Attributes");
var attributes = attrNode.SelectNodes("Attribute").Cast<XmlElement>().ToArray();
Assert.IsTrue(attributes.Count() == 1);
Assert.AreEqual("One", attributes[0].FirstChild.InnerText);
Assert.AreEqual("Three", attributes[0].GetAttribute(Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AttributeRemoved()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
MDocUpdater.MakeAttributes(context.doc.FirstChild as XmlElement, new string[0], fx, typeEntry);
MDocUpdater.MakeAttributes(context.doc.FirstChild as XmlElement, new string[0], context.fx.Frameworks[2] , typeEntry);
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
Assert.IsNull(attrNode);
}
[Test]
public void Attributes_TypeOrMethod_AllFX ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
foreach (var fx in context.fx.Frameworks)
{
//var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsFalse (attributes[0].HasAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_OneMissing ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
foreach (var fx in context.fx.Frameworks)
{
//var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (fx.IsFirstFramework)
attributeList = new string[0];
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Three;Two", attributes[0].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_OneMissing_Last ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
foreach (var fx in context.fx.Frameworks)
{
//var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (fx.IsLastFramework)
attributeList = new string[0];
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("One;Three", attributes[0].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_RunExisting_Middle ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
// first, go through and add "One" and "Two" to all of them
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One", "Two" };
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
// Now, to test the first deployment on an existing set
// in this case, the truth of the matter is that `Two` only exists in the middle
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (!fx.IsFirstFramework && !fx.IsLastFramework) {
attributeList = new[] { "One", "Two" };
}
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 2);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsFalse (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Two", attributes[1].FirstChild.InnerText);
Assert.IsTrue (attributes[1].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Three", attributes[1].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_RunExisting_First ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
// first, go through and add "One" and "Two" to all of them
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One", "Two" };
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
// Now, to test the first deployment on an existing set
// in this case, the truth of the matter is that `Two` only exists in the middle
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (fx.IsFirstFramework)
{
attributeList = new[] { "One", "Two" };
}
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 2);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsFalse (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Two", attributes[1].FirstChild.InnerText);
Assert.IsTrue (attributes[1].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("One", attributes[1].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_RunExisting_Last ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
// first, go through and add "One" and "Two" to all of them
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One", "Two" };
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "Two" };
if (fx.IsLastFramework)
{
attributeList = new[] { "One", "Two" };
}
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 2);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Two", attributes[0].GetAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("Two", attributes[1].FirstChild.InnerText);
Assert.IsFalse (attributes[1].HasAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_TypeOrMethod_AllFX_OneMissing_Middle ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
foreach (var fx in context.fx.Frameworks)
{
//var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (!fx.IsLastFramework && !fx.IsFirstFramework)
attributeList = new string[0];
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("One;Two", attributes[0].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_Assembly ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
foreach (var fx in context.fx.Frameworks)
{
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
if (!fx.IsLastFramework && !fx.IsFirstFramework)
{
attributeList = new string[0];
}
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
}
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
Assert.AreEqual ("One;Two", attributes[0].GetAttribute (Consts.FrameworkAlternate));
}
[Test]
public void Attributes_Assembly_OtherAssembly ()
{
var context = InitContext<MyClass> (string.Format (typeFrameXml, multiFrameworkXml), 2, forceAlignment: false);
var fx = context.fx.Frameworks[1];
FrameworkTypeEntry typeEntry = fx.Types.First ();
string[] attributeList = new[] { "One" };
// this is the 'second' fx, and we've changed the expected assembly name,
// so the attribute, while it doesn't exist yet, shouldn't have an FX made since it doesn't exist in any other FX
MDocUpdater.MakeAttributes (context.doc.FirstChild as XmlElement, attributeList, fx, typeEntry);
var attrNode = context.doc.FirstChild.SelectSingleNode ("Attributes");
var attributes = attrNode.SelectNodes ("Attribute").Cast<XmlElement> ().ToArray ();
Assert.IsTrue (attributes.Count () == 1);
Assert.AreEqual ("One", attributes[0].FirstChild.InnerText);
Assert.IsTrue (attributes[0].HasAttribute (Consts.FrameworkAlternate));
}
string Normalize(string xml, bool clearFx = false) {
XmlDocument doc = new XmlDocument ();
doc.LoadXml (xml);
return doc.OuterXml;
}
string typeFrameXml = @"<Type><Members>{0}</Members></Type>";
#region MyClass XML
string startingEmptyXml = @"<Member MemberName=""Meth"">
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
</Parameters>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>";
string normalSingleXml = @"<Member MemberName=""Meth"">
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name = ""a"" Type=""System.Int32"" />
<Parameter Name = ""d"" Type=""System.String"" />
<Parameter Name = ""c"" Type=""System.Int32"" />
</Parameters>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>";
string multiFrameworkXml = @"<Member MemberName=""Meth"">
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name = ""a"" Type=""System.Int32"" Index=""0"" />
<Parameter Name = ""d"" Type=""System.String"" Index=""1"" FrameworkAlternate=""One;Three"" />
<Parameter Name = ""b"" Type=""System.String"" Index=""1"" FrameworkAlternate=""Two"" />
<Parameter Name = ""c"" Type=""System.Int32"" Index=""2"" />
</Parameters>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>";
string multiFrameworkAligned = @"<Member MemberName=""Meth"">
<MemberType>Method</MemberType>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name = ""a"" Type=""System.Int32"" Index=""0"" />
<Parameter Name = ""d"" Type=""System.String"" Index=""1"" />
<Parameter Name = ""c"" Type=""System.Int32"" Index=""2"" />
</Parameters>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>";
#endregion
string SigmultiFrameworkXml = @"<Member MemberName=""Meth"">
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string d, int c);"" FrameworkAlternate=""One;Three"" />
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string b, int c);"" FrameworkAlternate=""Two"" />
</Member>";
string SigmultiFrameworkAligned = @"<Member MemberName=""Meth"">
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string d, int c);"" FrameworkAlternate=""One;Three;Two"" />
</Member>";
string SigRegular = @"<Member MemberName=""Meth"">
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string d, int c);"" />
</Member>";
string SigRegularChanged = @"<Member MemberName=""Meth"">
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string b, int c);"" />
</Member>";
string SigRegularAllAligned = @"<Member MemberName=""Meth"">
<MemberSignature Language=""C#"" Value=""public void Meth (int a, string b, int c);"" FrameworkAlternate=""One;Three;Two"" />
</Member>";
private ParamContext InitComplexContext<T>(string methodXml, string ns, string methodName, FrameworkIndex fx)
{
var doc = new XmlDocument();
doc.LoadXml(methodXml);
var beforeXML = doc.OuterXml;
TypeDefinition type = GetDefinition<T>(ns);
var method = type.Methods.First(m => m.Name == methodName) as MethodReference;
var updater = new MDocUpdater();
return new ParamContext()
{
doc = doc,
beforeXML = beforeXML,
method = method,
parameters = method.Parameters.ToList(),
fx = fx,
updater = updater
};
}
private ParamContext InitContext <T>(string methodXml, int fxIndex, bool forceAlignment=false, string ns1="mdoc.Test", string ns2="mdoc.Test2")
{
Func<int, bool> indexCheck = fi => fi < 2;
if (typeof(T) == typeof(MyClass2))
indexCheck = fi => fi != 1;
bool useFirst = forceAlignment || indexCheck(fxIndex);
var doc = new XmlDocument ();
//doc.PreserveWhitespace = true;
doc.LoadXml (methodXml);
var beforeXML = doc.OuterXml;
XmlElement root = doc.SelectSingleNode ("//Docs") as XmlElement; // Docs
TypeDefinition type = GetDefinition<T> (ns1);
var method = type.Methods.First (m => m.Name == "Meth") as MethodReference;
var parameters = method.Parameters.ToList ();
TypeDefinition type2 = GetDefinition<T> (ns2);
var method2 = type2.Methods.First (m => m.Name == "Meth") as MethodReference;
var parameters2 = method2.Parameters.ToList ();
// updater
var updater = new MDocUpdater ();
var fx = new FrameworkIndex ("", 3, null);
fx.Frameworks.Add (new FrameworkEntry (fx.Frameworks, fx.Frameworks) { Id = "One", Name = "One", Replace=ns2, With=ns1 });
fx.Frameworks.Add (new FrameworkEntry (fx.Frameworks, fx.Frameworks) { Id = "Three", Name = "Three", Replace = ns2, With = ns1 });
fx.Frameworks.Add (new FrameworkEntry (fx.Frameworks, fx.Frameworks) { Id = "Two", Name = "Two", Replace = ns2, With = ns1 });
var i = 0;
foreach (var f in fx.Frameworks)
{
if (indexCheck(i) || forceAlignment)
{
var t = f.ProcessType (type);
t.ProcessMember (method);
var aset = new AssemblySet (new[] { "one.dll" });
f.AddAssemblySet (aset);
}
else {
var t = f.ProcessType (type2);
t.ProcessMember (method2);
}
i++;
}
updater.InitializeFrameworksCache (fx);
return new ParamContext ()
{
doc = doc,
beforeXML = beforeXML,
method = useFirst ? method : method2,
parameters = useFirst ? parameters : parameters2,
fx = fx,
updater = updater
};
}
TypeDefinition GetDefinition<T>(string ns)
{
Type t = typeof (T);
string path = t.Assembly.Location;
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly (path);
return assembly.MainModule.Types.First (type =>
type.Name == t.Name && type.Namespace == ns);
}
}
}
|