XPParserFilters.pas
上传用户:yjb1804
上传日期:2021-01-30
资源大小:3105k
文件大小:14k
源码类别:

Email服务器

开发平台:

Delphi

  1. unit XPParserFilters;
  2. {
  3.  $Source: /cvsroot/dunit/dunit/Contrib/DUnitWizard/Source/DelphiExperts/DUnitProject/XPParserFilters.pas,v $
  4.  $Revision: 1.3 $
  5.  $Date: 2004/08/22 14:25:40 $
  6.  Last amended by $Author: pvspain $
  7.  $State: Exp $
  8.  XPParserFilters:
  9.  Copyright (c) 2003 by The Excellent Programming Company Pty Ltd
  10.  (Australia) (ABN 27 005 394 918). All rights reserved.
  11.  Contact Paul Spain via email: paul@xpro.com.au
  12.  This unit is free software; you can redistribute it and/or
  13.  modify it under the terms of the GNU Lesser General Public
  14.  License as published by the Free Software Foundation; either
  15.  version 2.1 of the License, or (at your option) any later version.
  16.  This unit is distributed in the hope that it will be useful,
  17.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19.  Lesser General Public License for more details.
  20.  You should have received a copy of the GNU Lesser General Public
  21.  License along with this unit; if not, the license can be viewed at:
  22.  http://www.gnu.org/copyleft/lesser.html
  23.  or write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24.  Boston, MA  02111-1307  USA
  25. }
  26. interface
  27. uses
  28.   XPDUnitCommon,
  29.   XPTestedUnitUtils;
  30. type
  31.   { Primary filters: take raw output from parser as input. }
  32.   IXPPrimaryFilter = interface (IXPParserTree)
  33.     ['{76D2AA07-6C4A-416E-A058-96504F070022}']
  34.   end;
  35.   IXPTestedModuleFilter = interface (IXPPrimaryFilter)
  36.     ['{2582ADE6-2C85-415A-B685-89C8970B2699}']
  37.     procedure SetInput(const ASource: IXPParserTree;
  38.       const ABehaviours: IXPDUnitBehaviours);
  39.   end;
  40.   IXPTestedClassFilter = interface (IXPPrimaryFilter)
  41.     ['{1F98DE27-3BC4-4F0F-AB5A-5929BAF1EEE6}']
  42.     procedure SetInput(const ASource: IXPParserTree;
  43.       const ABehaviours: IXPDUnitBehaviours; const CursorPos: longint);
  44.   end;
  45.   { Secondary filters: take output from primary filters as input. }
  46.   IXPSecondaryFilter = interface (IXPParserTree)
  47.     ['{A8EB34F7-12A1-49F6-A02C-CC32635F0749}']
  48.   end;
  49.   IXPTestClassFilter = interface (IXPSecondaryFilter)
  50.     ['{B9E5A3DE-0926-4524-BA87-F41228F0FD36}']
  51.     procedure SetInput(const ASource: IXPPrimaryFilter);
  52.   end;
  53. { Takes raw TestedUnit parser output as input. Applies current Behaviour plus
  54.   additional pruning for external module testing. }
  55. function CreateTestedModuleFilter: IXPTestedModuleFilter;
  56. { Takes raw TestedUnit parser output as input. Applies current Behaviour plus
  57.   additional pruning for selected class testing. }
  58. function CreateTestedClassFilter: IXPTestedClassFilter;
  59. { Takes output of TestedModuleFilter or TestedClassFilter after additional user
  60.   manipulation as input. Creates test classes as output (*without*
  61.   parameter-modified class and method names). }
  62. function CreateTestClassFilter: IXPTestClassFilter;
  63. implementation
  64. uses
  65.   XPDUnitSetup,         // CreateXPDUnitBehaviours()
  66.   XPDUnitParameters,    // CreateXPDUnitParameters()
  67.   XPInterfacedObject,
  68.   SysUtils;             // Supports()
  69. const CVSID: string = '$Header: /cvsroot/dunit/dunit/Contrib/DUnitWizard/Source/DelphiExperts/DUnitProject/XPParserFilters.pas,v 1.3 2004/08/22 14:25:40 pvspain Exp $';
  70. type
  71.   TTestedModuleFilter = class (TXPParserTree, IXPPrimaryFilter,
  72.     IXPTestedModuleFilter)
  73.   protected
  74.     procedure SetInput(const ASource: IXPParserTree;
  75.     const ABehaviours: IXPDUnitBehaviours);
  76.   public
  77.     constructor Create(const ADelegator: IInterface = nil);
  78.   end;
  79. function CreateTestedModuleFilter: IXPTestedModuleFilter;
  80. begin
  81.   Result := TTestedModuleFilter.Create;
  82. end;
  83. type
  84.   TTestedClassFilter = class (TXPParserTree, IXPPrimaryFilter,
  85.     IXPTestedClassFilter)
  86.   protected
  87.    procedure SetInput(const ASource: IXPParserTree;
  88.      const ABehaviours: IXPDUnitBehaviours; const ACursorPos: longint);
  89.   public
  90.     constructor Create(const ADelegator: IInterface = nil);
  91.   end;
  92. function CreateTestedClassFilter: IXPTestedClassFilter;
  93. begin
  94.   Result := TTestedClassFilter.Create;
  95. end;
  96. type
  97.   TTestClassFilter = class (TXPParserTree, IXPSecondaryFilter,
  98.     IXPTestClassFilter)
  99.   protected
  100.     procedure SetInput(const ASource: IXPPrimaryFilter);
  101.   public
  102.     constructor Create(const ADelegator: IInterface = nil);
  103.   end;
  104. function CreateTestClassFilter: IXPTestClassFilter;
  105. begin
  106.   Result := TTestClassFilter.Create;
  107. end;
  108. { TTestedModuleFilter }
  109. constructor TTestedModuleFilter.Create(const ADelegator: IInterface);
  110. const
  111.   AParent = nil;
  112.   AName = '';
  113.   AEnabled = true;
  114. begin
  115.   inherited Create(AParent, AName, AEnabled, ADelegator);
  116. end;
  117. procedure TTestedModuleFilter.SetInput(const ASource: IXPParserTree;
  118.   const ABehaviours: IXPDUnitBehaviours);
  119. var
  120.   SourceSection: IXPParserNode;
  121.   Node: IXPParserNode;
  122.   SourceClass: IXPClassNode;
  123.   SourceVisibility: IXPParserNode;
  124.   SourceClassMember: IXPParserNode;
  125.   SourceMethod: IXPMethodNode;
  126.   SourceProperty: IXPPropertyNode;
  127.   SourceClassMemberCount: integer;
  128.   CurrentVisibility: TXPClassVisibility;
  129.   FilterSection: IXPSectionNode;
  130.   FilterClass: IXPClassNode;
  131.   FunctionNode: IXPFunctionNode;
  132.   GlobalFunctions: IXPClassNode;
  133.   FilterVisibility: IXPVisibilityNode;
  134. begin
  135.   Clear;
  136.   ASource.Children.Start;
  137.   while ASource.Children.Next(SourceSection) do
  138.   begin
  139.     // Exclude all sections except interface from parser output
  140.     if (SourceSection as IXPSectionNode).GetSection <> usInterface then
  141.       continue;
  142.     FilterSection := CreateXPSectionNode(self, usInterface,
  143.       SourceSection.Enabled);
  144.     SourceSection.Children.Start;
  145.     GlobalFunctions := nil;
  146.     while SourceSection.Children.Next(Node) do
  147.       if Supports(Node, IXPFunctionNode, FunctionNode) then
  148.       begin
  149.         if not Assigned(GlobalFunctions) then
  150.         begin
  151.           GlobalFunctions := CreateXPClassNode(FilterSection,
  152.             ASource.Name + 'Globals');
  153.           GlobalFunctions.DeleteChild(GlobalFunctions.Visibilities[cvPublished]);
  154.           GlobalFunctions.DeleteChild(GlobalFunctions.Visibilities[cvProtected]);
  155.           GlobalFunctions.DeleteChild(GlobalFunctions.Visibilities[cvPrivate]);
  156.         end;
  157.         CreateXPMethodNode(GlobalFunctions.Visibilities[cvPublic],
  158.           FunctionNode.Name);
  159.       end
  160.       else if Supports(Node, IXPClassNode, SourceClass) then
  161.       begin
  162.         SourceClass.Children.Start;
  163.         SourceClassMemberCount := 0;
  164.         while SourceClass.Children.Next(SourceVisibility) do
  165.           if (SourceVisibility as IXPVisibilityNode).GetVisibility
  166.             <> cvPrivate then
  167.             System.Inc(SourceClassMemberCount, SourceVisibility.ChildCount);
  168.         // Exclude empty classes
  169.         if SourceClassMemberCount = 0 then
  170.           continue;
  171.         FilterClass := CreateXPClassNode(FilterSection, SourceClass.Name,
  172.           SourceClass.Enabled);
  173.         SourceClass.Children.Start;
  174.         while SourceClass.Children.Next(SourceVisibility) do
  175.         begin
  176.           CurrentVisibility
  177.             := (SourceVisibility as IXPVisibilityNode).GetVisibility;
  178.           if (SourceVisibility.ChildCount = 0)
  179.             or (CurrentVisibility = cvPrivate) then
  180.             // Exclude private and empty visibility nodes.
  181.             FilterClass.DeleteChild(
  182.               FilterClass.Visibilities[CurrentVisibility] as IXPParserNode)
  183.           else
  184.           begin
  185.             // Add source methods to filter visibility node.
  186.             FilterVisibility := FilterClass.Visibilities[CurrentVisibility];
  187.             // Apply behaviours
  188.             if CurrentVisibility = cvProtected then
  189.               FilterVisibility.Enabled
  190.                 := ABehaviours.ModuleAddCurrentProtectedMethods
  191.             else if CurrentVisibility = cvPublic then
  192.               FilterVisibility.Enabled
  193.                 := ABehaviours.ModuleAddCurrentPublicMethods
  194.             else
  195.               FilterVisibility.Enabled
  196.                 := ABehaviours.ModuleAddCurrentPublishedMethods;
  197.             SourceVisibility.Children.Start;
  198.             while SourceVisibility.Children.Next(SourceClassMember) do
  199.               if SysUtils.Supports(SourceClassMember, IXPMethodNode,
  200.                 SourceMethod) then
  201.                 CreateXPMethodNode(FilterVisibility, SourceMethod.Name,
  202.                   SourceMethod.Enabled)
  203.               else if SysUtils.Supports(SourceClassMember, IXPPropertyNode,
  204.                 SourceProperty) then
  205.                 CreateXPPropertyNode(FilterVisibility, SourceProperty.Name,
  206.                   SourceProperty.Enabled);
  207.           end;
  208.         end;
  209.       end;
  210.   end;
  211. end;
  212. { TTestClassFilter }
  213. constructor TTestClassFilter.Create(const ADelegator: IInterface);
  214. const
  215.   AParent = nil;
  216.   AName = '';
  217.   AEnabled = true;
  218. begin
  219.   inherited Create(AParent, AName, AEnabled, ADelegator);
  220. end;
  221. procedure TTestClassFilter.SetInput(const ASource: IXPPrimaryFilter);
  222. var
  223.   SourceSection: IXPParserNode;
  224.   SourceClass: IXPClassNode;
  225.   SourceVisibility: IXPParserNode;
  226.   SourceClassMember: IXPParserNode;
  227.   SourceMethod: IXPMethodNode;
  228.   SourceProperty: IXPPropertyNode;
  229.   EnabledSourceClassMemberCount: integer;
  230.   FilterSection: IXPSectionNode;
  231.   FilterClass: IXPClassNode;
  232.   FilterVisibility: IXPVisibilityNode;
  233. begin
  234.   Clear;
  235.   ASource.Children.Start;
  236.   while ASource.Children.Next(SourceSection) do
  237.   begin
  238.     // Exclude disabled sections
  239.     if not SourceSection.Enabled then
  240.       continue;
  241.     FilterSection := CreateXPSectionNode(
  242.       self, (SourceSection as IXPSectionNode).GetSection);
  243.     SourceSection.Children.Start;
  244.     while SourceSection.Children.Next(SourceClass) do
  245.     begin
  246.       // exclude disabled classes
  247.       if not SourceClass.Enabled then
  248.         continue;
  249.       EnabledSourceClassMemberCount := 0;
  250.       SourceClass.Children.Start;
  251.       while SourceClass.Children.Next(SourceVisibility) do
  252.         if SourceVisibility.Enabled then
  253.           System.Inc(EnabledSourceClassMemberCount,
  254.             SourceVisibility.EnabledChildCount);
  255.       // exclude classes with no enabled methods
  256.       if EnabledSourceClassMemberCount = 0 then
  257.         continue;
  258.       // leave only published visibility
  259.       FilterClass := CreateXPClassNode(FilterSection, SourceClass.Name);
  260.       FilterClass.DeleteChild(
  261.         FilterClass.Visibilities[cvPrivate] as IXPParserNode);
  262.       FilterClass.DeleteChild(
  263.         FilterClass.Visibilities[cvProtected] as IXPParserNode);
  264.       FilterClass.DeleteChild(
  265.         FilterClass.Visibilities[cvPublic] as IXPParserNode);
  266.       FilterVisibility := FilterClass.Visibilities[cvPublished];
  267.       SourceClass.Children.Start;
  268.       while SourceClass.Children.Next(SourceVisibility) do
  269.         if SourceVisibility.Enabled then
  270.         begin
  271.           SourceVisibility.Children.Start;
  272.           while SourceVisibility.Children.Next(SourceClassMember) do
  273.             if SourceClassMember.Enabled then
  274.             begin
  275.               if SysUtils.Supports(SourceClassMember, IXPMethodNode,
  276.                 SourceMethod) then
  277.                 CreateXPMethodNode(FilterVisibility, SourceMethod.Name)
  278.               else if SysUtils.Supports(SourceClassMember, IXPPropertyNode,
  279.                 SourceProperty) then
  280.                 CreateXPPropertyNode(FilterVisibility, SourceProperty.Name);
  281.             end;
  282.         end;
  283.     end;
  284.   end;
  285. end;
  286. { TTestedClassFilter }
  287. constructor TTestedClassFilter.Create(const ADelegator: IInterface);
  288. const
  289.   AParent = nil;
  290.   AName = '';
  291.   AEnabled = true;
  292. begin
  293.   inherited Create(AParent, AName, AEnabled, ADelegator);
  294. end;
  295. procedure TTestedClassFilter.SetInput(const ASource: IXPParserTree;
  296.   const ABehaviours: IXPDUnitBehaviours; const ACursorPos: Integer);
  297. var
  298.   SourceSection: IXPParserNode;
  299.   SourceClass: IXPParserNode;
  300.   SourceClassNode: IXPClassNode;
  301.   SourceVisibility: IXPParserNode;
  302.   SourceMethod: IXPParserNode;
  303.   CurrentVisibility: TXPClassVisibility;
  304.   FilterSection: IXPSectionNode;
  305.   FilterClass: IXPClassNode;
  306.   FilterVisibility: IXPVisibilityNode;
  307. begin
  308.   Clear;
  309.   ASource.Children.Start;
  310.   while ASource.Children.Next(SourceSection) do
  311.   begin
  312.     SourceSection.Children.Start;
  313.     while SourceSection.Children.Next(SourceClass) do
  314.     begin
  315.       SourceClassNode := SourceClass as IXPClassNode;
  316.       // Exclude all sections and classes except that class (and section)
  317.       // whose declaration contains ACursorPos
  318.       if (SourceClassNode.ClassBegin > ACursorPos)
  319.         or (SourceClassNode.ClassEnd < ACursorPos) then
  320.         continue;
  321.       FilterSection := CreateXPSectionNode(self,
  322.         (SourceSection as IXPSectionNode).GetSection, SourceSection.Enabled);
  323.       FilterClass := CreateXPClassNode(FilterSection, SourceClass.Name,
  324.         SourceClass.Enabled);
  325.       while SourceClass.Children.Next(SourceVisibility) do
  326.       begin
  327.         CurrentVisibility
  328.           := (SourceVisibility as IXPVisibilityNode).GetVisibility;
  329.         if (SourceVisibility.ChildCount = 0) then
  330.           // Exclude empty visibility nodes.
  331.           FilterClass.DeleteChild(
  332.             FilterClass.Visibilities[CurrentVisibility] as IXPParserNode)
  333.         else
  334.         begin
  335.           // Add source methods to filter visibility node.
  336.           FilterVisibility := FilterClass.Visibilities[CurrentVisibility];
  337.           // Apply behaviours
  338.           if CurrentVisibility = cvPrivate then
  339.             FilterVisibility.Enabled
  340.               := ABehaviours.ClassAddCurrentPrivateMethods
  341.           else if CurrentVisibility = cvProtected then
  342.             FilterVisibility.Enabled
  343.               := ABehaviours.ClassAddCurrentProtectedMethods
  344.           else if CurrentVisibility = cvPublic then
  345.             FilterVisibility.Enabled
  346.               := ABehaviours.ClassAddCurrentPublicMethods
  347.           else
  348.             FilterVisibility.Enabled
  349.               := ABehaviours.ClassAddCurrentPublishedMethods;
  350.           SourceVisibility.Children.Start;
  351.           while SourceVisibility.Children.Next(SourceMethod) do
  352.             CreateXPMethodNode(FilterVisibility, SourceMethod.Name,
  353.               SourceMethod.Enabled);
  354.         end;
  355.       end;
  356.       // We have finished with so bail now rather than keep iterating
  357.       exit;
  358.     end;
  359.   end;
  360. end;
  361. end.